Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
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.
 
#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.
 
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}
#define MAX
max.
Definition server.c:33

◆ main()

int main ( void  )

Driver code.

82{
83#ifdef _WIN32
84 // when using winsock2.h, startup required
85 WSADATA wsData;
86 if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
87 {
88 perror("WSA Startup error: \n");
89 return 0;
90 }
91
92 atexit(cleanup); // register at-exit function
93#endif
94
95 int sockfd, connfd;
96 unsigned int len;
97 struct sockaddr_in servaddr, cli;
98
99 // socket create and verification
100 sockfd = socket(AF_INET, SOCK_STREAM, 0);
101 if (sockfd == -1)
102 {
103 perror("socket creation failed...\n");
104 exit(0);
105 }
106 else
107 {
108 printf("Socket successfully created..\n");
109 }
110 bzero(&servaddr, sizeof(servaddr));
111
112 // assign IP, PORT
113 servaddr.sin_family = AF_INET;
114 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
115 servaddr.sin_port = htons(PORT);
116
117 // Binding newly created socket to given IP and verification
118 if ((bind(sockfd, (SA *)&servaddr, sizeof(servaddr))) != 0)
119 {
120 perror("socket bind failed...\n");
121 exit(0);
122 }
123 else
124 {
125 printf("Socket successfully binded..\n");
126 }
127
128 // Now server is ready to listen and verification
129 if ((listen(sockfd, 5)) != 0)
130 {
131 perror("Listen failed...\n");
132 exit(0);
133 }
134 else
135 {
136 printf("Server listening..\n");
137 }
138 len = sizeof(cli);
139
140 // Accept the data packet from client and verification
141 connfd = accept(sockfd, (SA *)&cli, &len);
142 if (connfd < 0)
143 {
144 perror("server acccept failed...\n");
145 exit(0);
146 }
147 else
148 {
149 printf("server acccept the client...\n");
150 }
151
152 // Function for chatting between client and server
153 func(connfd);
154
155 // After chatting close the socket
156 close(sockfd);
157 return 0;
158}
#define SA
shortname for sockaddr
Definition server.c:35
#define PORT
port number to connect to
Definition server.c:34
void func(int sockfd)
Continuous loop to send and receive over the socket.
Definition server.c:47
Here is the call graph for this function: