Introduction To Berkeley Sockets: Data Communication
Introduction To Berkeley Sockets: Data Communication
Data Communication:
Big Picture
Machine Machine 1
process A
Machine 2
process B
network
Goal
Introduce socket API We will write two programs
A client and a server
What we want
Machine Machine 1
client kaksi.ifi.uio.no server
Hello world!
Hello world!
network
Hello world!
Hello world!
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>
Creation of a connection
One side must be the active one
take the initiative in creating the connection this side is called the client
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
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>
<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
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/