0% found this document useful (0 votes)
17 views

Tutor 2

The document discusses Berkeley sockets and how they can be used to create network connections and transmit data between processes. It covers topics like socket domains, types, and protocols as well as functions for addressing, packing addresses, and includes examples of simple client programs.

Uploaded by

Nitish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Tutor 2

The document discusses Berkeley sockets and how they can be used to create network connections and transmit data between processes. It covers topics like socket domains, types, and protocols as well as functions for addressing, packing addresses, and includes examples of simple client programs.

Uploaded by

Nitish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming Tutorial 2

Computer Science 367

Dr. Jianna J. Zhang


© copyright 2004
Department of Computer Science, Western Washington University

Berkeley Sockets

-- Berkeley sockets are part of application programming interface


-- specifies the data structures and function calls that interact with
the operating system’s network subsystem
-- generic enough to support different types of network
-- endpoint for communications between processes

1
Create a Socket

To create a socket, we need at least 3 information:


1. the socket’s domain (protocols and addressing)
a. AF_INET (the Internet protocols)
b. AF_UNIX (networking within a single host)
of AF_LOCAL because it can be used non-Unixly
2. the socket’s type (communication type)
a. SOCK_STREAM (continuous stream)
b. SOCK_DGRAM (in packages)
3. the socket’s protocol (must use getprotobyname( ) function)
a. tcp (transmission control protocol for stram sockets)
b. udp (user datagram protocol for datagram sockets)
c. icmp (internet control message protocol)
d. raw (creates IP packets manually)
Allowed combinations in the INET and UNIX domains:
1. AF_INET, SOCK_STREAM, and tcp
2. AF_INET, SOCK_DGRAM, and udp
3. AF_UNIX, SOCK_STREAM, and PF_UNSPEC (unspecified)
4. AF_UNIX, SOCK_DGRAM, and PF_UNSPEC

Datagram Sockets

Hello!

Hello to you.

lost
client server

Hello!

Hello to you.

2
Stream Sockets

Hello!

client server

Hello to you.

Datagram versus stream sockets:


1. reliability
2. time
3. one to many transmission

Socket Addressing

1. Unix: path on the host file system


example: /usr/bin/perl
2. Internet: IP address, the port, and the protocol
example: IP = 140.160.140.101
port = 13 (daytime server program)
protocol = 6 (TCP/IP protocol)
Notes:
1. port number 0 to 1023 reserved
examples: 80 http, 25 SMTP, 53 DNS

3
Functions by Perl Socket Module

1. $packed_address = inet_aton ($dotted_quad)


-- given an IP address, the function “inet_aton” packs it into binary form

2. $dotted_quad = inet_ntoa ($packed_address)


-- given an IP address, the function “inet_aton” packs it into binary form

3. $socket_addr = sockaddr_in ($port, $address)


-- the function “sockaddr_in” packs ($port, $address) into socket address

4. ($port, $address) = sockaddr_in ($socket_addr )


-- the function “sockaddr_in” unpacks socket address

5. $socket_addr = pack_sockaddr_in ($port, $address)


($port, $address) = unpack_sockaddr_in ($socket_addr)

A Simple Network Client: Header Example

#!/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

# load Socket module to import functions for socket programming


1. use Socket;
# default address if not provided on the command line
2. use constant DEFAULT_ADDR => '140.160.140.101';
# daytime service port number is 13
3. use constant PORT => 13;
# TCP/IP protoco number is 6, needed to constract the socket
4. use constant IPPROTO_TCP => 6;
# construct the socket using IP address and the port number of the host
5. my $address = shift || DEFAULT_ADDR;
6. my $packed_addr = inet_aton($address); # IP to binary
7. my $destination = sockaddr_in(PORT,$packed_addr);
# create the socket
8. socket(SOCK,PF_INET,SOCK_STREAM,IPPROTO_TCP) or die “socket: $!”;
# connect to the remote host
9. connect(SOCK,$destination) or die "Can't connect: $!";
# read data from the host and print it
10. print <SOCK>;

The Daytime Client Program


use strict;
use Socket;

use constant DEFAULT_ADDR => '140.160.140.101';


use constant PORT => 13;
use constant IPPROTO_TCP => 6;

my $address = shift || DEFAULT_ADDR;


my $packed_addr = inet_aton($address);
my $destination = sockaddr_in(PORT,$packed_addr);

socket(SOCK,PF_INET,SOCK_STREAM,IPPROTO_TCP) or die "Can't make


socket: $!";
connect(SOCK,$destination) or die "Can't connect: $!";

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.

You might also like