TCP client Asynchronous socket callback?

If you pass a state object which includes a reference to your socket, You'll have access to the socket itself public class SocketState { public SocketState(Socket s) { this. _socket = s; } private Socket _socket; public Socket Socket { get{return _socket;} } } void SomeFunction() { //do some stuff in your code SocketState stateObject = new SocketState(mySocket); mySocket. BeginReceive(buffer, offset, size, flags, CallBack, stateObject); //do some other stuff } public void CallBack(IAsyncResult result) { SocketState state = (SocketState)result.

AsyncState; state.Socket. EndReceive(result); //do stuff with your socket. If(state.Socket.

Available) mySocket. BeginReceive(buffer, offset, size, flags, CallBack, state); }.

If you pass a state object which includes a reference to your socket, You'll have access to the socket itself. Public class SocketState { public SocketState(Socket s) { this. _socket = s; } private Socket _socket; public Socket Socket { get{return _socket;} } } void SomeFunction() { //do some stuff in your code SocketState stateObject = new SocketState(mySocket); mySocket.

BeginReceive(buffer, offset, size, flags, CallBack, stateObject); //do some other stuff } public void CallBack(IAsyncResult result) { SocketState state = (SocketState)result. AsyncState; state.Socket. EndReceive(result); //do stuff with your socket.

If(state.Socket. Available) mySocket. BeginReceive(buffer, offset, size, flags, CallBack, state); }.

Your question is not really clear. By the tone of your question, it seems like you don't want to do any extra work. You can't do async without some extra work.

The best approach is to use the Asynchronous API from Microsoft, using BeginReceive/EndReceive. These will call your callbacks when the socket is closed. However, you cannot easily use the IO Stream support in .

NET by doing this, so there is some extra work involved. If you want more control, you have to do more work. That's all there is to it.

Mystere Man,when I call BeginDisconnect it disconnects me,while I'm expecting the program to do the callback when the socket gets disconnect by server. I feel fine with BeginRecv,EndRecv ,but It doesn't notify me if the connection is still alive. That's why im so angry.

Could you provide a solution – John Mar 26 '09 at 17:12 It doesn't have to notify you if the connection is alive, it need only notify you when the connection is dead. EndReceive does this by returning an exception, or 0 bytes. – Mystere Man Mar 26 '09 at 17:14.

You could also write a simple threaded version to monitor the socket and give you the events and call the callbacks. Server type code Simple Socket code.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions