reactor icon indicating copy to clipboard operation
reactor copied to clipboard

tcp sockets

Open jamesaxl opened this issue 9 years ago • 3 comments

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

jamesaxl avatar Dec 08 '15 00:12 jamesaxl

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

sinclairzx81 avatar Dec 08 '15 01:12 sinclairzx81

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

jamesaxl avatar Dec 08 '15 14:12 jamesaxl

have you ever tried Tls with mono?

jamesaxl avatar Dec 08 '15 18:12 jamesaxl