| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 582 人关注过本帖
标题:局域网聊天问题(连接成功,却传不了信息)
只看楼主 加入收藏
yanglin_gdqy
Rank: 2
等 级:论坛游民
威 望:3
帖 子:59
专家分:86
注 册:2013-12-2
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:1 
局域网聊天问题(连接成功,却传不了信息)
图片附件: 游客没有浏览图片的权限,请 登录注册
程序代码:
       //服务端
namespace Tcp_Server
{
    public partial class Server_ : Form
    {

 
        public Server_()
        {
            InitializeComponent();
        }

        string m_serverIP;
        int m_port;
        TcpListener m_Listener = null;
        IPAddress m_host;
        private void Server__Load(object sender, EventArgs e)
        {
            m_serverIP = txbServerName.Text;//IP地址
            m_host = IPAddress.Parse(m_serverIP);
            m_port = int.Parse(txbPort.Text);//端口
            m_Listener = new TcpListener(m_host, m_port);
            m_Listener.Start();
            MessageBox.Show("开始侦听");
            TcpClient m_client = m_Listener.AcceptTcpClient();
            handle m_handle = new handle();
            m_handle.ClientSocket = m_client;
            Thread m_clientthread = new Thread(new ThreadStart(m_handle.ResponseClient));
            m_clientthread.Start();
            txbShowIFMT.Text += m_handle.m_DataReturn;
        }
    }
    public class handle
    {
        byte[] EncodingASCII(string buf)//将数据转换为ASCII
        {
            byte[] data = Encoding.ASCII.GetBytes(buf);
            return data;
        }

        private TcpClient m_clientsocket;
        public string m_DataReturn, m_DataSend;
        byte[] data;//中间变量

        public TcpClient ClientSocket
        {
            get
            {
                return m_clientsocket;
            }
            set
            {
                m_clientsocket = value;
            }

        }//m_clientsocket属性

        public void ResponseClient()
        {
            StreamReader rs = new StreamReader(m_clientsocket.GetStream());
            m_DataReturn = rs.ReadLine();
            m_DataSend = "收到:" + m_DataReturn;
            NetworkStream ws = m_clientsocket.GetStream();

            data = EncodingASCII(m_DataSend);
            ws.Write(data, 0, data.Length);
        }
    }
}

程序代码:
//客户端
namespace Tcp_Client
{
    public partial class Client_ : Form
    {
        public Client_()
        {
            InitializeComponent();
        }
        static byte[] EncodingASCII(string buf)//将数据转换为ASCII
        {
            byte[] data = Encoding.ASCII.GetBytes(buf);
            return data;
        }

        string FriendServer, DataSend, DataReturn;
        int port;
        TcpClient m_client;
        byte[] data;
        StreamReader rs;//
        NetworkStream ws;
        private void Client__Load(object sender, EventArgs e)
        {
            FriendServer = txbFriend.Text;
            port = int.Parse(txbPort.Text);//端口
            m_client = new TcpClient();
            m_client.Connect(FriendServer, port);

        }

        private void btnSend_Click(object sender, EventArgs e)
        {

            rs = new StreamReader(m_client.GetStream());
            ws = m_client.GetStream();

            DataSend = txbSend.Text;

            data = EncodingASCII(DataSend);

            ws.Write(data, 0, data.Length);
        }
    }
}

这样用流传递信息是这样的么?求大神指点!

[ 本帖最后由 yanglin_gdqy 于 2014-6-27 14:27 编辑 ]
搜索更多相关主题的帖子: 局域网 信息 
2014-06-27 11:35
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:10 
嗯,嗯嗯嗯,都不知道从哪个问题说起,先解决你的核心问题吧
客户端
private void btnSend_Click(object sender, EventArgs e)
        {

            rs = new StreamReader(m_client.GetStream());
            ws = m_client.GetStream();

            DataSend = txbSend.Text;

            data = EncodingASCII(DataSend);

            ws.Write(data, 0, data.Length);
            ws.Flush(); //加上这句
        }
这样至少可以让你的服务端收到数据,当然把数据显示在文本框中的代码是有问题的,不过调试就可发现,这样做服务端可以收到数据
2014-06-27 21:11
快速回复:局域网聊天问题(连接成功,却传不了信息)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029959 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved