Title: TCP/IP Protocol Stack
1TCP/IP Protocol Stack
TCP Establish connection Maintain connection during the communication Release connection Reliable (Acknowledged, in order)
UDP No need to setup a link Independent packets Not reliable (no acknowledgement)
Application
Sockets
(Gate to network)
TCP
UDP
IP
Device Drivers
2Programming with Sockets
- Sockets are Berkeley software distribution UNIX
interface to network protocols - Endpoint of communication, has a type and one
associated process - Uses file system model (open-close-read-write)
- Internet vs. Unix domain
3Socket Creation
( 0 )
s socket( domain , type , protocol )
SOCKET DESCRIPTOR ( int type )
AF_INET
SOCK_STREAM
AF_UNIX
SOCK_DGRAM
include ltsys/socket.hgt include
ltnetinet/in.hgt include ltnetdb.hgt include
ltsys/types.hgt
4Connection Establishment
connect(socket, server address, size of address
structure)
s socket pointer returned from socket command
Size of the data structure (struct sockaddr_in)
Server information, the data structure that keeps
this information is struct sockaddr_in short
int sin_family //address type(AF_INET) struct
in_addr sin_addr //internet address of
server int sin_port //port number of
server struct sockaddr_in server
5How to get server address?
- You may either give the server name (or address)
from the command line or paste it directly into
the code. - There are 2 ways to get the servers internet
address - If the name of the server is given, you can use
- struct hostent gethostbyname(char name)
- hostent data type has two fields you will use
- struct in_addr h_addr internet address of
server - int h_length length of the address
- The last step will be to copy the internet
address into our servers address field. - Ex.
- struct hostent hp
- hp gethostbyname(sun114-16.cise.ufl.edu)
- memcpy( (char ) server.sin_addr, (char )
hp-gth_addr, hp-gth_length)
6How to get server address?
The second way is to convert the Internet host
address from the standard numbers-and-dots
notation into binary data. (ex. 128.56.560.67 ,
this is not the correct ip address of our server,
please use your knowledge to find out the ip
address of our server, the servers host name is
sun114-16.cise.ufl.edu) The function we will use
is int inet_aton( const char name, struct
in_addr addr) This function converts the
Internet host address name from the standard
numbers-and-dots notation into binary data and
stores it in the struct in_addr that addr points
to. inet_aton returns nonzero if the address is
valid, zero if not. Ex. Inet_aton(128.56.560.67
, server.sin_addr)
7Putting it all together
int s struct sockaddr_in server struct hostent
hp s socket( AF_INET, SOCK_STREAM, 0
) server.sin_family AF_INET hp
gethostbyname( argv1 ) memcpy( (char )
server.sin_addr, (char ) hp-gth_addr,
hp-gth_length) server.sin_port htons(
atoi(argv2) ) connect( s , (struct sockaddr
) server , sizeof(server) )
8Data Transfer
int nbytes write( int socket , char data ,
int length ) It is similar to writing to a file.
The first parameter is a pointer to socket, the
second parameter is the data you wish to send,
and the last parameter is the size of
data. Reading from the socket is done using int
nbytes read( int socket , char data , int
max_length ) nbytes gives you the number of
bytes read from the socket. The difference from
write is that since we do not know the size of
the stream to be received in advanced, we set the
third parameter to the maximum length of stream
that our data buffer may hold. In case of error
both read and write commands return -1.
9Data Transfer Example
define MAXMSG 256 char bufferMAXMSG int
nbytes buffer this is a test. nbytes
write(s, buffer, strlen(buffer)1) nbytes
read(s, buffer, MAXMSG)
10Closing Connection
close( int socket )
SOCKET DESCRIPTOR
A SOCK_STREAM socket can be discarded by a
close() system call. Ex. close( s )
11Client-Server Architecture
request
Process request
client
response
Host sun114-16.cise.ufl.edu Port 7000
12The Algorithm
input login
13Additional Notes
You will use a data format in your application to
send to our server. The data format is a data
structure with 3 fields. Please be sure to fill
all the fields correctly. After you create the
PDU, you have to cast it to char pointer in the
write command. Ex. struct PDU message //
message is your data structure. You have to set
// message.username, message.type, and if
necessary // message.payload fields. //now you
are ready to send it nbytes write( s, (char
)message, sizeof(message) ) What you will read
from our server is a string (char ). So, you do
not have to do casting. Once you read it to a
buffer, just print the string to the standard
output. You can refer to read example in slide 9.
14Additional Notes
We recommend you to use standard C in a UNIX
environment. To compile your program you should
use the following line gcc client.c lsocket
lnsl o client