IPC - Socket Server -- Can someone help me?

85 views
Skip to first unread message

Arnim Schachtschabel

unread,
Jun 15, 2016, 11:07:20 AM6/15/16
to Haxe

Hello community,

when I found this tutorial about Inter Process Communication via a socket server, I thought "Hey, this is much less complicated than I thought..."

How wrong I was.

The tutorial itself is quite easy to understand. But I want to understand the whole process and to exchange informations that are more complex than just strings.

First of all, what I don't understand is, why does the following why-scope is only "activated" when the connection is established?

while( true )
{
var c : sys.net.Socket = s.accept();
trace( "Client connected..." );
c.write( "hello\n" );
c.write( "your IP is " + c.peer().host.toString() + "\n" );
c.write( "exit" );
c.close();
}
 

The server process traces "Client connected..." only once, after the client process is started.

I see, that the connection is closed after sending the message to the client (c.close() ). But why doesn't the while-scope loop on? Isn't the trace() command supposed to be executed perpetually? The why-condition is always 'true'. And the process of the server.exe dos not stop on its own (that's why it's called a server)...

The reason, why I stumble across this is, that I want to retrieve a whole bunch of informations, the client is sending. I want to do that with the socket.custom field. At this moment the field is null. But the while loop only is executed once.

Maybe that is the reason, for not receiving the information?

.setBlocking() is 'false'.
I have tried out to set  a time-out at '0' or at '10'. It didn't help.

I have done a lot of research finding a better documention of haxe Socket. I only found one tutorial.

I have also tried to figure out the POSIX method of establishing sockets, as it is said to be the blueprint for haxe Sockets.

No approach worked.

Can someone help me?

Kind regards
Arnim

Horsetopus

unread,
Jun 15, 2016, 12:55:05 PM6/15/16
to Haxe
Funny, I was here for asking questions about IPC.
And I might be wrong, but what you are using is not IPC.
IPC are a special kind of socket for communicating between processes. You don't need an host/port or any networking.
Yours is just the common socket, used for client-server communication.

So, this being said here is what appends in your code:

/1 You make your server socket.
This socket is just here to listen to new connections.

2/ you enter the loop.

2.a/ Whenever you call accept() the program freezes until a new client tries to connect.
When the connection is opened, he creates a new socket and proceeds.

2.b/ Trace.

2.c/ Some messages are sent.

2.d/ The connection is closed.

2.e/ We end the loop, and go back to 2.a, therefor the process is blocked again until a new connection appends.


So it does loop. If you refresh/restart your client, it will connect again. Send a message. Close the socket. Loop. Wait.
If you had no while(true), the application would close after the connection is closed ( server or not ).

What else...
1/ I am not sure if blocking also affects the accept blocking, but if you want to try this, make sure the socket you set non blocking is the 's' socket, the one listening for new connections, not the 'c' socket.
2/ If you want to send info about the client, just do s.send( "whatever information" ) on the client, and raw = c.read() on the server.
3/ I prefer to use ServerLoop, that handles most of the work, including the threading, and present you with only what you really need. But I actually can't find a proper sample...
4/ If your goal is to connect a website, socket won't work, you will need websockets. If so have a look at https://bitbucket.org/yar3333/haxe-websocket

Arnim Schachtschabel

unread,
Jun 15, 2016, 1:08:19 PM6/15/16
to Haxe
Hello Horsetopus,

thank you for your reply. It really is worth a lot.

What you say about IPC without host/ port/ network is what I am asking myself for long. But I could not find any documentation for that case.

I would like to make two applications/ (both running locally) communicate with each other. Do you know a tutorial or other material that covers that for HAXE?

Kind regards
Arnim

Horsetopus

unread,
Jun 15, 2016, 1:32:14 PM6/15/16
to Haxe
Well, I didn't say that sockets are bad for this use. They work fine.

But if you are gonna run both applications locally, maybe you can just use in/out streams.

If application A launches application B, then you can easily set the communication between the two like such:


         public function launchProcess():Void {
                
var process:Process;

process = new Process( "neko", [ "otherprocess.n", "argument1", "argument2" ]);
read( process.stdout, onOutput );
read( process.stderr, onError );
}

private function read( input:Input, output:String->Void ) {

var thread = Thread.create( function() {

while ( true )
output( input.readLine() );
} );
}

private function onOutput( v:String ):Void {

trace( v );
}

private function onError( v:String ):Void {

trace( v );
}

So, basically, you launch another process with neko ( .n are neko binary files ) and read the outputs in threads.
On the otherprocess, when you call trace( or Std.println ) it will be redirected to the onOutput function and errors will be redirected to onError.

I have not run the code. But it should give you a proper idea of the mechanics. What remains is trial and error ^^

Arnim Schachtschabel

unread,
Jun 15, 2016, 2:35:13 PM6/15/16
to Haxe

Thanks again. This is exactly the initial spark, I was looking for.

Kind regards
Arnim
Reply all
Reply to author
Forward
0 new messages