c#TCP 客户端怎么监听接收数据
答案:2 悬赏:60 手机版
解决时间 2021-02-26 00:37
- 提问者网友:世勋超人
- 2021-02-25 09:51
c#TCP 客户端怎么监听接收数据
最佳答案
- 五星知识达人网友:長槍戰八方
- 2021-02-25 10:59
#region TCP协议
public void BindTcp()
{
try
{
//执行tcp 通讯
textBox2.ScrollBars = ScrollBars.Vertical;//设置滚动条
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个新的tcp实例
client.Connect(IPAddress.Parse("192.168.1.1"), 4545);//设置ip,端口
//执行线程
th = new Thread(xintiao);
th.Start();
th = new Thread(start1);
th.Start();
}
catch (Exception)
{
MessageBox.Show("执行tcp协议异常");
throw;
}
}
// 接收数据+显示
public void start1()
{
try
{
while (true)
{
string s = Receive(client, 10000 * 2);
string rec = "Receive:" + s + "\r\n";
if (s.Substring(0, 4) == "0231")
{
Start(s);
}
textBox2.Text += rec;
}
}
catch (Exception)
{
MessageBox.Show("显示数据异常");
}
}
//心跳
public void xintiao()
{
try
{
while (true)
{
Thread.Sleep(5000);
Send("00209999001000000000");
}
}
catch (Exception)
{
MessageBox.Show("发送心跳异常");
throw;
}
}
//发送
private static Encoding encode = Encoding.Default;
public void Send(string data)
{
try
{
client.Send(encode.GetBytes(data));
textBox2.Text += "\r\n" + "Send:" + data + "\r\n";
}
catch (Exception)
{
MessageBox.Show("发送异常");
}
}
//接收
private string Receive(Socket socket, int timeout)
{
string result = string.Empty;
socket.ReceiveTimeout = timeout;
List data = new List();
byte[] buffer = new byte[1024];
int length = 0;
try
{
while ((length = socket.Receive(buffer)) > 0)
{
for (int j = 0; j < length; j++)
{
data.Add(buffer[j]);
}
if (length < buffer.Length)
{
break;
}
}
}
catch
{
textBox2.Text += "超时,断开链接";
}
if (data.Count > 0)
{
result = encode.GetString(data.ToArray(), 0, data.Count);
}
return result;
}
#endregion
public void BindTcp()
{
try
{
//执行tcp 通讯
textBox2.ScrollBars = ScrollBars.Vertical;//设置滚动条
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个新的tcp实例
client.Connect(IPAddress.Parse("192.168.1.1"), 4545);//设置ip,端口
//执行线程
th = new Thread(xintiao);
th.Start();
th = new Thread(start1);
th.Start();
}
catch (Exception)
{
MessageBox.Show("执行tcp协议异常");
throw;
}
}
// 接收数据+显示
public void start1()
{
try
{
while (true)
{
string s = Receive(client, 10000 * 2);
string rec = "Receive:" + s + "\r\n";
if (s.Substring(0, 4) == "0231")
{
Start(s);
}
textBox2.Text += rec;
}
}
catch (Exception)
{
MessageBox.Show("显示数据异常");
}
}
//心跳
public void xintiao()
{
try
{
while (true)
{
Thread.Sleep(5000);
Send("00209999001000000000");
}
}
catch (Exception)
{
MessageBox.Show("发送心跳异常");
throw;
}
}
//发送
private static Encoding encode = Encoding.Default;
public void Send(string data)
{
try
{
client.Send(encode.GetBytes(data));
textBox2.Text += "\r\n" + "Send:" + data + "\r\n";
}
catch (Exception)
{
MessageBox.Show("发送异常");
}
}
//接收
private string Receive(Socket socket, int timeout)
{
string result = string.Empty;
socket.ReceiveTimeout = timeout;
List
byte[] buffer = new byte[1024];
int length = 0;
try
{
while ((length = socket.Receive(buffer)) > 0)
{
for (int j = 0; j < length; j++)
{
data.Add(buffer[j]);
}
if (length < buffer.Length)
{
break;
}
}
}
catch
{
textBox2.Text += "超时,断开链接";
}
if (data.Count > 0)
{
result = encode.GetString(data.ToArray(), 0, data.Count);
}
return result;
}
#endregion
全部回答
- 1楼网友:撞了怀
- 2021-02-25 12:27
private void listen设备forclients()
{
localdatabase eventdb = new localdatabase();
string eventprestr = "设备监听线程(" + thread.currentthread.managedthreadid.tostring() + ')';
this.listener设备.start(); eventdb.savestr(eventprestr + "启动");
while (this.ismainrun == true)
{
//blocks until a client has connected to the server
tcpclient client = this.listener设备.accepttcpclient();//有连接接入,
//create a thread to handle communication with connected client
thread clientthread = new thread(new parameterizedthreadstart(handle设备comm));//应答,并启动一个线程处理
//取得来源ip
eventdb.savestr(eventprestr + "接收到来自" + ((system.net.ipendpoint)client.client.remoteendpoint).address.tostring() + "的连接,并启动线程id:" + clientthread.managedthreadid.tostring());
clientthread.start(client);
}
}
我的程序中也是这样写的,你应该注意到
tcpclient client = this.listener设备.accepttcpclient();//有连接接入,
这句话是有设备连接了,才执行,而不是被跳过去,也就是说,循环是在这里等待的。
你说的网络事件通知,其实可以自己做,只不过是意义不大,现在程序框架已经这么成熟了,自己写也没什么意义。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯