-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloServer.c
More file actions
56 lines (43 loc) · 1.3 KB
/
HelloServer.c
File metadata and controls
56 lines (43 loc) · 1.3 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
/*This sample program creates a simple TCP server using socket which prints "Hello to the world of socket programming!!"
as soon as the client connects to the server*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h> // ntohs,ntohl,htons,htonl
#include<errno.h> // perror
int main(){
int sock,cli,sent;
struct sockaddr_in server, client;
unsigned int len;
char mesg[] = "Hello to the world of socket programming!!";
if((sock = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket: ");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons(10002);
server.sin_addr.s_addr = INADDR_ANY;
//alternative to bzero is memset
bzero(&server.sin_zero,0);//The bzero() function sets the first n bytes of the area starting at s to zero (bytes containing '\0').
len = sizeof(struct sockaddr_in);
if((bind(sock, (struct sockaddr *)&server,&len))==-1){
perror("bind");
exit(-1);
}
if(listen(sock,5)==-1){
perror("listen");
exit(-1);
}
while(1){
if((cli == accept(sock,(struct sockaddr *)&client, len))==-1){
perror("accept");
exit(-1);
}
sent = send(cli,mesg,strlen(mesg),0);
printf("Sent %d bytes to client : %s\n",sent,inet_ntoa(client.sin_addr)) ;
close(cli);
}
}