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
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
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:
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.
Jump to Post— Momerath 1,327Do you open the socket? We'll need more code to see what might be the problem.
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.