SpecialA10
asked on
Multi Client TCP Server
Here is the deal:
I need to have a server running that is goign to do two things:
1 - Accept a "registration" or "sign on" from a client application, and store the username and i suppose "socket" of that machine
2 - Accept a "hit" from ANOTHER server application, that is goign to tell the server which "client" (step 1) to notify of a hit
The server app then needs to loop through the list of connected clients, and send the message to each of them.
I am 600% lost.....NOTE: This is not in ANY WAY a commercial application, I am just playing around with C# - trying to learn the ropes.
Any help?
Best,
Avery
I need to have a server running that is goign to do two things:
1 - Accept a "registration" or "sign on" from a client application, and store the username and i suppose "socket" of that machine
2 - Accept a "hit" from ANOTHER server application, that is goign to tell the server which "client" (step 1) to notify of a hit
The server app then needs to loop through the list of connected clients, and send the message to each of them.
I am 600% lost.....NOTE: This is not in ANY WAY a commercial application, I am just playing around with C# - trying to learn the ropes.
Any help?
Best,
Avery
Okay, your problem is easy.. but will you tell me where do you need help? Or do you want a complete solution for this?
One more thing, you can check the following article: http://www.codeproject.com/csharp/tcpclientserver.asp
It's a simple client-server connection using TCP.. And for your second problem, just think that a server can also act as a client to another server. Just connect to other server and send any data you want.
It's a simple client-server connection using TCP.. And for your second problem, just think that a server can also act as a client to another server. Just connect to other server and send any data you want.
ASKER
Thanks for your response,
It is not quite as simple as that example (which i have seen before, but thanks for the link). The server probibly needs to be multi-threaded so it can continuesly listen for new connections, thats where the confusion sets in.
Thanks,
Avery
It is not quite as simple as that example (which i have seen before, but thanks for the link). The server probibly needs to be multi-threaded so it can continuesly listen for new connections, thats where the confusion sets in.
Thanks,
Avery
Well, since I don't have a ready sample for you, you better start coding and ask when you're stuck.. I mean, there are a few things you should know. I see that you already know about System.Net, System.Net.Sockets and simple socket programming. You'll just have to create a listener using TcpListener, start listening.. and when a client connects to your listener, you will Accept() it and pass this Socket object to a new thread. That's it.. Your thread will take care of the client. And also you can do some messaging between threads and your main thread (using delegates, events).
I suggest you to create a listener thread, so your server will always be listening.
So, your server application will be something like this:
ClientThread ct;
Thread t;
TcpListener listener = new TcpListener( IPAddress.Any, 5678 );
while( true )
{
listener.Start();
Socket s = listener.AcceptSocket();
ct = new ClientThread( s, this );
t = new Thread( new ThreadStart( ct.Run ) );
}
public class ClientThread
{
Socket s = null;
Object parent = null;
public ClientThread( Socket _s, Object _parent )
{
s = _s;
parent = _parent;
}
public void Run()
{
// Do your client work here... possibly an infinite loop with read operation,
// processing them etc...
}
}
The above code is just written here.. I didn't test it, there might be many errors/warnings.. just look as a pseudo-code..
Caner
I suggest you to create a listener thread, so your server will always be listening.
So, your server application will be something like this:
ClientThread ct;
Thread t;
TcpListener listener = new TcpListener( IPAddress.Any, 5678 );
while( true )
{
listener.Start();
Socket s = listener.AcceptSocket();
ct = new ClientThread( s, this );
t = new Thread( new ThreadStart( ct.Run ) );
}
public class ClientThread
{
Socket s = null;
Object parent = null;
public ClientThread( Socket _s, Object _parent )
{
s = _s;
parent = _parent;
}
public void Run()
{
// Do your client work here... possibly an infinite loop with read operation,
// processing them etc...
}
}
The above code is just written here.. I didn't test it, there might be many errors/warnings.. just look as a pseudo-code..
Caner
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.