简体   繁体   中英

asynchronous single server multiple clients using c#

I new to networking and c#. I am trying to develop a asynchronous single server to which multiple asynchronous clients can connect and can send and receive messages between each clients. the problem occurs in my server which can connect to only one client. I am using threads to connect to multiple clients but its not working.

I am using .NET 4.5 framework.

Note: I use the StateObject when I receive something from the client. Also, my server works only if one client is connected(if i remove the threadclient() method ).

Server:
public class mainclass
{
    public static void Main()
    {
        newTCPNetworkNode nn = new newTCPNetworkNode();
        nn.Initialize();
        nn.threadClient();            
    }
}

public class StateObject
{
    public Socket workSocket = null;
    public const int BufferSize = 100;
    public byte[] buffer = new byte[BufferSize];
}

class newTCPNetworkNode
{
    private IPAddress addr;
    private IPEndPoint endpoint;
    private TcpListener mListener;
    public static ManualResetEvent allDone = new ManualResetEvent(false);
    public Thread listenthread;

    public void Initialize()
    {
        addr = IPAddress.Parse("127.0.0.1");
        endpoint = new IPEndPoint(addr, 1234);
        mListener = new TcpListener(endpoint);
        mListener.Start();
    }

    public void threadClient()
    {            
        listenthread = new Thread(new ThreadStart(Start));
        listenthread.Start();            
    }


    public override void Start()
    {
        Console.WriteLine("Starting TCP server");
        try
        {                
            while (true)
            {                   
                allDone.Reset();
                mListener.BeginAcceptTcpClient(new AsyncCallback(OnClientConnected), mListener);
                allDone.WaitOne();
            }
        }
        catch (Exception e)
        { 
            Console.WriteLine(e.Message); 
        }
    }

    public void OnClientConnected(IAsyncResult result)
    {
        allDone.Set();
        TcpListener listener = (TcpListener)result.AsyncState;
        Socket handler = listener.EndAcceptSocket(result);
        StateObject state = new StateObject();
        state.workSocket = handler;
        try
        {
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
} 

You're asking for a lot here, Pratul.

I'm afraid I can't answer "How do I build a multi-threaded TCP server app?" here. But I can give you a couple of pointers. Typically, you'd have a listener socket (as you have) and a blocking call that waits for new connections. There should be no need to wait on any synchronization objects in the listener's loop.

When you accept the new connection you get a new socket - the socket assigned to the new connection. I don't see where you're picking up this new socket.

Then start a new thread for that new connection.

See https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(System.Net.Sockets.TcpListener);k(TargetFrameworkMoniker-.NETFramework

Regards,

Adam.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM