TCP:开启监听Thread thListen = new Thread(new ThreadStart(Listen));
thListen.Start();
如有收到消息,则进行相应处理:
private void Listen()
{
while (appRun)
{
try
{
TcpListener tcpLi = new TcpListener(IPAddress.Any,2009);
tcpLi.Start();
TcpClient tcpCli = tcpLi.AcceptTcpClient();
NetworkStream ns = tcpCli.GetStream(); byte[] buf = new byte[100000];
ns.Read(buf, 0, buf.Length); string[] msg = Encoding.Default.GetString(buf).Split('|');
//此处可以根据接收到的msg进行相应处理
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return;
}
}
}消息发送部分:
private void SendServceLogin(string msg)
{
try
{
TcpClient tcClie = new TcpClient();
tcClie.Connect(IPAddress.Parse(txtIP.Text), 2009); NetworkStream ns = tcClie.GetStream();
byte[] SendB = Encoding.Default.GetBytes(msg); ns.Write(SendB, 0, SendB.Length);
tcClie.Close();
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString(), "提示", MessageBoxButtons.OK);
}
}
希望看到大神的回复
thListen.Start();
如有收到消息,则进行相应处理:
private void Listen()
{
while (appRun)
{
try
{
TcpListener tcpLi = new TcpListener(IPAddress.Any,2009);
tcpLi.Start();
TcpClient tcpCli = tcpLi.AcceptTcpClient();
NetworkStream ns = tcpCli.GetStream(); byte[] buf = new byte[100000];
ns.Read(buf, 0, buf.Length); string[] msg = Encoding.Default.GetString(buf).Split('|');
//此处可以根据接收到的msg进行相应处理
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return;
}
}
}消息发送部分:
private void SendServceLogin(string msg)
{
try
{
TcpClient tcClie = new TcpClient();
tcClie.Connect(IPAddress.Parse(txtIP.Text), 2009); NetworkStream ns = tcClie.GetStream();
byte[] SendB = Encoding.Default.GetBytes(msg); ns.Write(SendB, 0, SendB.Length);
tcClie.Close();
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString(), "提示", MessageBoxButtons.OK);
}
}
希望看到大神的回复
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货