Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: Async socket connection

by Corion (Patriarch)
on Feb 09, 2017 at 08:08 UTC ( [id://1181491]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Async socket connection
in thread Async socket connection

For completeness, I'll replicate the drawing here, just in case that other resource goes away:

(port 8000) "SEND" PC1 (port 8010) "RECEIVE" | | v ADD ^ | | (port 8000) "RECEIVE" PC2 (port 8010) "SEND"
(port 8000) "SEND" PC1 (port 8010) "RECEIVE" | | ^ ACK ^ | | (port 8000) "RECEIVE" PC2 (port 8010) "SEND"
(port 8000) "SEND" PC1 (port 8010) "RECEIVE" | | v ^ STAT | | (port 8000) "RECEIVE" PC2 (port 8010) "SEND"

From that diagram resp. my interpretation of it, you don't have "SEND" and "RECEIVE" sockets, but some sockets that are listening for and accepting incoming socket connections, and some sockets that are used to talk to the listening sockets. The listening sockets are listening on port 8000 (PC2) and port 8010 (PC1).

You also use the terms "server" and "client", even though that distinction seems somewhat arbitrary to me because both machines seemm to be able to initiate connections to each other.

I'm not clear on whether your current two programs client.pl and server.pl are running on both machines or whether client.pl only runs on PC1 and server.pl only runs on PC2.

I would conceptually split up the "STAT" part separate from the "ADD" part. You can combine the server parts of both if the general format of an incoming message is the same.

The easiest approach to combine the logic of the listeners and the senders in a single program is to launch for threads, two listener threads and two sender threads, or just two threads if you only need a listener on port 8000 and a sender to port 8010 (PC2) or a listener on port 8010 and a sender to port 8000 (PC1).

The next step would be to look at AnyEvent or IO::Select to manage the writing to sockets and the reading from sockets. AnyEvent gives you callbacks for each socket when it's ready to write more data or when it's ready to read more data.

Personally, I would go with the AnyEvent approach because that avoids race conditions.

Replies are listed 'Best First'.
Re^4: Async socket connection
by ljamison (Sexton) on Feb 09, 2017 at 15:41 UTC

    Hi Corion, thank you for your insight! Would it be possible to ask for a boilerplate of what you mentioned? I've done some basic work with IO::Socket::INET and what I had tried to do was along the lines of creating an INET socket with client and server using PeerAddr/PeerPort & LocalPort but was never able to accomplish the task. I had previously looked over AnyEvent and IO::Select but was unable to figure out where to start with the type of application I'd use them for. Thank you for any assistance!

      I'm not sure I could do better than the documentation that I already linked.

      See the EXAMPLE section in IO::Select and AnyEvent::Socket.

      Maybe you can ask questions by showing actual code you have which tries to use IO::Select or AnyEvent::Socket or threads and tell us how it fails for you to do what you want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1181491]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found