Path: blob/master/06-half-duplex-chat-tcp-ip/server.c
137 views
/*1* Title : Half duplex server side2* Name : Aditya Pratap Singh Rajput3* Subject : Network Protocols And Programming using C4*5Note : please consider the TYPOS in comments.6Thanks.7*/89#include "stdio.h"10#include "stdlib.h"11#include "string.h"12//headers for socket and related functions13#include <sys/types.h>14#include <sys/socket.h>15//for including structures which will store information needed16#include <netinet/in.h>17#include <unistd.h>18//for gethostbyname19#include "netdb.h"20#include "arpa/inet.h"21#define MAX 100022#define BACKLOG 5 // how many pending connections queue will hold23int main()24{25char serverMessage[MAX];26char clientMessage[MAX];27//create the server socket28int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);293031struct sockaddr_in serverAddress;32serverAddress.sin_family = AF_INET;33serverAddress.sin_port = htons(9002);34serverAddress.sin_addr.s_addr = INADDR_ANY;3536//calling bind function to oir specified IP and port37bind(socketDescriptor, (struct sockaddr*)&serverAddress, sizeof(serverAddress));3839listen(socketDescriptor, BACKLOG);4041//starting the accepting42int clientSocketDescriptor = accept(socketDescriptor, NULL, NULL);4344while (1)45{46printf("\ntext message here .. :");47scanf("%s", serverMessage);48send(clientSocketDescriptor, serverMessage, sizeof(serverMessage) , 0);49//recieve the data from the server50recv(clientSocketDescriptor, &clientMessage, sizeof(clientMessage), 0) ;51//recieved data from the server successfully then printing the data obtained from the server52printf("\nCLIENT: %s", clientMessage);5354}55//close the socket56close(socketDescriptor);57return 0;58}596061