I am creating a program similar to Microsoft Netmeeting for that I have to send Multiple type of data from a single connection such as Mouse position, Pressed key and offcourse a screen shot at a time. I am success fully sending and receiving Screen shot to the client but I am unable to understand how multiple data could be send through the single connetion.
I think it require multiple ports for this purpose. One for Screen shot, One for Mouse Pos and One for Key pressed.
Following are the codes I am using: Server = Sender of ScreenShot, Recever of MousePos and Key. Client = Receiver of ScreenShot, Sender of MousePos and Key.
Server:
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);
}
}
Client:
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");
}
}
You can use a single connection, but you have to provide a way for the receiver to differentiate the type of data (and its size) prior to reading it.
That being said, if you're writing both sides of the connection (the client + server), it would be far simpler to use a technology like WCF. It would allow you to pass full class instances with strongly typed data, and take care of the underlying transport (in a configurable way) for you automatically.