• XSS.stack #1 – первый литературный журнал от юзеров форума

c# socket sending images issue

thesecure123

CD-диск
Пользователь
Регистрация
09.08.2021
Сообщения
15
Реакции
6
hello

i am creating application to send images over socket

on some devices the server receives only part of the packet not all of it

also on some other devices the app is working fine

note that i send the images over the internet not in the internal network

this is the server

C#:
TcpListener listener = new TcpListener(port);
listener.Start();
 
Socket socket = listener.AcceptSocket();
 
byte[] data = new byte[4];
int count = socket.Receive(data, SocketFlags.None);
int messagesize = 0;
 
//could optionally call BitConverter.ToInt32(sizeinfo, 0);
messagesize |= data[0];
messagesize |= (((int)data[1]) << 8);
messagesize |= (((int)data[2]) << 16);
messagesize |= (((int)data[3]) << 24);
 
int len = 0;
label3.Text = messagesize.ToString();
while(len != messagesize)
{
    data = new byte[1024];
    len += socket.Receive(data);
    label4.Text = len.ToString();
}

the client

C#:
byte[] data = Capture();
TcpClient client = new TcpClient(ip, port);
 
NetworkStream stream = client.GetStream();
 
int length = data.Length;
byte[] sizeinfo = new byte[4];
 
//could optionally call BitConverter.GetBytes(data.length);
sizeinfo[0] = (byte)data.Length;
sizeinfo[1] = (byte)(data.Length >> 8);
sizeinfo[2] = (byte)(data.Length >> 16);
sizeinfo[3] = (byte)(data.Length >> 24);
 
stream.Write(sizeinfo, 0, sizeinfo.Length);
stream.Write(data, 0, data.Length);
stream.Close();
client.Close();
 
How u identify unfinished message? Try to add some logging, to check where problem occurs.

Ex:

Client:
[Out] Payload length: {length}
[Out] Payload length bytes: {BitConverter.ToString(sizeinfo)}

Server:
[In] Payload length bytes: {BitConverter.ToString(data)}
[In] Payload length: {messagesize}

And ofc save source & received payloads, then try to identify what part of message missed.
 
if you read the server code you will notice that i receive the image length first then doing a while loop until i reach the full length of the packet size

If u read my prev message you will notice that i asked:
How u identify unfinished message
And second i advised u to add logging, to see the whole picture at every stage

Is exception thrown - no => It is tcp - no packet loss => Error in code. So, try to find where u code contain error.
I give u advice, how to check every stage of client/server communication. And then u will found differences, it will be easier to identify problem
But no, better to ask on forum for fix, and then somebody will give you advice, just start trash talking.
 
there is no error happen in my code
the only thing that is happen is that there is some packets not arrived

if the size of packets is 1704288 byte i got only 24911 byte

then the connection die on the client side , but there is no error on the server side and there is no code to close the connection with the client
 


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх