I am teaching myself perl and am writing a socket application to get experience.
I am using Eclipse with the EPIC plugin to run the code in debug mode. I think that sometimes the script is not releasing the port if I terminate it in debug mode as I am occasionally getting the message: -
"bind: Address already in use"
When this happens I have to restart eclipse to clear the port.
I would be grateful if anyone could tell me how to free the port / socket and also if the code example I have built the script around could be strengthened in some way to ensure the socket is freed when the process is terminated.
Code:
##-----------------------------------------------
## create a socket, make it reusable
##-----------------------------------------------
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!$LF";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!$LF";
##-----------------------------------------------
## Grab the port
##-----------------------------------------------
my $paddr = sockaddr_in($lport, INADDR_ANY);
##-----------------------------------------------
## bind to the port and listen
##-----------------------------------------------
bind(SERVER, $paddr) or die "bind: $!$LF";
listen(SERVER, SOMAXCONN) or die "listen: $!$LF";
I don't seem to have the problem if I run it in debug mode and just set it to run, I still keep the option to terminate the process that way.
No I haven't tried IO::Socket::INET, the use statement I have is: -
Code:
use Socket qw(:DEFAULT :crlf);
I don't really understand these use statements yet, other than a generic understanding that I am including some libraries.
If you are recommending one library / module over another I would appreciate a heads up on why it is better to do so.
Thanks for the feed back.
---------- Post updated at 05:15 PM ---------- Previous update was at 10:51 AM ----------
Thanks for the heads up on the Socket::INET, it seems more flexible and also allows me to set a timeout.
I have discovered how to set the parameters using the => syntax
Code:
$socket = IO::Socket::INET->new
(
LocalPort => $lport,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 5,
Timeout => 60,
proto => $proto
) or die $msg;
With the old code I could do this: -
Code:
##------------------------------------------------
## Get the name and ip of the client
##------------------------------------------------
($client_port, $client_ip) = sockaddr_in($client_addr);
$client_ipnum = inet_ntoa($client_ip);
$client_host = gethostbyaddr($client_ip, AF_INET);
To discover what client was connecting.
Can anyone tell me how I do this using the new IO::Socket::INET?
Hello community,
I'm programming a simple code using socket connection in perl:
$sock = new IO::Socket::INET(
PeerAddr => '192.168.10.7',
PeerPort => 8000,
Proto => 'tcp');
$sock or die "no socket :$!";Then sending data using a loop:
... (1 Reply)
Hi All
I am getting an error when using the below code
Receiver
use IO::Socket;
$sock = new IO::Socket::INET (LocalHost => 'goldengate',
LocalPort => 1200,
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "Socket could not be created. Reason: $!" unless $sock;
while ($new_sock =... (5 Replies)
I need clarification on whether it is okay to set socket options on a listening socket
simultaneously when it is being used in an accept() call?
Following is the scenario:-
-- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct...
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include... (3 Replies)
Hi there, Really quick question that I have been unable to find an answer for on the web is simply, can use the newer IO::Socket module on my sever process and use the older perl built-in "socket" module to connect to the IO:socket?
or do i need to have the same module (be it socket or... (1 Reply)
Hey guys, im trying to write a program that'll create a report then email the report... my problem is when it comes to the socket trying to send the second command after EHLO smtp,*.*
When the first command is sent its working fine... but when the program tries to send the second command it... (6 Replies)
Hello there:
What is default value of listen if I, avoid init it on constructor. I need maximum num for Listen. I want to write a daemon thus i need unlimited listen.
my $sock = IO::Socket::INET->new( Listen => 20,
LocalPort => $port,
Reuse => 1)
Thanks in advance. (3 Replies)
Hi,
I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime.
To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Hi all,
I am facing some problem in Socket programming(C++ & Solaris 5.0).I am using socket as
basic connecting point & MEP(Mesagae Exchange Protocol) as high level protocol to send &
receive Message & ACK, between two different system.
MEP is a protocol through which you can send MSG... (0 Replies)
hi
i want to write simple socket program which will listen on socket . here is the code
## read msg on socket
#! /usr/bin/perl
use IO::Socket::INET;
my $MySocket= IO::Socket::INET->new(LocalPort=>1234,
Proto=>'udp') ;
while ()... (2 Replies)