added a conflict
[munin-test.git] / server.c
blob82730d8bcf16bb3efea8ddd1c797ddeb893b2f1d
1 /* A simple server in the internet domain using TCP
2 The port number is passed as an argument */
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
8 void error(char *msg)
10 perror(msg);
11 exit(1);
14 int main(int argc, char *argv[])
16 int sockfd, newsockfd, portno, clilen;
17 char buffer[256];
18 struct sockaddr_in serv_addr, cli_addr;
19 int n;
20 portno = 4949;
21 serv_addr.sin_family = AF_INET;
22 serv_addr.sin_addr.s_addr = INADDR_ANY;
23 serv_addr.sin_port = htons(portno);
25 sockfd = socket(AF_INET, SOCK_STREAM, 0);
27 if (sockfd < 0)
28 error("ERROR opening socket");
30 bzero((char *) &serv_addr, sizeof(serv_addr));
32 if (bind(sockfd, (struct sockaddr *) &serv_addr,
33 sizeof(serv_addr)) < 0)
34 error("ERROR on binding");
36 listen(sockfd,5);
37 clilen = sizeof(cli_addr);
39 newsockfd = accept(sockfd,
40 (struct sockaddr *) &cli_addr,
41 &clilen);
43 if (newsockfd < 0)
44 error("ERROR on accept");
46 bzero(buffer,256);
47 n = read(newsockfd,buffer,255);
49 if (n < 0) error("ERROR reading from socket");
51 printf("Here is the message: %s\n",buffer);
53 n = write(newsockfd,"I got your message",18);
55 if (n < 0) error("ERROR writing to socket");
57 return 0;