System.Net.Sockets.Socket class hangs when Receive method is invoked
-
Friday, February 03, 2012 5:28 AM
Hi,
I am very new to sockets so please bear with me. I'm trying to send and receive a string data to and from another machine in our network. I tried 2 different approach in doing this in .NET but whenever I read the data that is sent by the server, the program hangs. The odd thing is that when I execute the perl script that my colleague created, I get the response data properly. So, I'm not really sure what the problem is. I just know that there shouldn't be any problem with my computer accessing the server since I was able to execute the perl script successfully.
Here's the first approach:
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) { var ip = "my.ip.add.ress"; var ipAddress = IPAddress.Parse(ip); var port = 9999; var endpoint = new IPEndPoint(ipAddress, port); socket.Connect(ip, port); if (socket.Connected) { var request = "my data"; var bytes = Encoding.UTF8.GetBytes(request); socket.Send(bytes); var str = string.Empty; var buffer = new byte[4096]; var i = socket.Receive(buffer); // this is where it hangs } }
Here's the second approach:var tcp = new TcpClient("my.ip.add.ress", 9999); var stream = tcp.GetStream(); var writer = new StreamWriter(stream); var request = "my data"; writer.Write(request); writer.Flush(); var reader = new StreamReader(stream); var response = reader.ReadToEnd(); // this is where it hangs stream.Close();
Here's the code where I invoke the perl script successfully:var arg = "my data"; var info = new ProcessStartInfo("perl.exe", string.Format("C:\\myperl.pl {0}", arg)); info.RedirectStandardOutput = true; info.CreateNoWindow = true; info.ErrorDialog = false; info.UseShellExecute = false; info.WindowStyle = ProcessWindowStyle.Hidden; var process = new Process { StartInfo = info }; process.Start(); process.WaitForExit(); var str = process.StandardOutput.ReadToEnd().Trim(); // Success!
I hope someone can help me with this. Thanks to all in advance!
- Edited by ponki.d.monkey Friday, February 03, 2012 8:07 AM
- Edited by ponki.d.monkey Friday, February 03, 2012 8:09 AM
All Replies
-
Friday, February 03, 2012 9:53 AM
Hi,
did you read http://msdn.microsoft.com/en-us/library/8s4y8aff.aspx?
Receive blocks when no data is available. Are you sure, that data was sent back?
You could also check the Available Property to see, if data is available to be read.
With kind regards,
Konrad
-
Monday, February 06, 2012 2:17 AM
Hi Konrad,
Thank you very much for your reply and sorry for my late reply. I completely forgot about this problem over the weekend.
Anyway, thanks for the link. It was very helpful.
I'm back in the office and working on this problem now. I checked the Socket.Available property just now and it's 0. So, I guess I'm not getting anything from the server. I set the Socket.ReceiveTimeout property to 2000 and this is the error that was thrown: "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."
I don't understand why when I use .NET it fails to connect or get any data to and from the server while the Perl script can connect and receive data successfully.
Anyway, I'll continue checking what I can do about this. If you have any idea why this happens, feel free to reply to this post. Thanks again.
-
Monday, February 06, 2012 10:26 AM
Hi,
you should check our the protocoll the server expects.
Maybe you even want to check, what the perl client is doing. You was sending text "my data" - on text based protocols, delimiters are commonly used e.g. a newline or 2 newlines. So maybe you want to send something like "my data\r\n" instead? But that is just a guess because we know nothing about the server you are taling to.
With kind regards,
Konrad

