Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
server.c File Reference

Server side implementation of Server-Client system. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
Include dependency graph for server.c:

Macros

#define MAX   80
 max. More...
 
#define PORT   8080
 port number to connect to
 
#define SA   struct sockaddr
 shortname for sockaddr
 

Functions

void func (int sockfd)
 Continuous loop to send and receive over the socket. More...
 
int main ()
 Driver code.
 

Detailed Description

Server side implementation of Server-Client system.

Author
Nairit11
Krishna Vedala
See also
client_server/client.c

Macro Definition Documentation

◆ MAX

#define MAX   80

max.

characters per message

Function Documentation

◆ func()

void func ( int  sockfd)

Continuous loop to send and receive over the socket.

Exits when "exit" is sent from commandline.

Parameters
sockfdsocket handle number
48 {
49  char buff[MAX];
50  int n;
51  // infinite loop for chat
52  for (;;)
53  {
54  bzero(buff, MAX);
55 
56  // read the message from client and copy it in buffer
57  read(sockfd, buff, sizeof(buff));
58  // print buffer which contains the client contents
59  printf("From client: %s\t To client : ", buff);
60  bzero(buff, MAX);
61  n = 0;
62  // copy server message in the buffer
63  while ((buff[n++] = getchar()) != '\n')
64  {
65  ;
66  }
67 
68  // and send that buffer to client
69  write(sockfd, buff, sizeof(buff));
70 
71  // if msg contains "Exit" then server exit and chat ended.
72  if (strncmp("exit", buff, 4) == 0)
73  {
74  printf("Server Exit...\n");
75  break;
76  }
77  }
78 }
MAX
#define MAX
max.
Definition: server.c:33