roburst network programming, tcpClient.Client.SetSocketOption(SocketOptionLevel.Tcp , SocketOptionN

  • Thread starter Thread starter Ryan Liu
  • Start date Start date
R

Ryan Liu

Hi,

I use

Server:
Use an endless thread to lisiten to clients requests:

while(true)
{
TcpClient client = myListener.AcceptTcpClient();
....
}


Client:
tcpClient = new TcpClient(host,port);
stream =tcpClient.GetStream();
....


to build a client/server application, then both server and clients start
another endless thread to receive messages from each other, and use yet
other threads to send response to each other, or server pass messages from
one client to another.

Now about 60 workstations connect to one server, all in the same local LAN.

It is very ofter on server side log I see network problem for both read and
write to network stream, like:

Unable to write data to the transport connection.
An established connection was aborted by the software in your host machine.

Read System.InvalidOperationException: Operation not allowed on
non-connected sockets.

Can someone tell me what will cause the problem and how to write a roburst
network application?

I am using TcpClient, in SDK, I see there is a method

Socket.SetSocketOption(), which can set SocketOptionName.KeepAlive

What does this mean, will this be helpful?

And do I have set this on both client and server side? And is this the right
way to use it:
tcpClient.Client.SetSocketOption(SocketOptionLevel.Tcp ,
SocketOptionName.KeepAlive , 1);

Thanks a lot!
Ryan
 
Ryan said:
It is very ofter on server side log I see network problem for both read and
write to network stream, like:

Unable to write data to the transport connection.
An established connection was aborted by the software in your host machine.

Read System.InvalidOperationException: Operation not allowed on
non-connected sockets.

Can someone tell me what will cause the problem and how to write a roburst
network application?

I am using TcpClient, in SDK, I see there is a method

Socket.SetSocketOption(), which can set SocketOptionName.KeepAlive

What does this mean, will this be helpful?

I don't think KeepAlives will help you (see google). You are getting the
error because you are writing to a socket whose connection has been
closed (either intentionally or because of other reasons like network
problems or a crashed server).

When calling Socket.Receive you should 1) use a try/catch block around
it and 2) check the return value. A return value of 0 means that the
connection has been closed. You should not call Send on the socket after
it returned 0 bytes or after an exception. Close and destroy it.

btw - why not use async sockets instead of starting so many threads?

hth,
Max
 
Hi Markus,

Actually I have no problem with Listener, I have problem after the
connection is made. Then the server spawn one thread for each connected
client.

Actually there is not lot of threads. Only one thread for server to listen,
and one thread in server to receive data from each client and handle it.

When one client send message to server then server pass to another client,
it only involves one thread of the first client, but 2 network streams.

Thanks,
Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top