Hi!

Im having a problem in the next line:

i = socket.Receive(buffer)

buffer is defined as: byte[] buffer = new byte[1024];

It crash when its waiting for the answer.

In c++ im sending this:

len = send(sock,"hola",4,0);

Any idea?

Thanks

Dani AI

Generated

As later reported, the real problem was a second Receive loop running on a different thread. Two concurrent readers on the same Socket lead to races, blocking surprises, and hard-to-debug failures. 's request for more code was the right move — knowing whether multiple threads call Receive is one of the first things to check.

Key points and best practices:

  • Only one thread/callback should call Receive/Read for a given socket. If multiple components need the data, have a single reader that dispatches messages to a thread-safe queue or event.
  • Always use the integer return value from Receive. A return of 0 means the peer closed the connection. Decode only the bytes actually read (Encoding.GetString(buffer, 0, bytesRead)).
  • Use try/catch around socket ops and log SocketException details (SocketErrorCode / Message). That makes the real failure mode visible.
  • Consider nonblocking modes, a ReceiveTimeout, or async APIs (BeginReceive/EndReceive, ReceiveAsync, or NetworkStream.ReadAsync) instead of blocking calls on arbitrary threads.
  • Agree on framing between sender and receiver (length prefix or delimiter). A C++ call like send(sock,"hola",4,0) sends exactly 4 bytes (no NUL); both sides must expect the same framing.

Example receiver pattern (one dedicated reader):

var buf = new byte[1024];
try {
    while (true) {
        int n = socket.Receive(buf, 0, buf.Length, SocketFlags.None);
        if (n == 0) break; // remote closed
        string msg = Encoding.UTF8.GetString(buf, 0, n);
        messageQueue.Enqueue(msg); // single reader hands off work
    }
} catch (SocketException ex) {
    // log ex.SocketErrorCode and ex.Message
}

Troubleshooting tips: add logging around thread entry/exit for any receive loops, use Socket.Available or Poll to inspect readiness, and capture packet traces (e.g., Wireshark) to confirm what the peer actually sends.

Recommended Answers

All 3 Replies

Do you open the socket? We'll need more code to see what might be the problem.

Yeah, socket is well open beacuse first i tried to send a string, and the string go well.

getSocket(numsock).socketSend("filemanager"); --> this is ok, beacuse server recive the string.
 Console.WriteLine(getSocket(numsock).socketRecv()); --> I will put down the method code
public string socketRecv()
        {
             buffer = new byte[1024];
                        i = socket.Receive(buffer); --> Here crash 
                        string recibido = System.Text.Encoding.UTF8.GetString(buffer);
                        return recibido;

        }

Solved !!
I had a loop o "receive()" in other thread of other class xD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.