forked from UIPEthernet/UIPEthernet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,34 @@ | ||
#!/usr/bin/perl | ||
#tcpclient.pl | ||
|
||
use IO::Socket::INET; | ||
|
||
# flush after every write | ||
$| = 1; | ||
|
||
my ($socket,$client_socket); | ||
|
||
# creating object interface of IO::Socket::INET modules which internally creates | ||
# socket, binds and connects to the TCP server running on the specific port. | ||
$socket = new IO::Socket::INET ( | ||
PeerHost => '192.168.0.6', | ||
PeerPort => '1000', | ||
Proto => 'tcp', | ||
) or die "ERROR in Socket Creation : $!\n"; | ||
|
||
print "TCP Connection Success.\n"; | ||
|
||
# write on the socket to server. | ||
$data = "DATA from Client"; | ||
print $socket "$data\n"; | ||
# we can also send the data through IO::Socket::INET module, | ||
# $socket->send($data); | ||
|
||
# read the socket data sent by server. | ||
$data = <$socket>; | ||
# we can also read from socket through recv() in IO::Socket::INET | ||
# $socket->recv($data,1024); | ||
print "Received from Server : $data\n"; | ||
|
||
sleep (10); | ||
$socket->close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/perl | ||
#tcpserver.pl | ||
|
||
use IO::Socket::INET; | ||
|
||
# flush after every write | ||
$| = 1; | ||
|
||
my ($socket,$client_socket); | ||
my ($peeraddress,$peerport); | ||
|
||
# creating object interface of IO::Socket::INET modules which internally does | ||
# socket creation, binding and listening at the specified port address. | ||
$socket = new IO::Socket::INET ( | ||
LocalHost => '192.168.0.1', | ||
LocalPort => '5000', | ||
Proto => 'tcp', | ||
Listen => 5, | ||
Reuse => 1 | ||
) or die "ERROR in Socket Creation : $!\n"; | ||
|
||
print "SERVER Waiting for client connection on port 5000\n"; | ||
|
||
while(1) | ||
{ | ||
# waiting for new client connection. | ||
$client_socket = $socket->accept(); | ||
|
||
# get the host and port number of newly connected client. | ||
$peer_address = $client_socket->peerhost(); | ||
$peer_port = $client_socket->peerport(); | ||
|
||
print "Accepted New Client Connection From : $peer_address, $peer_port\n "; | ||
|
||
# read operation on the newly accepted client | ||
$data = <$client_socket>; | ||
# we can also read from socket through recv() in IO::Socket::INET | ||
# $client_socket->recv($data,1024); | ||
print "Received from Client : $data\n"; | ||
|
||
# write operation on the newly accepted client. | ||
$data = "DATA from Server"; | ||
print $client_socket "$data\n"; | ||
# we can also send the data through IO::Socket::INET module, | ||
# $client_socket->send($data); | ||
|
||
sleep(1); | ||
|
||
$client_socket->close(); | ||
} | ||
|
||
$socket->close(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/perl | ||
#udpclient.pl | ||
|
||
use IO::Socket::INET; | ||
|
||
# flush after every write | ||
$| = 1; | ||
|
||
my ($socket,$data); | ||
|
||
# We call IO::Socket::INET->new() to create the UDP Socket | ||
# and bind with the PeerAddr. | ||
$socket = new IO::Socket::INET ( | ||
PeerAddr => '192.168.0.6:5000', | ||
Proto => 'udp' | ||
) or die "ERROR in Socket Creation : $!\n"; | ||
#send operation | ||
$data = "data from client"; | ||
$socket->send($data); | ||
|
||
#read operation | ||
$data = <$socket>; | ||
print "Data received from socket : $data\n "; | ||
|
||
sleep(10); | ||
$socket->close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/perl | ||
#udpserver.pl | ||
|
||
use IO::Socket::INET; | ||
|
||
# flush after every write | ||
$| = 1; | ||
|
||
my ($socket,$received_data); | ||
my ($peeraddress,$peerport); | ||
|
||
# we call IO::Socket::INET->new() to create the UDP Socket and bound | ||
# to specific port number mentioned in LocalPort and there is no need to provide | ||
# LocalAddr explicitly as in TCPServer. | ||
$socket = new IO::Socket::INET ( | ||
LocalPort => '5000', | ||
Proto => 'udp', | ||
) or die "ERROR in Socket Creation : $!\n"; | ||
|
||
while(1) | ||
{ | ||
# read operation on the socket | ||
$socket->recv($recieved_data,1024); | ||
|
||
#get the peerhost and peerport at which the recent data received. | ||
$peer_address = $socket->peerhost(); | ||
$peer_port = $socket->peerport(); | ||
print "($peer_address , $peer_port) said : $recieved_data\n"; | ||
|
||
#send the data to the client at which the read/write operations done recently. | ||
$data = "data from server\n"; | ||
|
||
$socket->send($data); | ||
|
||
print $socket "$data"; | ||
|
||
} | ||
|
||
$socket->close(); | ||
|