Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Select Tcp Client

by NERDVANA (Deacon)
on Oct 13, 2019 at 02:32 UTC ( [id://11107397]=note: print w/replies, xml ) Need Help??


in reply to Select Tcp Client

Tybalt has the right fix, but I think some additional commentary might help.

Socket programming can be bit messy. When you read, sometimes you get a partial message, and sometimes you get more than one message. You code will need to read a chunk of data, then find out how many whole messages you got, then process each of them, then hold onto any leftover fragment to be combined with the next read. This needs tracked per-connection. When you ask perl to read a line with <FILEHANDLE> perl will keep reading until it sees an end-of-line. If you have a blocking file handle, this stalls your whole program. But, if you change to non-blocking file handles, perl will think it reached end-of-file and return the fragment even if it isn't a whole line. The second one is a useful behavior, but sysread() is generally a better approach, since it keeps you closer to the native Unix interface (and you need most of that to write a sockets program correctly).

While doing this is a good learning exercise, and might be sort of a required first step to fully understand sockets, for "getting things done" I recommend an event-driven framework like AnyEvent or IO::Async ecosystem. (Check out AnyEvent:Handle ) These frameworks take care of lots of socket details for you and let you focus on the interesting parts of the program.

Replies are listed 'Best First'.
Re^2: Select Tcp Client
by PerlNewbie94 (Novice) on Oct 14, 2019 at 16:41 UTC
    Hei Nedvana, Thank you for your reply. I was able to solve the issue by fork()

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found