-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
101 lines (85 loc) · 2.12 KB
/
server.c
File metadata and controls
101 lines (85 loc) · 2.12 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void chat(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff, MAX);
// read the message from client and copy it into the buffer
read(sockfd, buff, sizeof(buff));
// print buffer which contains the client contents
printf("\nCLIENT: %sEnter MESSAGE: ",buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;
// and send that buffer to client
write(sockfd, buff, sizeof(buff));
// if msg contains "exit" then the server exits and the chat is ended
if (strncmp("exit", buff, 4) == 0)
{
printf("\nSERVER Exit !\n");
break;
}
}
}
// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("Socket creation failed !\n");
exit(0);
}
else
printf("Socket successfully created\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0)
{
printf("Socket bind failed !\n");
exit(0);
}
else
printf("Socket successfully binded\n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0)
{
printf("Listen failed !\n");
exit(0);
}
else
printf("Server listening\n");
len = sizeof(cli);
// Accept the data packet from client and verification
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("Server acccept failed !\n");
exit(0);
}
else
printf("Server acccepted the client\n");
// Function for chatting between client and server
chat(connfd);
// After chatting close the socket
close(sockfd);
}