i did create socket application to send and receive images over internet
i did test the application locally and on vmware , it did works fine
also test it on some devices over internet also worked fine
but there is other devices that the Server application receive only small amount of the sent data
here is an image of the server image
and this is the server code
while(true)
{
Socket socket = listener.AcceptSocket();
label4.Text = "Connected";
byte[] image = new byte[99999999];
int offset = -1;
int ite = 0;
while (true)
{
byte[] data = new byte[1024];
int count = socket.Receive(data);
if (count > 0)
{
String txt = Encoding.ASCII.GetString(data, 0, count);
if (txt.Contains("hello"))
{
label8.Text = txt;
continue;
}
if (!txt.Contains("end"))
{
ite += 1;
label7.Text = ite.ToString();
label12.Text = offset.ToString();
for (int i = 0; i <= count - 1; i++)
{
offset += 1;
image[offset] = data[i];
}
try
{
MemoryStream stream = new MemoryStream(image, 0, offset);
Image x = Image.FromStream(stream);
pictureBox1.Image = x;
}
catch (Exception ex)
{
}
}
else
{
label7.Text = ite.ToString();
ite = 0;
label5.Text = label2.Text;
label2.Text = offset.ToString();
MemoryStream stream = new MemoryStream(image, 0, offset);
Image x = Image.FromStream(stream);
pictureBox1.Image = x;
image = new byte[99999999];
offset = -1;
break;
}
}
}
}
and this is the client code
while (true)
{
TcpClient client = new TcpClient(ip, port);
NetworkStream stream = client.GetStream();
byte[] data = Capture();
int dataSize = 1024;
int offset = 0;
byte[] start = ASCIIEncoding.ASCII.GetBytes("hello " + data.Length.ToString());
stream.Write(start, 0, start.Length);
while (offset < data.Length)
{
int sum = offset + dataSize;
if (sum > data.Length)
{
dataSize = data.Length - offset;
}
stream.Write(data, offset, dataSize);
offset += dataSize;
}
Thread.Sleep(1000);
byte[] end = ASCIIEncoding.ASCII.GetBytes("end");
stream.Write(end, 0, end.Length);
client.Close();
Thread.Sleep(interval);
Console.WriteLine("iter=" + iteration.ToString());
}
socketintoimage. 2) Your code incorrectly assumes that a socket read will return a complete message. TCP/IP is a streaming protocol, and if you expect 100 bytes, you must read until you've accumulated 100 bytes.Socket.ReceivenorNetworkStream.Readwait until the requested number of bytes have been received. And often you only see partial reads in some network scenarios.