O
O
Ogoun Er2013-02-20 16:42:31
.NET
Ogoun Er, 2013-02-20 16:42:31

Socket issue?

I want to implement the following functionality in a client-server application:
the client cools down the data to the server, and the server immediately after receiving the data transfers the number of bytes received to the client.

Server:

using (SocketEx socket = new SocketEx((IntPtr)state))<br>
            {<br>
                byte[] data = socket.Receive();<br>
                socket.Send(BitConverter.GetBytes(data.Length));               <br>
            }<br>


Customer:
public int Send(byte[] data)<br>
        {<br>
            int count = socket.Send(data);<br>
            byte[] response = socket.Receive();<br>
            if (response != null && response.Length == 4)<br>
            {<br>
                return BitConverter.ToInt32(response, 0);<br>
            }<br>
            return -1;<br>
        }<br>


TCP protocol, sockets from the ws2_32 standard library (regular, not WSA).
And the code on the client
is socket.Receive();
does not read the server response (recv returns -1), WSA error is written 10053 - the connection is broken. Although debugging on the server, the send method says that it sent 4 bytes.

Actually the question is how to correctly implement the server's response to the client? Is it possible to write to the same socket from which I read the data?

In theory, the recv method blocks the execution thread and must wait for the data to appear. But why doesn't it wait?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question