Solved

Multi Client TCP Server

Posted on 2004-04-08
7
323 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now