igor92128
asked on
C# Network Program hangs
I got a question about my first C# networking program. I have a server and a client program, and the server just sends out numbers to the client. I don't just want the client to receive but to send also. For example, to create commands to send only odd/even numbers, or numbers in some range.
My server program is multithreaded and it goes like this:
while true
{
SocketComm socObj = new SocketComm();
sockObj.dummySocket = tlsserver.AcceptSocket();
Thread thd = new Thread(new ThreadStart(sockObj.Thread Proc));
thr.IsBackground = true;
thr.Start();
thr.Sleep(750);
}
public void ThreadProc()
{
NetworkStream serverSockStream = new NetworkStream(mySocket);
StreamReader serverStreamReader = new StreamReader(serverSockStr eam);
StreamWriter serverStreamWriter = new StreamWriter(serverSockStr eam);
Random random = new Random();
while(true)
{
int randomNumber.Next(1000);
try
{
// This works fine and sends the numbers to client like expected
serverStreamWriter.WriteLi ne(randomN umber);
// Client times out when I add this line:
// string returndata = serverStreamReader.Readlin e();
}
catch
{ break; }
In the client, I connect to the server like this:
private void ConnectToServer(string ServerIP, int ServerPort)
{
// Create a new instance of a TCP client
tcpClient = new TcpClient();
try
{
// Connect the TCP client to the specified IP and port
tcpClient.Connect(ServerIP , ServerPort);
NetworkStream clientSockStream = tcpClient.GetStream();
clientStreamReader = new StreamReader(clientSockStr eam);
clientStreamWriter = new StreamWriter(clientSockStr eam);
and when I click 'Get Number" in the form, these are the important lines:
...
string returndata = clientStreamReader.ReadLin e();
lbparam.Items.Add(returnda ta);
...
And when I want to send something to the server, I have this line:
clientStreamWriter.WriteLi ne(myComma ndToServer );
Now, the problem is, the client hangs if I add this line to read in the while loop of ThreadProc in the server:
string returndata = serverStreamReader.Readlin e();
Why does the client hang? How do I get the server to also read from a stream?
Thanks,
Igor
My server program is multithreaded and it goes like this:
while true
{
SocketComm socObj = new SocketComm();
sockObj.dummySocket = tlsserver.AcceptSocket();
Thread thd = new Thread(new ThreadStart(sockObj.Thread
thr.IsBackground = true;
thr.Start();
thr.Sleep(750);
}
public void ThreadProc()
{
NetworkStream serverSockStream = new NetworkStream(mySocket);
StreamReader serverStreamReader = new StreamReader(serverSockStr
StreamWriter serverStreamWriter = new StreamWriter(serverSockStr
Random random = new Random();
while(true)
{
int randomNumber.Next(1000);
try
{
// This works fine and sends the numbers to client like expected
serverStreamWriter.WriteLi
// Client times out when I add this line:
// string returndata = serverStreamReader.Readlin
}
catch
{ break; }
In the client, I connect to the server like this:
private void ConnectToServer(string ServerIP, int ServerPort)
{
// Create a new instance of a TCP client
tcpClient = new TcpClient();
try
{
// Connect the TCP client to the specified IP and port
tcpClient.Connect(ServerIP
NetworkStream clientSockStream = tcpClient.GetStream();
clientStreamReader = new StreamReader(clientSockStr
clientStreamWriter = new StreamWriter(clientSockStr
and when I click 'Get Number" in the form, these are the important lines:
...
string returndata = clientStreamReader.ReadLin
lbparam.Items.Add(returnda
...
And when I want to send something to the server, I have this line:
clientStreamWriter.WriteLi
Now, the problem is, the client hangs if I add this line to read in the while loop of ThreadProc in the server:
string returndata = serverStreamReader.Readlin
Why does the client hang? How do I get the server to also read from a stream?
Thanks,
Igor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.