Tutor 2
Tutor 2
Berkeley Sockets
1
Create a Socket
Datagram Sockets
Hello!
Hello to you.
lost
client server
Hello!
Hello to you.
2
Stream Sockets
Hello!
client server
Hello to you.
Socket Addressing
3
Functions by Perl Socket Module
#!/usr/bin/perl
######################################################
# Program: daytime_cli.pl #
# Description: A Daytime Client which construct a socket #
# Usage: daytime_cli.pl [IP] #
# Student: your name (W-ID) #
# References: #
# 1. Lincoln Stein, Network Programming with Perl, Addison #
# Wesley, Boston, 2001, 754 pages. #
# 2. Andrew Tanenbaum, Computer Networks, 4th Edition #
# Printice Hall PTR, Upper Saddle River, 2003, 891 pages. #
######################################################
4
A Simple Client Program: Explained
print <SOCK>;
5
A Simple Echo Client Program
1. use strict; # turn on strict syntax checking
2. use Socket; # load the socket
3. use IO::Handle; # load the IO:: Handle to use autoflush( )
4. my ($bytes_out, $bytes_in) = (0,0); # global variables to record bytes sent and received
5. my $host = shift || 'localhost'; # host from command line or localhost
6. my $port = shift || getservbyname('echo','tcp'); # look up echo server port number
7. my $protocol = getprotobyname('tcp'); # look up TCP protocol number
8. $host = inet_aton($host) or die "$host: unknown host"; # host address to IP
# create socket handle called SOCK, pass Internet address family, TCP socket
9. socket (SOCK, AF_INET, SOCK_STREAM, $protocol) or die “socket( ) failed: $!”;
10. my $dest_addr = sockaddr_in ($port,$host); # create packed destination address
11. connect (SOCK,$dest_addr) or die "connect() failed: $!"; # connect to destination
12. SOCK->autoflush(1); # output immediately
13. while (my $msg_out = <>) { # get input from SDTIN until
14. print SOCK $msg_out; # write to SOCK immediately sent to host
15. my $msg_in = <SOCK>; # read from Echo server though SOCK handle
16. print $msg_in;
17. $bytes_out += length($msg_out); # counting bytes out and in
18. $bytes_in += length($msg_in); }
19. close SOCK; # disconnect
20. print STDERR "bytes_sent = $bytes_out, bytes_received = $bytes_in\n";
References
1. Lincoln D. Stein, “Network Programming with Perl”, Addison Wesley, Toronto, 2001, 754
pages.
2. Douglas Comer, "Computer Networks and Internets", 3rd ed., Prentice Hall, Upper Saddle River,
NJ., 2001, 683 pages (The textbook).
3. William J. Beyda, “Data Communications” 3rd ed., Prentice Hall, Upper Saddle River, NJ., 2000,
330 pages.
4. Andrew Tanenbaum, ”Computer Networks”, Prentice Hall, ISBN 0-13-349945-6, 1996, 813
pages (A classical text book that is used for many years by many professors).
5. Tech Encyclopedia, by CMP Media Inc., “http://www.globetechnology.com/site
/tech_encyclopedia.html/”, (The most excellent resource for computer science terminology you
can find on the Internet).
6. John Davidson, "An Introduction to TCP/IP”, Springer-Verlag, ISBN 0-387-96651-X, 1988, 100
pages (It is the easiest read on the subject you will ever find).
7. Douglas E. Comer, "Internetworking with TCP/IP," Volumes I, II and III, Prentice-Hall, ISBN
0-13-468505-9, 1991, (regarded as the Bible for TCP/IP).
8. James F. Kurose and Keith W. Ross, “Computer Networking: A Top-Down Approach Featuring
the Internet”, Addison Wesley Longman, Inc., ISBN 0-201-47711-4, 1999, (presents a new
perspective to the study of computer networking concepts).
9. Halliday, Resnick, and Walker, “Fundamentals of Physics”, Sixth Edition, John Wiley & Sons,
Inc., 2001.