-
Notifications
You must be signed in to change notification settings - Fork 1
/
server3.c
56 lines (39 loc) · 1.08 KB
/
server3.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
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
int main()
{
char server_message[256] = "Hi, Yes you have reached the server!";
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);
// send the message
char st1[30]="exit";
do
{
if(recv(client_socket, &buf, sizeof(buf), 0))
{
printf(" %s \n", buf);
}
printf("enter message ");
scanf("%s",&server_message);
send(client_socket, server_message, sizeof(server_message), 0);
}
while(strcmp(server_message,st1));
// close the socket
close(server_socket);
return 0;
}