0

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

pats
  • 1,273
  • 2
  • 20
  • 43

1 Answers1

0
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
Pang
  • 9,564
  • 146
  • 81
  • 122
  • I am using SocketAsyncEventArgs, not TcpClient – pats May 18 '15 at 04:28
  • I'm saying there are better ways. You limit your scope of use by what your attempting to do, so the real world value is not there. – Travis Halvorson May 18 '15 at 14:42
  • Here is a tutorial. It does what your attemptinh, but with better form. http://csharp.net-informations.com/communications/csharp-multi-threaded-server-socket.htm – Travis Halvorson May 18 '15 at 14:45
  • I will also advise the book "tcp/ip sockets in c#: a practical guid for programmers" – Travis Halvorson May 18 '15 at 14:47
  • thanks, but I don't get why my scenario is not real. I have a server which needs to respond to clients requests as well as send commands to clients. So server needs to send data to client as well when data is available. This meas server has to send data both directions simultaneously. – pats May 18 '15 at 22:51