sys.net.Socket & lib easyNet

Hi. I ran into a simple problem :frowning: I dont understand just

  1. how to check if socket is connected
  2. how to check if input data is available for read

For example in C# class Socket have a property “.IsConnected” and socket’s stream have a property “.DataAvailable” or “.Length”. But I can’t find something like that in Haxe :frowning:

Mybe somebody knowns a simply solution for these (‘standard’, as it seems to me) features?
Thanks.

Probably, there is no simply and beauty solution :frowning: Only play with exceptions, for example:

class Client {
	static function main() {
		var s = new sys.net.Socket();
		s.setBlocking(false);
		s.connect(new sys.net.Host("localhost"), 5000);
		
		trace("client connected...");
		
		while (s != null) {
			try
			{
				var r = s.input.readByte();
				trace('r is $r');
				
			}
			catch (e:Dynamic) {
				trace(e); // Blocked - no data, Eof - connection was terminated
			}
			
			Sys.sleep(1);
		}
	}
}

Hello there.

You might want to wrap

s.connect(new sys.net.Host(“localhost”), 5000);

in a try/catch block as well, because according to DOCS it

Throw an exception in case we couldn’t successfully connect.

yep, thanks for the notice!

Eventually I made library EasyNet, who provides these features and have a really easy inferface for usage.

2 Likes