I’ve got a TCP server up and running on localhost on vs2010 on windows 2k8.
When I connect the client on 127.0.0.1 using vs2010, the server gets the call, (I can see it
from debug) as the tcp server executes the command line protocol handler, but the client does
recieve the server response, which is the session key, but blocks at the client when reading
the respone stream.
When I use Telnet on loopback for the same port, sending the same command sequence, the
response, i.e. the session key, is received instantaneously.
Here is the client code:
EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9121);
sing (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
socket.Connect(serverAddress);
var socketStream = new NetworkStream(socket);
var reader = new StreamReader(socketStream, Encoding.ASCII, false);
var writer = new StreamWriter(socketStream, Encoding.ASCII, 1024);
string charSource = Guid.NewGuid().ToString().Replace("-", string.Empty)
+ Guid.NewGuid().ToString().Replace("-", string.Empty)
+ Guid.NewGuid().ToString().Replace("-", string.Empty);
Random rd = new Random();
int startPos = rd.Next(0, charSource.Length - 2);
int endPos = rd.Next(startPos + 1, charSource.Length - 1);
var currentMessage = charSource.Substring(startPos, endPos - startPos + 1);
Console.WriteLine("Sent Command");
writer.Write("HEAR {0} {1}", currentMessage.Length.ToString().PadLeft(4, '0'), currentMessage);
writer.Flush();
Console.WriteLine("Reading Command Results");
var line = reader.ReadLine();
Console.WriteLine("Received: " + line);
}
Bob
To make this work, please change Write with WriteLine as shown in the code below: