INF1060: Introduction to Operating Systems and Data Communication
Data Communication:
Introduction to Berkeley Sockets
Big Picture
Machine Machine 1
process A
Machine 2
process B
network
INF1060 introduction to operating systems and data communication
2005 Kjell ge Bringsrud & Pl Halvorsen
Goal
Introduce socket API We will write two programs
A client and a server
Each will run on one machine
the server will run on kaksi.ifi.uio.no (129.240.65.193)
They will work as follows
The The The The client sends the text Hello world! to the server server will write the received text on the screen server sends the received text back to the client and quits client writes the received text onto the screen and quits
INF1060 introduction to operating systems and data communication
2005 Kjell ge Bringsrud & Pl Halvorsen
What we want
Machine Machine 1
client kaksi.ifi.uio.no server
Hello world!
Hello world!
network
Hello world!
Hello world!
INF1060 introduction to operating systems and data communication
2005 Kjell ge Bringsrud & Pl Halvorsen
What we want
Client
<necessary includes> int main() { char buf[13]; <Declare some more data structures> <Create a socket called sock> <Identify the server that you want to contact> <Connect to the server> /* Send data */ write(sock, Hello world!, 12); /* Read data from the socket */ read(sock, buf, 12); /* Add a string termination sign, and write to the screen. */ buf[12] = \0; printf(%s\n, buf); <Closing code> } }
Server
<necessary includes> int main() { char buf[13]; <declare some more data structures> <create a socket called request-sock> <Define how the client can connect> <Wait for a connection, and create a new socket sock for that connection> <Identify the server that you want to contact> /* read data from the sock and write it to the screen */ read(sock, buf, 12); buf[12] = \0; printf(%s\n, buf ); /* send data back over the connection */ write(sock, buf, 12); <Closing code>
INF1060 introduction to operating systems and data communication
2005 Kjell ge Bringsrud & Pl Halvorsen
Read & Write
Same functions used for files etc.
The call read(sock, buffer, n);
Reads n characters From socket sock Stores them in the character array buffer
The call write(sock, buffer, n);
Writes n characters From character array buffer To the socket sock
INF1060 introduction to operating systems and data communication
2005 Kjell ge Bringsrud & Pl Halvorsen
Alternatives to Read & Write
The call recv(sock, buffer, n, flags);
Reads n characters From socket sock Stores them in the character array buffer Flags, normally just 0, but e.g., MSG_DONTWAIT
The call send(sock, buffer, n, flags);
Writes n characters From character array buffer To the socket sock Flags
2005 Kjell ge Bringsrud & Pl Halvorsen
INF1060 introduction to operating systems and data communication
Creation of a connection
One side must be the active one
take the initiative in creating the connection this side is called the client
The other side must be passive
it is prepared for accepting connections waits for someone else to take initiative for creating a connection this side is called the server
This use of the words client and server is not entirely consistent with everyday use, but for programming this is conventional
INF1060 introduction to operating systems and data communication 2005 Kjell ge Bringsrud & Pl Halvorsen
Special for the server side
In case of TCP
one socket on the server side is dedicated to waiting for a connection for each client that takes the initiative, a separate socket on the server side is created this is useful for all servers that must be able to serve several clients concurrently (web servers, mail servers)
INF1060 introduction to operating systems and data communication 2005 Kjell ge Bringsrud & Pl Halvorsen
To do slightly more details
Client
<Necessary includes> int main() { char buf[13]; <Declare some more data structures> <Create a socket called sock> <Identify the server that you want to contact> <Connect to the server> /* Send data */ write(sock, Hello world!, 12); /* Read data from the socket */ read(sock, buf, 12); /* Add a string termination sign, and write to the screen. */ buf[12] = \0; printf(%s\n, buf); <Closing code> } }
Server
<Necessary includes> int main() { char buf[13]; <Declare some more data structures> <Create a socket called request-sock> <Define how the client can connect> <Wait for a connection, and create a new socket sock for that connection> <Identify the server that you want to contact> /* read data from the sock and write it to the screen */ read(sock, buf, 12); buf[12] = \0; printf(%s\n, buf ); /* send data back over the connection */ write(sock, buf, 12); <Closing code>
INF1060 introduction to operating systems and data communication
2005 Kjell ge Bringsrud & Pl Halvorsen
<Necessary includes>
#include #include #include #include #include <netinet/in.h> <sys/socket.h> <netdb.h> <stdio.h> <string.h>
prototypes & defines (htons, etc.) sockaddr_in prototypes (send, connect, etc.)
defines prototypes (gethostbyame, etc.) prototypes (printf, etc.) prototypes (bzero, etc.)
These five files are needed by both client and server They include definitions and declarations as described on the following sides Some systems will have the same declarations in different files these should work at IFI
(see /usr/include on Linux & Solaris)
INF1060 introduction to operating systems and data communication 2005 Kjell ge Bringsrud & Pl Halvorsen
<Create a socket>
Client
/* declarations */ int sock; /* creation of the socket */ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Server
/* declarations */ int request_sock; /* creation of the socket */ request_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Call to the function socket() creates a transport control block (hidden in kernel), and returns a reference to it (integer used as index)
sock user kernel
control block control block control block
INF1060 introduction to operating systems and data communication
2005 Kjell ge Bringsrud & Pl Halvorsen
More about the socket call
sock = socket(int domain, int type, int protocol)
PF_INET, SOCK_STREAM and IPPROTO_TCP are constants that are defined in the included files
<bits/socket.h> which is included by <sys/socket.h> <netinet/in.h>
The use of the constants that we used on the previous slides (and above) creates a TCP/IP socket Many other possibilities exist
Domain: PF_UNIX, PF_INET, PF_INET6, Type: SOCK_STREAM, SOCK_DGRAM, Protocol: IPPROTO_TCP, IPPROTO_UDP,
INF1060 introduction to operating systems and data communication 2005 Kjell ge Bringsrud & Pl Halvorsen
Thank You
You can join me at http://linuxsocketprogrammingshare.blogspot.com/,you can share your thread and comment ..Also you can work on assignments which will be uploaded shortly Also at my facebook group https://www.facebook.com/groups/codingtheworld/
INF1060 introduction to operating systems and data communication
2005 Kjell ge Bringsrud & Pl Halvorsen