Solved

Multi Client TCP Server

Posted on 2004-04-08
7
328 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:SpecialA10
  • 4
7 Comments
 
LVL 4

Expert Comment

by:caner_elci
ID: 10797310
Okay, your problem is easy.. but will you tell me where do you need help? Or do you want a complete solution for this?
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10797330
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.
0
 

Author Comment

by:SpecialA10
ID: 10797957
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
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10798939
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
0
 
LVL 4

Accepted Solution

by:
caner_elci earned 500 total points
ID: 10798982
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question