sinclairzx81 / reactor

Asynchronous Event Driven IO for .NET
MIT License
44 stars 13 forks source link

tcp sockets #3

Open jamesaxl opened 8 years ago

jamesaxl commented 8 years ago

i wonder i can not get any message from the server side, have you ever had a problem with client?

sinclairzx81 commented 8 years ago

Hi there,

If you are working from the master branch, you will need to wait for the OnConnect event to fire before attaching the OnData OnEnd events. I have occasionally forgotten to do this myself which is liable to cause you some problems, the following is the recommended approach for connecting to and reading from a remote TCP endpoint.

Reactor.Loop.Start();
var socket = Reactor.Tcp.Socket.Create("towel.blinkenlights.nl", 23);
// (important) wait for connect
socket.OnConnect += () => { 
    // attach events and stream
    socket.OnData += data => Console.WriteLine(data.ToString("utf8"));
    socket.OnEnd  += ()   => Console.WriteLine("end!");
};
socket.OnError += err => Console.WriteLine(err);

note: if you are on the next branch, the following is achieves the same result, but waiting for the Connect is optional.

Reactor.Loop.Start();
var socket = Reactor.Tcp.Socket.Create("towel.blinkenlights.nl", 23);
socket.OnConnect(() => { // optional
    socket.OnData(data => Console.WriteLine(data));
    socket.OnEnd(()    => Console.WriteLine("ended"));
});
socket.OnError(err => Console.WriteLine(err));

Outside of forgetting to wait for the OnConnect event, I haven't run into any issues connecting to remote endpoints and streaming data over a reactor TCP socket. Perhaps try telnet into the remote server and see if you can connect that way. Note, if you attach aonerror event on the reactor socket, it will notify you if the host is unreachable (or other error) so give that a try also.

Hope that helps!!

Cheers

jamesaxl commented 8 years ago

thanks a lot for your help, i can build my chat client for Windows machines without any complicated ways :+1:

jamesaxl commented 8 years ago

have you ever tried Tls with mono?