简体   繁体   中英

C# Socket.Receive() data is out of order & Truncated at the beginning & at End

I and trying to make a TCPServer & TCPClient simultaneously in C# (DotNET 4.7.2) on Windows. From Client I send data by calling Socket.Send() consecutively 4 times, & these 4 Consecutive Sends together are in loop. On the server side I receive Data by looping through until Data available & within loop I print each byte array on the console.

now comes my Doubt:-

  1. some times data comes out of order (not in order in which I called Socket.Send() at client ).

  2. data in the beginning & at the end is truncated.Now people might say that first i need to collect all the bytes & after the receive loop is finished then I should concatenate it and print,Correct,I got it. But The first initial bytes itself are missing then what ?? First byte should always have been same no matter whatever the way i print it,but the inital bytes itself are truncated

  3. how does SocketFlags affect this, specially partial flag ?

  4. does routing affect this ?

  5. Difference between calling send consecutively & sending all at once ? does TCP handles both conditions differently at protocol level or what way it handles consecutive sends at low protocol level ?

  6. is it possible that that 2nd Send() method call data arrived before the 1st send() method call, in consecutive send method calling ?

I am new to socket Programming.

I am attaching the output screenshots also .In the screenshots look at the text marked by yellow.

console ScreenShot1

console Screenshot2

console screenshot3

my code is here :-

This is client side code

    static void Main(string[] args)
    {
        Console.WriteLine("press any key to start");
        Console.ReadLine();

        byte[] data = Encoding.ASCII.GetBytes("@123456789@");
        byte[] data2 = Encoding.ASCII.GetBytes("&.RAJA.BHOJ.&");
        byte[] data3 = Encoding.ASCII.GetBytes("#abcdefghi#");
        byte[] data4 = Encoding.ASCII.GetBytes("/ABCDEFGHI/");

        Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        s.Connect(ipEnd);

    mark1:
        
        int x = s.Send(data);
            x = s.Send(data2);
            x = s.Send(data3);
            x = s.Send(data4);            

        goto mark1;
   }

This is Sever Side Code

    static void Main(string[] args)
    {
        byte[] Bytes = new byte[50];
        Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, protocolType.Tcp);
        listener.Bind(ipEnd);
        listener.Listen(100);

        Socket handler = listener.Accept();
    mark1:
        
        while(handler.Available != 0)
        {
            int byteCount = handler.Receive(Bytes);
            string text = System.Text.Encoding.ASCII.GetString(Bytes);
            Console.WriteLine("received data is :: " + text);
        }     
        
        goto mark1;
    }

The data should never be out of order, however: checking .Available is usually wrong - fortunately it doesn't really matter in this case, because your goto makes it basically a hot loop. In reality, you should usually loop until byteCount is non-positive (unless you're doing zero-length reads, which you aren't). However: you aren't applying any framing. In TCP, reads don't match 1:1 with writes; it is a stream protocol that just offers "the right bytes in the right order, or a dead socket" - it doesn't do anything about messages. As such, it is up to the protocol to define some mechanism for splitting messages. For a text protocol, a line-ending of some variety is common, or a sentinel marker byte such as a non-printing reserved character like nul (byte zero). Your code would be responsible for finding these markers and splitting the input data correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM