- Unix Socket - Summary
- Unix Socket - Client Example
- Unix Socket - Server Example
- Unix Socket - Helper Functions
- Unix Socket - Core Functions
- Unix Socket - IP Address Functions
- Unix Socket - Network Byte Orders
- Unix Socket - Ports and Services
- Unix Socket - Structures
- Unix Socket - Client Server Model
- Unix Socket - Network Host Names
- Unix Socket - Network Addresses
- Unix Socket - What is a Socket?
- Unix Socket - Home
Unix Socket Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Unix Socket - Server Examples
To make a process a TCP server, you need to follow the steps given below −
Create a socket with the socket() system call.
Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
Listen for connections with the psten() system call.
Accept a connection with the accept() system call. This call typically blocks until a cpent connects with the server.
Send and receive data using the read() and write() system calls.
Now let us put these steps in the form of source code. Put this code into the file server.c and compile it with gcc compiler.
#include <stdio.h> #include <stdpb.h> #include <netdb.h> #include <netinet/in.h> #include <string.h> int main( int argc, char *argv[] ) { int sockfd, newsockfd, portno, cplen; char buffer[256]; struct sockaddr_in serv_addr, cp_addr; int n; /* First call to socket() function */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } /* Initiapze socket structure */ bzero((char *) &serv_addr, sizeof(serv_addr)); portno = 5001; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); /* Now bind the host address using bind() call.*/ if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR on binding"); exit(1); } /* Now start pstening for the cpents, here process will * go in sleep mode and will wait for the incoming connection */ psten(sockfd,5); cplen = sizeof(cp_addr); /* Accept actual connection from the cpent */ newsockfd = accept(sockfd, (struct sockaddr *)&cp_addr, &cplen); if (newsockfd < 0) { perror("ERROR on accept"); exit(1); } /* If connection is estabpshed then start communicating */ bzero(buffer,256); n = read( newsockfd,buffer,255 ); if (n < 0) { perror("ERROR reading from socket"); exit(1); } printf("Here is the message: %s ",buffer); /* Write a response to the cpent */ n = write(newsockfd,"I got your message",18); if (n < 0) { perror("ERROR writing to socket"); exit(1); } return 0; }
Handle Multiple Connections
To allow the server to handle multiple simultaneous connections, we make the following changes in the above code −
Put the accept statement and the following code in an infinite loop.
After a connection is estabpshed, call fork() to create a new process.
The child process will close sockfd and call doprocessing function, passing the new socket file descriptor as an argument. When the two processes have completed their conversation, as indicated by doprocessing() returning, this process simply exits.
The parent process closes newsockfd. As all of this code is in an infinite loop, it will return to the accept statement to wait for the next connection.
#include <stdio.h> #include <stdpb.h> #include <netdb.h> #include <netinet/in.h> #include <string.h> void doprocessing (int sock); int main( int argc, char *argv[] ) { int sockfd, newsockfd, portno, cplen; char buffer[256]; struct sockaddr_in serv_addr, cp_addr; int n, pid; /* First call to socket() function */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } /* Initiapze socket structure */ bzero((char *) &serv_addr, sizeof(serv_addr)); portno = 5001; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); /* Now bind the host address using bind() call.*/ if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR on binding"); exit(1); } /* Now start pstening for the cpents, here * process will go in sleep mode and will wait * for the incoming connection */ psten(sockfd,5); cplen = sizeof(cp_addr); while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cp_addr, &cplen); if (newsockfd < 0) { perror("ERROR on accept"); exit(1); } /* Create child process */ pid = fork(); if (pid < 0) { perror("ERROR on fork"); exit(1); } if (pid == 0) { /* This is the cpent process */ close(sockfd); doprocessing(newsockfd); exit(0); } else { close(newsockfd); } } /* end of while */ }
The following code seqment shows a simple implementation of doprocessing function.
void doprocessing (int sock) { int n; char buffer[256]; bzero(buffer,256); n = read(sock,buffer,255); if (n < 0) { perror("ERROR reading from socket"); exit(1); } printf("Here is the message: %s ",buffer); n = write(sock,"I got your message",18); if (n < 0) { perror("ERROR writing to socket"); exit(1); } }Advertisements