0
votes

I am referring to .net asyn example here https://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx

In this code server responds to client when a message is received. My question

1) How can server send data to a required client, Do I need to find the same SocketAsyncEventArgs initiated during the connection and send it though?

2) How can server and and receive from the same client concurrently? I mean can I send and receive stuff simultaneously.

Thanks

1

1 Answers

0
votes
public void startServer(int port)
{
    port1 = port;//sets the port number
    th = new Thread(thServ);//creates user thread
    UserCounter = new Thread(nextUser1);//creates token thread
    UserCounter.Start();
    th.Start();
}//end start server 

public void thServ()
{
    TcpClient clientSocket = default(TcpClient);//Setup The Client socket type
    try
    {
        //MessageBox.Show(port1.ToString());
        serverSocket = new TcpListener(port1); 
        serverSocket.Start();//start server

    }
    catch { MessageBox.Show("Port is not available"); return; }


    Boolean isthere = true; 
    while (isthere)//start listening for client... would set boolean for back ground worker?
    {
        try
        {
            //countMe++;
            //MessageBox.Show("Server has started");
            //MessageBox.Show(SampleInv.Form4.setUp());//test passing info of user data
            clientSocket = serverSocket.AcceptTcpClient(); // accept client
            //MessageBox.Show("user is here0");
            handleClinet client = new handleClinet();// creat instance of handle client

            client.startClient(clientSocket);//dowork for client

        }
        catch  { isthere = false; MessageBox.Show("clientSocket Failed"); }
    }// end while (shouldn't this be in a try catch)

}//end thServ