[英]how can I make the server receive pictures from several clients sockets
so here is my server code that receive a picture from my client 所以这是我的服务器代码,可以从我的客户那里收到一张照片
public void Hey()
{
Functions.ServerSend("Picture");
if (Functions.serverrecievetext().CompareTo("Okay") == 0)
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
LingerOption lo = new LingerOption(false, 0);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lo);
try
{
sck.Bind(Login.ip_ep);
sck.Listen(100);
Socket accepted = sck.Accept();
byte[] buff = new byte[300000];
MemoryStream m = new MemoryStream();
read = accepted.Receive(buff, buff.Length, 0);
m.Write(buff, 0, read);
while (read > 0)
{
read = accepted.Receive(buff, buff.Length,0);
if (read != 0)
m.Write(buff, 0, read);
}
Bitmap p = new Bitmap(m, false);
m.Dispose();
pictureBox1.Image = p;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
accepted.Close();
sck.Dispose();
sck.Close();
}
catch (Exception f)
{
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Thread t = new Thread(Hey);
t.Start();
}
it works just fine for one client and here is the client code : 它仅对一个客户端有效,这是客户端代码:
private void timer1_Tick(object sender, EventArgs e)
{
p = Functions.ClientRecieve();
if (p.CompareTo("Picture") == 0)
{
Functions.ClientSendText("Okay");
Bitmap b = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(b as Image);
g.CopyFromScreen(0, 0, 0, 0, b.Size);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = b;
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sck.Connect(Login.ip_ep);
MemoryStream s = new MemoryStream();
pictureBox1.Image.Save(s, System.Drawing.Imaging.ImageFormat.Png);
sck.Send(s.ToArray());
sck.Disconnect(true);
sck.Dispose();
sck.Close();
}
catch (Exception f)
{
}
}
else
{
Functions.ClientSendText("Okay");
timer1.Stop();
this.Close();
}
}
I want the server to have the ability to receive different pictures from different clients at the same time (if that's even possible) note I open a new server when I click on button and it gives command to start new client too so when I click on the button two times I want to have two server forms each one is getting different pictures thanks in advance ^^ 我希望服务器能够同时从不同的客户端接收不同的图片(如果可能的话)请注意,当我单击按钮时,我打开了一个新服务器,它也给出了启动新客户端的命令,所以当我单击时该按钮两次,我想拥有两个服务器表单,每个表单都获得不同的图片,在此先感谢^^
You want to receive different pictures from multiple clients at a same time. 您想同时从多个客户端接收不同的图片。 Then you'll need to learn using threads.
然后,您需要学习使用线程。 The recipient server will listen for clients and start a new thread for each client where code to receive image will be written.
接收方服务器将侦听客户端,并为将写入接收图像代码的每个客户端启动一个新线程。 Like this you can simultaneously receive or send data in parallel manner.
这样,您可以同时并行接收或发送数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.