Professional Documents
Culture Documents
Introduction
The following are some general information about Berkeley Sockets
2. It is an API.
5. There are higher level tools for programs that span more than one
machine. RPC, DCOM and windows remoting are examples.
Berkeley sockets support two types of communications. These sit on top of the
TCP Internet datagrams.
What are sockets? They represent end points for communications. In the
UNIX world, they are file descriptors. Thus we can use system calls like read
and write to receive and send data.
The following are the major system calls supplied by Berkeley sockets and
UNIX to perform TCP communications. There are other calls that we will use
and calls to support UDP. These will be presented later.
4. read - to get data that was sent on the connection. It returns the number
of bytes read. 0 is returned if the connection broken. (NOTE: WIN32
gives an error return.).
Client:
^ |
|______|
Server:
socket bind listen accept read write close
^ ^ | |
|_______|______|______|
Design idea
getservbyname
Many standard services such as ECHO have predefined port numbers. This
function determines the port number for a service given its name.
#include <netdb.h>
struct servent {
};
NOTE: this function is not thread safe. Why? There is a thread safe version.
How do you think they fixed the problem?
gethostbyname
The gethostbyname function allows you to get the IP address of a computer
given its full name. It has the following prototype:
#include <netdb.h>
struct hostent {
};
NOTE: this function is not thread safe. There is a thread safe version.
socket
This function returns a file descriptor that will eventually become the end of a
pipe between client and server or a point at which we listen for connections.
Note: the location of the include files my differ on various UNIX systems.
#include <sys/socket.h>
domain - the family of addresses. The only one we care about is:
SOCK_STREAM
SOCK_DGRAM
protocol - specifies the protocol. Since there is usually only one choice, we
use 0 for the default.
connect
The connect call establishes a link between to the client that calls it and a server
application. It has the following prototype.
int connect ( int socket, const struct sockaddr *address, size_t address_len );
Notes on example:
The code requires the address/name of server computer and the port
number/service name as command-line arguments. User will be able to enter
the IP address in decimal notation, the domain name (e.g. phobos.ramapo.edu)
or if no entry the default address will be our computer. See /etc/hosts for local
list of hosts. User will be able to enter the name of the service (see
/etc/services), the port number, or if no entry, defaults to the DAYTIME
service.
Lab6.
Time how long it takes to connect to an echo server and to send and receive
20,000 bytes in 100 byte packets. You should pick a far away site. We will
discuss this in class.