-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoServer.c
More file actions
66 lines (56 loc) · 1.47 KB
/
EchoServer.c
File metadata and controls
66 lines (56 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<errno.h> // perror
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
#define ERROR -1
#define MAX_CLIENTS 2 //max no of clients in queue
#define MAX_DATA 1024
int main(int argc,char **argv){
struct sockaddr_in server,client;
int sock;
int new; //for descriptor for client
int sockaddr_len = sizeof(struct sockaddr_in);
int data_len;
char data[MAX_DATA];
if((sock = socket(AF_INET,SOCK_STREAM,0))== ERROR){
perror("server socket: ");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[1]));
server.sin_addr.s_addr = INADDR_ANY;//to listen on all interfaces on our host(local machine) on this given port
bzero(&server.sin_zero,0);
if(bind(sock,(struct sockaddr *)&server,sockaddr_len)==ERROR){
perror("bind: ");
exit(-1);
}
if((listen(sock,MAX_CLIENTS))==ERROR){
perror("listen: ");
exit(-1);
}
while(1){
//accept is a blocking call
if((new = accept(sock,(struct sockaddr *)&client, &sockaddr_len))==ERROR){
perror("accept");
exit(-1);
}
printf("New client connected from port no %d and IP is %s \n",ntohs(client.sin_port),inet_ntoa(client.sin_addr));
data_len = 1;
while(data_len){
data_len = recv(new, data, MAX_DATA, 0);
if(data_len){
send(new,data,data_len,0);
data[data_len]='\0';
printf("Sent mesg; %s",data);
}
}
printf("Client disconneted\n");
close(new);
}
close(sock);
}