using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Drawing.Imaging;
namespace ClientV5
{
public partial class Form1 : Form
{
Socket hostSocket; Thread thread;
public Form1()
{
InitializeComponent();
ThreadPool.QueueUserWorkItem(startListening);
}
private void startListening(object state)
{
try
{
Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint hostIpEndPoint = new IPEndPoint(IPAddress.Any, 9050);
//Connection node
receiveSocket.Bind(hostIpEndPoint);
receiveSocket.Listen(10);
MessageBox.Show("start");
hostSocket = receiveSocket.Accept();
thread = new Thread(new ThreadStart(trreadimage));
thread.IsBackground = true;
thread.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
}
private void trreadimage()
{
try
{
while (true)
{
byte[] b = new byte[1024 * 1000];//Picture of great
hostSocket.Receive(b);
MemoryStream ms = new MemoryStream(b);
Image img = Image.FromStream(ms);
pictureBox1.Image = img;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
thread.Abort();
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
client
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Client2
{
public partial class Form1 : Form
{
Socket sendsocket;
public Form1()
{
InitializeComponent();
}
private void threadimage()
{
try
{
while (true)
{
MemoryStream ms = new MemoryStream();
GetScreen().Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);//Here I use the BMP format
byte[] b = ms.ToArray();
sendsocket.Send(b);
Thread.Sleep(200);//I'm here to set to send a second
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
return;
}
}
private Bitmap GetScreen()
{
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
return bitmap;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
sendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//The instantiation of socket, IP for 192.168.1.106, 10001 for Port
IPEndPoint ipendpiont = new IPEndPoint(IPAddress.Parse(textBox1.Text.Trim()), 9050);
sendsocket.Connect(ipendpiont);
//Establishment of end point
Thread th = new Thread(new ThreadStart(threadimage));
th.IsBackground = true;
th.Start();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
return;
}
// this.Hide();//Hidden form
}
}
}