-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path033server.c
More file actions
58 lines (48 loc) · 1.63 KB
/
033server.c
File metadata and controls
58 lines (48 loc) · 1.63 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
/*
..........................................................................................................................................
Name : 033(server).c
Author : SHRUTI VERMA
Description : Write a program to communicate between two machines using socket.
Date : 30 Sep 2025
..........................................................................................................................................
*/
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/socket.h>
#include<fcntl.h>
#include<netinet/in.h>
int main() {
int sockfd, newsockfd;
//create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//bind to IP/Port
struct sockaddr_in saddr, caddr;
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = INADDR_ANY;
saddr.sin_port = htons(8080);
bind(sockfd, (struct sockaddr*)&saddr, sizeof(saddr));
//listen for connections on socket
listen(sockfd, 5);
while(1) {
//accept client on newsocket
socklen_t clen;
clen = sizeof(caddr);
newsockfd = accept(sockfd, (struct sockaddr*)&caddr, &clen);
// send and recieve
char buffer[1024];
read(newsockfd, buffer, sizeof(buffer));
printf("Client says - %s\n", buffer);
write(newsockfd, "Hello from server!", 19);
}
//close
close(newsockfd);
close(sockfd);
}
/*------------------------------------OUTPUT------------------------------------
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ ./server
Client says - Hello Server!!
Client says - Hello Server!!
Client says - Hello Server!!
Client says - Hello Server!!
*/