Network System Calls
// Libraries
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include<pthread.h>
#include <stdbool.h>
#include <time.h>
//Socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0); /// for UDP
sockfd = socket(AF_INET ,SOCK_STREAM, 0); /// for TCP
// Server Information
struct sockaddr_in servaddr,cliaddr;
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
memset(&buffer, 0, sizeof(buffer));
// Bind
bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr));
// listen
listen(sockfd, 10);
// Receiving data in server side
Int len=sizeof(cliaddr);
int n=recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, ( struct sockaddr *) &cliaddr, &len);
buffer[n]='\0';
// sending data in server side
sendto(sockfd, (const char *)hello, strlen(hello), MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
sizeof(cliaddr));
// Receiving data in client side
Int len=sizeof(servaddr);
int n=recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, ( struct sockaddr *) &servaddr, &len);
buffer[n]='\0';
// sending data in client side
sendto(sockfd, (const char *)hello, strlen(hello), MSG_CONFIRM, (const struct sockaddr *) &servaddr,
sizeof(servaddr));
// Connect
connect (sockfd, (const struct sockaddr *) &servaddr, sizeof(servaddr));
// Accept
int newfd=accept(sockfd,(struct sockaddr *)&cliaddr,&len);
// Receiving data
int n=recv(sockfd, (char *)buffer, MAXLINE, 0);
buffer[n]='\0';
// sending data
send(sockfd, (const char *)hello, strlen(hello),0);
// Close Socket
close(sockfd);
// Fork
int pid = fork();
if(pid == -1){
perror("Issue in fork call");
continue;
f(pid == 0){
// child process
else {
// parent process
// threads
pthread_t tid; //thread declaration
pthread_create(&tid,NULL, (void*)function_Name,NULL); // call to create a thread, 4 th argument is
NULL, if you want to pass some data in thread then you just need to change fourth argument.
void* function_Name (void *ptr) // this function will have only one parameter
{}
pthread_join(tid, NULL);
pthread_exit(NULL);
// conversions
sprintf(portArr,"%d", ntohs(cliaddr.sin_port)); // into string
int num=atoi(buffer); // ascii to integer
// String built-in functions
strcat(str, str1); // for concatenation
strtok(str,”.”); // for tokenization
strcpy(buffer, "Hello"); // for copy string
//To get current date & time
struct tm* local;
time_t t = time(NULL);
local = localtime(&t);
//dynamic memory allocation
int * ptr = (int *)malloc(4 * sizeof(int)); // 4 integer pointers
//dynamic memory deallocation
int * ptr = (int *)malloc(4 * sizeof(int)); // 4 integer pointers
free(ptr) //deallocation of ptr
// to retreive IP and Port in host byte order
char* ip = inet_ntoa(cliaddr.sin_addr);
int port = ntohs(cliaddr.sin_port);
// File handling in append mode
FILE * file = fopen("[Link]", "a");
fprintf(file, "%s %s\n", buffer, asctime(local));
fclose(file);
// Alternate of Printf(“”);
write(1,data,strlen(data));
// TCP client server (Iterative ) flow chart