简体   繁体   中英

Socket.send() hanging/sleeping in server

I have this method which i use to send a Transfer object

        IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 7777);
        Socket sockListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
        sockListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        sockListener.Bind(ipEnd);
        sockListener.Listen(100);


        s = sockListener.Accept();

private void sendToClient(Transfer.Transfer tt)
    {
        byte[] buffer = new byte[15000];

        IFormatter f = new BinaryFormatter();
        Stream stream = new MemoryStream(buffer);
        f.Serialize(stream, tt);
        Console.WriteLine("1/3 serialized");
        stream.Flush();
        Console.WriteLine("2/3 flushed stream");
        s.Send(buffer, buffer.Length, 0);
        Console.WriteLine("3/3 send to client");
    }

The strange thing is it work the first 2 times i call it, then on the 3rd call it hangs on s.send().

Its the same if i want to send String instead of Transfer.

The comment by @nos is probably correct, the TCP Send buffer is probably filling up and the 3rd call to s.send() is blocking until the data is sent over the network. You can set the used buffer sizes like this:

Socket s = sockListener.Accept();
s.ReceiveBufferSize = 1024 * 64;
s.SendBufferSize = 1024 * 64;

You should be able to confirm your problem by setting your buffer sizes to a larger multiple of the size of data you're sending.

Also, as suggested, you should check the client to make sure its reading the data properly.

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