Docsity

Sockets - Linux System Programming - Lecture Notes, Study notes of Linux skills

Toggle sidebar
bg1
Sockets
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Sockets - Linux System Programming - Lecture Notes and more Study notes Linux skills in PDF only on Docsity!

Sockets

Socket connection

  • A Socket is another method of inter-process communication. It allows us to develop the true distributed client/server applica- tions to run either locally, or across net- works.
  • Socket connection is as phone call (client) to an operator, and operator put it through the correct department(server process), and then from there to the right per- son(server socket).
  • A Socket is characterized by three at- tributes: Socket Domain, Socket Type and Protocol.
  • A Socket domain is an abstraction that provides an addressing structure and a set of protocols. Sockets connect only with sockets in the same domain.
  • A Stream socket guarantees the data not to be lost, duplicated or reordered with- out an indication of an error. Large mes- sages are fragmented, transmitted and re-assembled.
  • A Datagram socket supports a two-way flow of messages. The order of message is not guaranteed, it may get lost or du- plicated, also record boundaries are pre- served.

Socket Function Prototypes

  1. Creating a Socket

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

int socket(int domain, int type, int protocol);

protocol is determined by the socket do- main and type. Normally not specified. The sytem select the default protocol that supports the specified socket type. The socket handle (a file descriptor) is re- turned.

A remote process has no way to iden- tify a socket until an address is bound to it. Communicating processes connect through addresses.

In the UNIX domain, a connection is usu- ally composed of one or two path names. In the Internet domain, a connection is composed of local and remote addresses and local and remote ports.

#include <sys/un.h>

struct sockaddr_un{ sa_family_t sun_family; char sun_path[]; }; In Internet domain, the address is speci- fied with this structure: #include <sys/netinet/in.h>

struct sockaddr_in{ short int sin_family; unsigned short int sin_port; struct in_addr sin_addr; }; The IP address structure is defined as, #include <sys/netinet/in.h>

struct in_addr{ unsigned long int s_addr; };

  1. Creating a Socket queue

#include <sys/socket.h>

int listen(int sr_sd, int backlog);

Server create a queue for storing the pend- ing requests. It allows incoming connec- tions to be held pending while server pro- gram is busy dealing with a previous client.

The size of the queue is set to “backlog”, the value of 5 is common used.

listen() returns 0 on success, -1 and a set of errno for failure.

  1. Requesting a connections

#include <sys/socket.h>

int connect(int sd, struct sockaddr *address, size_t addr_length);

Client program requests a connection be- tween an unnamed socket to the server and the server listen the socket.

Applications

socket on file system

  1. A local client

#include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <unistd.h> #include <stdio.h>

int main() { int sockfd; //socket descriptor int len; //len of address

struct sockaddr_un address; int result; //result from connection

char ch = ’A’;

/* client create a socket*/ sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

/associate the socket as agree with server/ address.sun_family = AF_UNIX; strcpy(address.sun_path, "server_socket"); len= sizeof(address);

/* request a connection to server */ result= connect(sockfd, (struct sockaddr ) &address, len); if(result == -1){ perror("oops, client 1"); exit(1); } /now write to the server */ write(sockfd, &ch, 1); read(sockfd, &ch, 1); printf("char from server = %c\n", ch); close(sockfd); exit(0); }

/* Name the socket, it available to client*/ server_address.sun_family= AF_UNIX; strcpy(server_address.sun_path, "server_socket"); server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *) &server_address, server_len);

/* create a socket queue wait for client*/ listen(server_sockfd, 5); while(1){ char ch; printf("server waiting\n");

/* accept a client*/ client_len= sizeof(client_address); client_sockfd= accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);

/read and write to client/

read(client_sockfd, &ch, 1); ch ++; write(client_sockfd, &ch, 1); close(client_sockfd); } }

socket on network

  1. Network client

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

int main() { int sockfd; //socket descriptor int len; //len of address

write(sockfd, &ch, 1); read(sockfd, &ch, 1); printf("char from server = %c\n", ch); close(sockfd); exit(0); }

  1. Network server

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

int main() { int server_sockfd; int client_sockfd; int server_len; int client_len;

struct sockaddr_in server_address; struct sockaddr_in client_address;

/* server create an new socket*/ server_sockfd = socket(AF_INET,SOCK_STREAM,0);

/* Name the socket, it available to client/ server_address.sin_family= AF_INET; /allow the connection from any IP address*/ server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port= 9734; server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *) &server_address, server_len);

/* create a socket queue wait for client*/ listen(server_sockfd, 5); while(1){ char ch; printf("server waiting\n");

/* accept a client*/

After the server socket is created, the server program calls fork(), then a second copy of server is generated, also the open socket is inherited.

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

int main() { int server_sockfd; int client_sockfd; int server_len; int client_len; pid_t pid;

struct sockaddr_in server_address; struct sockaddr_in client_address;

/* server create an new socket*/ server_sockfd = socket(AF_INET,SOCK_STREAM,0);

/* Name the socket, it available to client/ server_address.sin_family= AF_INET; /allow the connection from any IP address*/ server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port= 9734; server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *) &server_address, server_len);

/* create a socket queue wait for client/ listen(server_sockfd, 5); /don’t wait for child to complete*/ signal(SIGCHLD, SIG_IGN); while(1){ char ch; printf("server waiting\n");

/* accept a client*/ client_len= sizeof(client_address);