求助聊天通信
程序代码:
static int port = 2000; static string host = "127.0.0.1"; static IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port); Thread thrd1; private void Form1_Load(object sender, EventArgs e) { thrd1 = new Thread(new ThreadStart(rec)); thrd1.Start(); } private void rec() { Socket st = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); st.Connect(ipe); while (true) { string recStr = ""; byte[] recByte = new byte[1024]; int bytes; bytes = st.Receive(recByte, recByte.Length, 0); recStr += Encoding.UTF8.GetString(recByte, 0, bytes); textBox2.AppendText(DateTime.Now + Environment.NewLine + " " + recStr + Environment.NewLine); } }这段代码哪里有问题。编译没错,就是执行不出来,接受不到服务器发来的信息。
换句话说,怎么才能做到客户端时时都可以收到服务器发来的信息。
请指点一下,谢谢了
我刚接触聊天通信这部分,不太清楚。