1 /* A simple server in the internet domain using TCP
2 The port number is passed as an argument */
5 #include <sys/socket.h>
6 #include <netinet/in.h>
14 int main(int argc
, char *argv
[])
16 int sockfd
, newsockfd
, portno
, clilen
;
18 struct sockaddr_in serv_addr
, cli_addr
;
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);
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");
37 clilen
= sizeof(cli_addr
);
39 newsockfd
= accept(sockfd
,
40 (struct sockaddr
*) &cli_addr
,
44 error("ERROR on accept");
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");