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

Socket Programming: Sirak Kaewjamnong Computer Network Systems

A socket is a communication mechanism that allows systems to communicate locally or across networks. There are two main types - connection-oriented which establishes a connection between client and server before transmitting data, and connectionless which sends data without establishing a connection. To create a socket, a server calls socket(), bind(), listen(), and accept() while a client calls socket() and connect(). The server forks a new process for each client connection to allow simultaneous connections.

Uploaded by

aymanya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Socket Programming: Sirak Kaewjamnong Computer Network Systems

A socket is a communication mechanism that allows systems to communicate locally or across networks. There are two main types - connection-oriented which establishes a connection between client and server before transmitting data, and connectionless which sends data without establishing a connection. To create a socket, a server calls socket(), bind(), listen(), and accept() while a client calls socket() and connect(). The server forks a new process for each client connection to allow simultaneous connections.

Uploaded by

aymanya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Socket Programming

Sirak Kaewjamnong Computer Network Systems

Whats a Socket?
A socket is a communication mechanism that allows client/systems to be developed either locally or across networks.
C lie n t C lie n t S e rv e r

E th e r n e t

Connection-Oriented Protocol
socket() bind() listen() accept()
blocks until connection from client

server

client
connection establishment data (request) data(reply) socket() connect() write() read()

read() write()

Connectionless Protocol
socket() bind() recvfrom()
blocks until data received from a client

Server Client socket() bind()

data(request)

sendto()

process request sendto()

data(reply)

recvfrom()

The Steps in Establishing a Socket on Server

Create a socket with the socket() system call Bind the socket to an address using the bind() system call. For a server socket, an address consists of a port number on the host machine Listen for connections with listen() system call Accept a connection with the 5

The Steps in Establishing a Socket on Client

Create a socket with the socket() system call Connect the socket to the address of the server using the connect() system call Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() or send() and recv()system calls 6

Kinds of Socket
Traditional BSD families
TCP/IP (AF_INET,Internet) UNIX (AF_UNIX) XNS (AF_NS) APPLETALK,DECNET,IPX

Socket Address
A sockadd_in is a structure containing an internet address. This structure must defined in <netinet/in.h>
struct sockadd_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char }; sin_zero[8];
8

Socket System Call


int socket (int family, int type, int protocol); int socket (int domain, int type, int protocol);
Domain parameter specifies a communication domain;this selects the protocol family which will be used for communication Type specifies the communication semantics 9

Start Server Code(1)


#include <sys/types.h> This header file contains definitions of a number of data types used in system calls. These types are used in the next two include files. #include <sys/socket.h> The header file socket.h includes a number of definitions of structures needed for sockets. #include <netinet/in.h> 10 The header file in.h contains

Start Server Code(2)


int main( ) { int sockfd, newsockfd, portno, clilen;

sockfd and newsockfd are file descriptors, i.e. array subscripts into the file descriptor table. These two variables store the values returned by the socket system call and the accept system call. portno stores the port number on which the server accepts connections. 11

Start Server Code(3)


char buffer[256];

The server reads characters from the socket connection into this buffer
struct sockaddr_in serv_addr, cli_addr;

The variable serv_addr will contain the address of server The variable cli_addr will contain the address of client To create socket and return a socket descriptor that can be used for accessing the socket
12 AF_INET is family of IPv4 Internet Protocol
sockfd = socket(AF_INET,SOCK_STREAM,0); if (sockfd < 0) error(Error opening Socket);

Start Server Code(4)


serv_addr.sin_family = AF_INET;

The variable serv_addr is a structure of type struct sockaddr_in. This structure has four fields. The first field is short sin_family, which contains a code for the address family. It should always be set to the symbolic constant AF_INET.
serv_addr.sin_port = htons(portno);

The second field of serv_addr is unsigned short sin_port , which contain the port number. However, instead of simply copying the port 13 number to this field, it is necessary

Byte Ordering
different byte ordering in varoius platform
little endian : Intel 80x86 , DEC VAX , DEC PDP-11 little endian high-order byte low-order byte big endian : IBM 370 , Motorola 68000 , Pyramid addr A+1 addr A
big endian high-order byte addr A low-order byte addr A+1

14

Byte Ordering Routines


Network Byte Order is big endian
u_long htonl (u_long hostlong); //convert host-to-network,long int u_short htons(u_short hostshort); //convert host-to network,short int u_long ntohl (u_long netlong); // convert network-to-host,long int u_short ntohs (u_short netshort); // convert network-to-host,short integer 15

Start Server Code(5)


serv_addr.sin_addr.s_addr = INADDR_ANY;

The third field of sockaddr_in is a structure of type struct in_addr which contains only a single field unsigned long s_addr. This field contains the IP address of the host. For server code, this will always be the IP address of the machine on which the server is running, and there is a symbolic constant 16

Start Server Code(6)


if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding");

The bind() system call binds a socket to an address, in this case the address of the current host and port number on which the server will run. bind() takes three arguments,
the socket file descriptor the address to which is bound the size of the address to which it is bound.

The second argument is a pointer to a structure of type sockaddr, but what is passed in is a structure of17 type sockaddr_in, and so this must

Start Server Code(7)


listen(sockfd,5);

The listen system call allows the process to listen on the socket for connections.
The first argument is the socket file descriptor The second is the size of the backlog queue: the number of connections that can be waiting while the process is handling a particular connection.The maximum size 18 permitted by most systems.

Start Server Code(8)


clilen = sizeof(struct sockaddr_in); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept");

The accept() system call causes the process to block until a client connects to the server. It wakes up the process when a connection from a client has been successfully established. It returns a new file descriptor, and all communication on this connection should be done using 19 the new file descriptor.

Start Server Code(9)


n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer);

This code reads from the socket,the read call uses the new file descriptor, the one returned by accept(), not the original file descriptor returned by socket().
20

Start Server Code(10)


n=write(newsockfd,"I got your message",18); if (n < 0) error("ERROR writing to socket");

Once a connection has been established, both ends can both read and write to the connection. Naturally, everything written by the client will be read by the server, and everything written by the server will be read by the 21 client.

Start Server Code(11)


close (sockfd); close(newsockfd) }

The close() system call closes a socket descriptor of sockfd and newsockfd and terminates program

22

Start Client Code(1)


#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h>

The header files are the same as for the server

23

Start Client Code(2)


int main(int argc, char *argv[]) { int sockfd, portno,n; struct sockaddr_in serv_addr; char buffer[256];

All of this code is the same as that in the server


if (argc < 2) { fprintf(stderr,"usage %s IP address and port\n", argv[0]); exit(0); }

The user needs to pass in the port number on which the server will accept connections as an 24

Start Client Code(3)


sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket");

The socket() system call creates a new socket. It takes three arguments 1st argument is family:AF_INET is family of IPv4 Internet Protocol 2nd argument is type:SOCK_STREAM is type of connection-orient(TCP), SOCK_DGRAM is type of connectionless (UDP) 25
rd

Start Client Code(4)


serv_addr.sin_family = AF_INET;

The variable serv_addr is a structure of type struct sockaddr_in. This structure has four fields. The first field is short sin_family, which contains a code for the address family. It should always be set to the symbolic constant AF_INET
serv_addr.sin_port = htons(portno);

serv_addr is unsigned short sin_port , which contain the port number. However, instead of simply copying the port number to this field, it is 26

Start Client Code(5)


inet_pton(AF_INET, argv[1],&serv_addr.sin_addr);

This function converts the character string from first argument, Server address into a network address structure in the AF address
if (connect(sockfd,&serv_addr,sizeof(serv_a ddr)) < 0) error("ERROR connecting");

The connect function is called by the client to establish a connection to the server. It takes three arguments
The socket file descriptor,

27

Start Client Code(6)


n=write(sockfd,This is my message",18); if (n < 0) error("ERROR writing to socket");

Once a connection has been established, both ends can both read and write to the connection. Naturally, everything written by the client will be read by the server, and everything written by the server will be read by the client. 28

Start Client Code(7)


n = read(sockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer);

This code reads from the socket


close(sockfd);

The close() system call closes a socket descriptor of sockfd


29

Enhancements to The Server Code


To allow the server to handle multiple simultaneous connections

1. Put the accept statement and the following code in an infinite loop. 2. After a connection is established, call fork() to create a new process. 3. The child process will close sockfd and call dostuff(), passing the new socket file descriptor as an argument. When the two processes 30

Enhancements to The Server Code


while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd);

31

Enhancements to The Server Code


void do_stuff (int sock) { int n; char buffer[256]; n = read(sock,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); n = write(sock,"I got your message",18); if (n < 0) error("ERROR writing to socket"); 32 }

You might also like