-
Notifications
You must be signed in to change notification settings - Fork 0
/
server2.c
47 lines (34 loc) · 931 Bytes
/
server2.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
int main() {
char server_message[256];
char buf[200];
// create the server socket
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
// define the server address
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(3001);
server_address.sin_addr.s_addr = INADDR_ANY;
// bind the socket to our specified IP and port
bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
listen(server_socket, 5);
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
while(1)
{
// send the message
recv(client_socket, &buf, sizeof(buf), 0);
printf("\n %s \n", buf);
if(strcmp(buf,"GoodBye")==0)
break;
}
// close the socket
close(server_socket);
return 0;
}