problem with socket programing for image transformation
-
hi at first excuse me because of my poor english i have written a program for sending an image or for video conference via internet with sockets and i am using tcp protocols my problem is that when i test program on my computer it works fine but when i test it over internet with between two pcs the reciver cant recieve image correctly minwhile i am using dial up connection i dont know where is the problem perhaps it is because of low speed of internet i changed the buffer size to 512 1024 4096 and it didnt change. or i maybe should compress or encode image i dont know .please please help me with this problem. :(( here is the code for sending an image : private void button2_Click(object sender, EventArgs e) { byte[] bytes; using (MemoryStream ms = new MemoryStream()) { pictureBox1.Image.Save(ms,ImageFormat.Jpeg); pictureBox2.Image = pictureBox1.Image; bytes = ms.ToArray(); ms.Close(); } startcapturing(bytes); } private void startcapturing(byte[] bytes) { int i = 0; byte[] dataBuffer = new byte[128]; int index = 0; byte[] loop = new byte[128]; loop = System.Text.Encoding.ASCII.GetBytes(((bytes.Length / 128) + 1).ToString()); label4.Text = ((bytes.Length / 128) + 1).ToString(); if (serverSocket != null) { try { rc = clientSocket.Send(loop, SocketFlags.None); while(i= bytes.Length) { dataBuffer[index] = 0; } else { dataBuffer[index] = bytes[i]; i++; } } rc = lientSocket.Send(dataBuffer,SocketFlags.None); } } catch (Exception err) { MessageBox.Show(err.Message.ToString()+"here"); } }
-
hi at first excuse me because of my poor english i have written a program for sending an image or for video conference via internet with sockets and i am using tcp protocols my problem is that when i test program on my computer it works fine but when i test it over internet with between two pcs the reciver cant recieve image correctly minwhile i am using dial up connection i dont know where is the problem perhaps it is because of low speed of internet i changed the buffer size to 512 1024 4096 and it didnt change. or i maybe should compress or encode image i dont know .please please help me with this problem. :(( here is the code for sending an image : private void button2_Click(object sender, EventArgs e) { byte[] bytes; using (MemoryStream ms = new MemoryStream()) { pictureBox1.Image.Save(ms,ImageFormat.Jpeg); pictureBox2.Image = pictureBox1.Image; bytes = ms.ToArray(); ms.Close(); } startcapturing(bytes); } private void startcapturing(byte[] bytes) { int i = 0; byte[] dataBuffer = new byte[128]; int index = 0; byte[] loop = new byte[128]; loop = System.Text.Encoding.ASCII.GetBytes(((bytes.Length / 128) + 1).ToString()); label4.Text = ((bytes.Length / 128) + 1).ToString(); if (serverSocket != null) { try { rc = clientSocket.Send(loop, SocketFlags.None); while(i= bytes.Length) { dataBuffer[index] = 0; } else { dataBuffer[index] = bytes[i]; i++; } } rc = lientSocket.Send(dataBuffer,SocketFlags.None); } } catch (Exception err) { MessageBox.Show(err.Message.ToString()+"here"); } }
I don't understand the full code, but I must say the problem is probably with the send or the Receive. I am not sure if the Send can send partial data, but receive surelly do. For example: You send 128 bytes. The connection is local, you get 128 bytes. But, if the connection is remote, you can: Receive 10 bytes. Then, receive 20 bytes. + 10 bytes... until you finally receive all the buffer (the 128 you expected). So, you must check this. Also, I think a better approach is: Instead of writing everything to a memorystream and them sending the memorystream buffer, get the socket stream (socket.GetStream()) and save the image directly to the socket stream. Then, on the other side, call Image.FromStream(socket.GetStream())
-
I don't understand the full code, but I must say the problem is probably with the send or the Receive. I am not sure if the Send can send partial data, but receive surelly do. For example: You send 128 bytes. The connection is local, you get 128 bytes. But, if the connection is remote, you can: Receive 10 bytes. Then, receive 20 bytes. + 10 bytes... until you finally receive all the buffer (the 128 you expected). So, you must check this. Also, I think a better approach is: Instead of writing everything to a memorystream and them sending the memorystream buffer, get the socket stream (socket.GetStream()) and save the image directly to the socket stream. Then, on the other side, call Image.FromStream(socket.GetStream())
thanks sockets doesnt have Getstream() methode only tcp client has it please if you guide me write more cause i am new to c# thanks in advance
-
thanks sockets doesnt have Getstream() methode only tcp client has it please if you guide me write more cause i am new to c# thanks in advance
Well... I usually use sockets by TcpClient in C#... but, the same way, I am not sure about send, but when reading, you must do something like this: Instead of read(buffer, 0, 128) you set: int totalRead = 0 int count = 128; while(count > 0) { int actualRead = socket.Read(buffer, totalRead, count); if (actualRead <= 0) throw new Exception("Connection lost"); totalRead += actualRead; count -= actualRead; }
-
Well... I usually use sockets by TcpClient in C#... but, the same way, I am not sure about send, but when reading, you must do something like this: Instead of read(buffer, 0, 128) you set: int totalRead = 0 int count = 128; while(count > 0) { int actualRead = socket.Read(buffer, totalRead, count); if (actualRead <= 0) throw new Exception("Connection lost"); totalRead += actualRead; count -= actualRead; }
ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh :omg: :(( :(( :(( oh my god you solved my problem oh my god thank you thank you thanks please please let me kiss your foot :(( :(( i wont forget your name paulo zemek i wont forget you thanks thanks a milion thanks a bunch let me let me kiss your foot :(( :(( :(( :(( :(( :(( i am really crying here :(( :(( :(( :(( thanks thanks .................................. ................................................ ................................................. .................................................. ...................................................