Sie sind auf Seite 1von 11

Origine IT Workshop

Writing your web server


Origine IT

Objective
Learn about socket programming
Group Activities
Write a socket server
Write a socket client

Establishing a TCP/IP Connection


3-way Handshake : SYNC, SYNC-ACK, ACK
I want to talk to
you (SYNC)

Sync-ack

sync

Got it. You can talk


to me (SYNC-ACK)

Really ? Lets talk


(ACK)
Established

ack

Closing a TCP/IP Connection


FIN
We are FINished

ACK
Got it. mmm...
I am waiting .... (FIN_WAIT2)
FIN
Ok lor. Finish lor ...

ACK
I have gotten over
I need time ... Before it is truly over ...

Closing a connection

Socket Server

Create socket
Bind socket (with IP address and port)
Socket listen (to connection)
Socket Accept connection
Socket write (or send)
Socket read (or receive)
Socket close

Socket Client

Create socket
Socket connect (to the host address and port)
Socket read (if expecting messages from host)
Socket write (message to host)
Socket close

Socket Server in Perl


#tcpserver.pl
use IO::Socket;
$| = 1;
$socket = new IO::Socket::INET (
LocalHost => '127.0.0.1',
LocalPort => '5000',
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "Coudn't open socket" unless $socket;
print "\nTCPServer Waiting for client on port 5000";
while(1)
{
$client_socket = "";
$client_socket = $socket->accept();
$peer_address = $client_socket->peerhost();
$peer_port = $client_socket->peerport();
print "\n I got a connection from ( $peer_address ,
$peer_port ) ";

while (1) {
print "\n SEND( TYPE q or Q to Quit):";
$send_data = <STDIN>;
chop($send_data);
if ($send_data eq 'q' or $send_data eq 'Q) {
$client_socket->send ($send_data);
close $client_socket;
last;
}
else {
$client_socket->send($send_data);
}
$client_socket->recv($recieved_data,1024);
if ( $recieved_data eq 'q' or $recieved_data
eq 'Q) {
close $client_socket;
last;
}
else {
print "\n RECIEVED: $recieved_data";
}
}

Socket Client in Perl


#tcpclient.pl
use IO::Socket;

else
{
print "RECIEVED: $recv_data";
print "\nSEND( TYPE q or Q to Quit):";

$socket = new IO::Socket::INET (


PeerAddr => '127.0.0.1',
PeerPort => 5000,
Proto => 'tcp',
)
or die "Couldn't connect to Server\n";

while (1)
{
$socket->recv($recv_data,1024);
if ($recv_data eq 'q' or $recv_data eq 'Q')
{
close $socket;
last;
}

$send_data = <STDIN>;
chop($send_data);

if ($send_data ne 'q' and $send_data ne 'Q')


{
$socket->send($send_data);
}
else
{
$socket->send($send_data);
close $socket;
last;
}

}
}

Simple Requirement
Server
Once received a new connection, send greeting
message to client
Wait for message from client
Upon receive a new message from client
Check if own IP address exist in the message
If not found
connect to another server
Wait for server message
append the original message from client with own IP address,
separated with semi-colon, and send to server
Wait for reply message from server
Once receive the message, reply to client

If found
Reply with anything you like

Simple Exercise
Host 1

Host 2

Host 3

Connect

Listening

Listening

Listening

Connect

Connect

Host N
Listening
Connect

Das könnte Ihnen auch gefallen