• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Client Server C program

November 28, 2015 by Dhaval Dave

Today we are going to show you to write simple C level Client – Server Networking & Communication program. This Program and methods are base to create any communication software like whatsapp, fb chat or other, because each connected terminal’s chat goes via server and then routed to device. So this is fist step for communication.

Step 1 : Understand Client Server Communication architecture from book

Step 2 : Run this client and Server code, modify and understand.

Step 3: Enjoy :)

Client Code 
__________________________
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define RAND_MAX 3

int main(int argc, char *argv[])
{
int sockfd;//client & Main Server
int c_len;
int i,j,rc;
char buff[100];
struct sockaddr_in c_address;
printf("**********This is the client program*************\n");

sockfd=socket(AF_INET, SOCK_STREAM,0);
c_address.sin_family = AF_INET;
c_address.sin_addr.s_addr = inet_addr("127.0.0.1");
c_address.sin_port = htons(7734);
c_len=sizeof(c_address);
rc=connect(sockfd, (struct sockaddr *)&c_address, c_len);
if(rc==1){
perror("Error in Client-Server Connect");
exit(0);
}
while(1){
printf("Enter Data(Buffer)\n");
gets(buff);

send(sockfd,buff,100,0);
}//while endeds
close (sockfd);
return(0);

}//main ended
Server Code
__________________________

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h> 

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define RAND_MAX 3

int main(int argc, char *argv[])
{

int s_sockfd, c_sockfd;//client & Main Server
int sc[4];//child servers pids
int sc_sockfd[4];//Child Server Socket IDs
int s_len, c_len;
int i,j,rc;
long int rand;
char buff[100];

struct sockaddr_in s_add;
struct sockaddr_in c_add;

if(s_sockfd=socket(AF_INET,SOCK_STREAM,0)<0)
 perror("Unable to create SOCKET :");

bzero((char *)&s_add,sizeof(s_add));
s_add.sin_family = AF_INET;
s_add.sin_addr.s_addr = htons(INADDR_ANY);
s_add.sin_port = htons(7766);
s_len=sizeof(s_add);


if(bind(s_sockfd, (struct sockaddr *) &s_add, s_len)<0)
 {
 perror("Error in ''Bind'' \n:");
 exit(0);
 }
if(listen(s_sockfd,10)<0)
 {
 perror("Error in ''Listen'' \n:");
 exit(0);
 }

for(i=0;i<4;i++)
 {
 sc[i]=fork();//four child server created;
 if(sc[i] < 0)
 perror("Error in Child server \n");
 }//e o for i<4 loop

while(1){
flag:
rand=random();//random numbers in the range from 0 to RAND_MAX
if(rand>3)
 goto flag;

if(sc[rand]==0){

 c_len = sizeof(c_add);
 c_sockfd = accept( sc[rand], (struct sockaddr *) &c_add, c_len);

 printf("After accept()");
 if(c_sockfd<0)
 {
 printf("Accept error\n");
 exit(0);
 }
 //close(s_sockfd);//dnt close it becuse child server will resend data to it
 
 dup2(c_sockfd,0);//std ip
 dup2(c_sockfd,1);//std op

 rc = recv(c_sockfd,buff,100,0);/*buff received from client*/
 
 printf("Received from client data => %s",buff);
 
 send(s_sockfd,buff,100,0);/*now resend it*/

}//if sc[r-1]==0 ended //child ended

}//while ended
rc = recv(sc[rand],buff,100,0); //received from child server
printf("Received at Main Server = >%s",buff);

close (s_sockfd);
return(0);
}//main ended

Similar Articles

Filed Under: Interview Questions, problem Tagged With: c

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Find the kth number with prime factors 3, 5 and 7

Find Percentage of Words matching in Two Strings

Add Sub Multiply very large number stored as string

Linked List V/S Binary Search Tree

Print all nodes that are at distance k from a leaf node

K’th Largest Element in BST when modification to BST is not allowed

Daughter’s Age VeryGood Puzzle

Adobe Interview Questions 8 month Exp

Circular Linked List

Python List

Find loop in Singly linked list

How Radix sort works

Binary Tree in Java

Apriori algorithm C Code Data Mining

Number of Islands BFS/DFS

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

The Magic HackerEarth Nirvana solutions Hiring Challenge

Amazon Interview Experience – SDE Chennai

TicTacToe Game As Asked in Flipkart

Subset Sum Problem Dynamic programming

Mirror of Tree

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

simple sql injection

Spanning Tree

Calculate price of parking from parking start end time prices

Find min element in Sorted Rotated Array (Without Duplicates)

Sort Stack in place

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Find the smallest window in a string containing all characters of another string

Check if an array has duplicate numbers in O(n) time and O(1) space

Copyright © 2025 · Genesis Framework · WordPress · Log in