Search code examples
c#socketssendasyncsocket

C# AsyncSockets, where to send data without receiveing data before?


I am new to socket programming (especially to asyncsockets).

I used this tutorial http://www.codeproject.com/Articles/22918/How-To-Use-the-SocketAsyncEventArgs-Class to get an AsynSocketserver (my client uses synchronous sockets)

Basically it works. The client can connect to the server, send some data and get it echoed back.

I have this code (in a SocketListener class) to receive the data (without echo):

private void ProcessReceive(SocketAsyncEventArgs e) {
        //check if remote host closed the connection
      if (e.BytesTransferred > 0) {
            if (e.SocketError == SocketError.Success) {
                Token token = e.UserToken as Token;

                token.SetData(e);

                Socket s = token.Connection;

               if (s.Available == 0) {
                    token.ProcessData(e);


               }

                bool IOPending = s.ReceiveAsync(e);
                if (!IOPending) {
                    ProcessReceive(e);
                } 

            //echo back
          /*     if (s.Available == 0) {
                    //set return buffer
                    token.ProcessData(e);
                    if (!s.SendAsync(e)) {
                        this.ProcessSend(e);
                    }
                } else if (!s.ReceiveAsync(e)) {
                    this.ProcessReceive(e);
                }*/
            } else {
                this.ProcessError(e);

            }
        } else {

            this.CloseClientSocket(e);
        }

    }
private void ProcessSend(SocketAsyncEventArgs e) {
        if(e.SocketError == SocketError.Success){
            Token token = e.UserToken as Token;

            if(!token.Connection.ReceiveAsync(e)){
                this.ProcessReceive(e);
            }
        } else {
            this.ProcessError(e);
        }
    }

Now I want that the client can connect to the server (maybe send some data to server, but it should not be necessary that the client sends at first some data to the server, it only needs to connect to the server) and that the client can receive some data from the server.

Problem: I have no idea where or how to use the socketEventArgs.senAsync()-method without receiving data before.

Currently I am using a send()-method in the Token-Class, that creates a new AsyncEventArgs object and uses the (in the token) stored connection to send data:

      public void send() {
        SocketAsyncEventArgs args = new SocketAsyncEventArgs();

        args.UserToken = this;
        args.SetBuffer(sendBuffer, 0, sendBuffer.Length);
        connection.SendAsync(args);

    }

it works, but this seems to be the wrong approach.

So, how do I send data to a client with an open connection without receiving data from the client first?


Solution

  • To me, the only thing "wrong" in there is that you aren't checking the return value of SendAsync (it could have returned synchronously, especially in the case of failure) - and you don't have a Completed event subscription, so when synchronous or asynchronous, you won't be able to detect failure. Also, it is probably possible (and certainly desirable) to re-use the SocketAsyncEventArgs instance, rather than new-ing one for every send.

    But fundamentally: fine. You can send at any point, really.