



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















#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; };
#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.
#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.
socket on file system
#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
#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); }
#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);