root / trunk / code / projects / mapping / python / netserv.c @ 902
History | View | Annotate | Download (897 Bytes)
1 |
#include "netserv.h" |
---|---|
2 |
#include <stdio.h> |
3 |
#include <stdlib.h> |
4 |
#include <strings.h> |
5 |
#include <string.h> |
6 |
#include <unistd.h> |
7 |
#include <sys/types.h> |
8 |
#include <sys/socket.h> |
9 |
#include <netinet/in.h> |
10 |
|
11 |
int make_sock(int portno) |
12 |
{ |
13 |
|
14 |
int sockfd, newsockfd, clilen;
|
15 |
struct sockaddr_in serv_addr, cli_addr;
|
16 |
|
17 |
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
18 |
if (sockfd < 0) |
19 |
error("ERROR opening socket");
|
20 |
bzero((char *) &serv_addr, sizeof(serv_addr)); |
21 |
serv_addr.sin_family = AF_INET; |
22 |
serv_addr.sin_addr.s_addr = INADDR_ANY; |
23 |
serv_addr.sin_port = htons(portno); |
24 |
if (bind(sockfd, (struct sockaddr *) &serv_addr, |
25 |
sizeof(serv_addr)) < 0) |
26 |
error("ERROR on binding");
|
27 |
listen(sockfd,5);
|
28 |
clilen = sizeof(cli_addr);
|
29 |
newsockfd = accept(sockfd, |
30 |
(struct sockaddr *) &cli_addr,
|
31 |
(socklen_t *) &clilen); |
32 |
if (newsockfd < 0) |
33 |
error("ERROR on accept");
|
34 |
return newsockfd;
|
35 |
} |