【发布时间】:2011-10-26 17:13:04
【问题描述】:
我正在创建一个类似于 Microsoft Netmeeting 的程序,因为我必须从单个连接发送多种类型的数据,例如鼠标位置、按下的键和一次关闭屏幕截图。我成功地向客户端发送和接收屏幕截图,但我无法理解如何通过单一连接发送多个数据。
我认为为此需要多个端口。一个用于屏幕截图,一个用于鼠标位置,一个用于按键。
以下是我正在使用的代码: 服务器 = ScreenShot 的发送者,MousePos 和 Key 的接收者。 Client = ScreenShot 的接收者,MousePos 和 Key 的发送者。
服务器:
void StartListen()
{
try
{
IPEndPoint ipendp = new IPEndPoint(IPAddress.Parse(OwnIP()), 532);
tcpl = new TcpListener(ipendp);
tcpl.Start();
s1 = tcpl.AcceptSocket();
ns = new NetworkStream(s1);
timer1.Enabled = true;
while (true)
{
byte[] buffer = imageToByteArray(CaptureScreenShot());
s1.Send(buffer, buffer.Length, SocketFlags.None);
Thread.Sleep(250);
}
}
catch
{
tcpl.Stop();
ns.Close();
//tcpl.EndAcceptSocket();
Form1_Load(0,EventArgs.Empty);
}
}
客户:
void StartClient()
{
try
{
IPEndPoint ipendp = new IPEndPoint(IPAddress.Parse(toolStripTextBox1.Text), 532);
this.Text = "SWare Application - " + toolStripTextBox1.Text + ":532";
tcpc = new TcpClient();
tcpc.Connect(ipendp);
Socket s1 = tcpc.Client;
ns = tcpc.GetStream();
while (true)
{
byte[] b = new byte[500000];
s1.Receive(b);
MemoryStream ms = new MemoryStream(b);
pictureBox1.Image = Image.FromStream(ms);
//Thread.Sleep(250);
}
}
catch
{
tcpc.Close();
MessageBox.Show("Disconnected from the Remote PC");
}
}
【问题讨论】:
标签: c# networking tcp tcpclient