Skip to content

Instantly share code, notes, and snippets.

@shubham7298
Created June 27, 2019 19:24
Show Gist options
  • Save shubham7298/730f35011bcd203a1bbdce006f03b3b2 to your computer and use it in GitHub Desktop.
Save shubham7298/730f35011bcd203a1bbdce006f03b3b2 to your computer and use it in GitHub Desktop.
Build a server socket in C
#include <stdio.h> // io
#include<sys/types.h> // def. of datatypes used in system calls
#include<sys/socket.h> // def. of structures needed for socket
#include<netinet/in.h> // constants and addr. needed for internet domain addr.
// this func is called when a system call fails
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen, n;
// sockfd and newsockfd are file descriptors.
// variables store the values returned by the socket system call and the accept system call
// portno stores port no. to which server accepts connection
//clilen stores the size of addr. of the client.
// n is the return value for read() & write() calls.
char buffer[256];
// server reads characters from socket connection into the buffer.
struct sockaddr_in ,serv_addr, cli_addr;
// sockaddr_in is the address containing internet address.
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
error("ERROR opening socket");
// socket creates a new socket.
// first argument is addr domain
// AF_INET is internet domain - 2 hosts
// AF_UNIX is unix domain - 2 processes
// Type of socket
// SOCK_STREAM stream socket - char. read in continous stream as if a file
// SOCK_DGRAM socket datagrams - msg read in chunks
// Protocol - if 0 OS will choose more appropriate protocol
// TCP for stream sock and UDP for datagram socks.
bzero((char*) &serv_addr, sizeof(serv_addr));
// bzero() sets all values in buffer to zero
// pointer to buffer
// size of buffer
portno = atoi(argv[1]);
// port no. on which server will listen is passed in args
// atoi - str to int
serv_addr.sin_family = AF_INET;
// code for addr family
serv_addr.sin_portt = htons(portno);
// htnos - host byte order to network byte order
serv_addr.sin_addr.s_addr = INADDR_ANY;
// IP addr of the host
if( bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
//
listen(sockfd,5);
// allows process to listen on sockets for connections.
// socket file descriptor
// backlog queue - number of connections that can be waiting while the process is handling a particular connection
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
// accept() system call causes the process to block until a client connects to the server
// it wakes up the process when connection is estd.
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment