C#winfrom中TCP客户端同时发送几个指令和同时接收多个响应的问题

weixin_39556523 2017-11-07 07:15:16
想实现客户端同时发送几个指令和同时接收多个响应,但现在读回来的响应相互有影响,就是两个响应的数据连在一起。
感觉好像没有分开通道一样,
客户端同时发送给服务器没有问题,因为服务器没有反馈错误的响应
而客户端接收的响应我想实现分开,要怎么实现?



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;///
using System.Net.Sockets;///
using System.Threading;
using System.IO;

namespace KV_7500监控软件多线程2
{
public partial class Form1 : Form
{
TcpClient client;

UpdateMessage outdelegate1;///声明委托
UpdateMessage outdelegate2;///声明委托
UpdateRead UR; ///声明委托
Thread thread;
Thread thread1;
int port;
int a;
string ip;
int TCP;
/// -------------------------------------------------------------------------------
/// -----------------------------线程委托--------------------------------------------
public delegate void UpdateMessage(string mes);///声明一个委托类型 UpdateMessage
public void UpdatePortMessage(string mes) ///声明一个UpdateMessage类型的对象
{
label3.Text = mes;///委托读取网络连接状态
}
public delegate void UpdateRead(string read);///声明一个委托类型 UpdateRead
public void UpdatePortRead(string read)///声明一个UpdatePortRead类型的对象
{
textBox3.Text = read;
textBox8.Text= read;
//a = int.Parse(read);
//if (a == 55) { label3.Text = "已连接"; }
//else if (a != 55) { label3.Text = "未连接"; }
}
///-------------------------------------------------------------------------------
///-------------------------------------------------------------------------------
public Form1()
{
InitializeComponent();
client = new TcpClient();

outdelegate1 = new UpdateMessage(UpdatePortMessage);
outdelegate2 = new UpdateMessage(UpdatePortMessage);
UR = new UpdateRead(UpdatePortRead);
}
///-------------------------------------------------------------------------------
///--------------------------------窗口加载---------------------------------------
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "192.168.0.2";
textBox1.ReadOnly = true;
textBox2.Text = "8501";
textBox2.ReadOnly = true;
thread = new Thread(TcplinkFuntion);//声明一个TCP连接线程
thread.IsBackground = true;//TCP线程在后台执行
thread.Start();//TCP线程开始执行
}
///-------------------------------------------------------------------------------
///-------------------------------------------------------------------------------


///-------------------------------------------------------------------------------
///--------------------------------Tcp连接的线程----------------------------------
private void TcplinkFuntion()//
{
if (a == 1) { thread1.Abort();a = 0; }
//if (client != null)
//{ client.Close(); }
ip = textBox1.Text;
port = Convert.ToInt32(textBox2.Text.Trim());//将端口号强制为32位整型,存放在port
try{ client.Connect(IPAddress.Parse(ip), port);
if (client != null){
thread1 = new Thread(read);
thread1.IsBackground = true;//线程在后台执行
thread1.Start();
return; }
}
catch {TcplinkFuntion(); }//(SocketException a)MessageBox.Show("连接服务器失败!!!" + a.Message);
}
///-------------------------------------------------------------------------------
///-------------------------------------------------------------------------------
private void read()//
{
thread.Abort();
while (true)
{
//if (client.Client.Poll(20, SelectMode.SelectRead)) //如果远程主机处于关机状态或关闭了连接,则 Available 会引发 SocketException。
if ((client.Connected)&& (client != null))
{
try
{
NetworkStream ns = client.GetStream();
//写入数据
byte[] outStream = Encoding.ASCII.GetBytes("RDS D2000 1\r");// "RDS D0.D 3\r""?K\r"
ns.Write(outStream, 0, outStream.Length);
//读取数据
byte[] inStream = new byte[8];//定义一个内存缓冲区 用于临时性存储接收到的信息
if (ns.DataAvailable)
{
ns.Read(inStream, 0, inStream.Length);
string str = System.Text.Encoding.Default.GetString(inStream);
this.BeginInvoke(UR, new object[] { str });
System.Threading.Thread.Sleep(1 * 100);
}
}
catch (SocketException ab)
{
MessageBox.Show("0000!!!" + ab.Message);
}
catch (IOException abc) { }
// { try
// {
// a = 1;
// thread = new Thread(TcplinkFuntion);//声明一个TCP连接线程
// thread.IsBackground = true;//TCP线程在后台执行
// thread.Start();
// }
// catch (ThreadStateException abcd) { MessageBox.Show("???" + abcd.Message); }
// MessageBox.Show("!!!" + abc.Message);
// }
}
}
}

private void button1_Click(object sender, EventArgs e)
{
NetworkStream run = client.GetStream();
//写入数据
byte[] outRUN = Encoding.ASCII.GetBytes("M1\r");// "RDS D0.D 3\r""?K\r"
run.Write(outRUN, 0, outRUN.Length);
//读取数据
byte[] inRUN = new byte[1024];
run.Read(inRUN, 0, inRUN.Length);
string inRUN1 = System.Text.Encoding.Default.GetString(inRUN);//数据转string
textBox4.Text = inRUN1;
}
private void button2_Click(object sender, EventArgs e)
{
NetworkStream prg = client.GetStream();
//写入数据
byte[] prgRUN = Encoding.ASCII.GetBytes("M0\r");// "RDS D0.D 3\r""?K\r"
prg.Write(prgRUN, 0, prgRUN.Length);
}
}

}
...全文
261 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧