/
tcpserver.c
64 lines (51 loc) · 1.6 KB
/
tcpserver.c
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
59
60
61
62
63
64
/* Writing here the iterative connection oriented server */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
// Passive Sock
int
passiveSock(const char *service, const char *transport, int qlen) {
struct servent *pserv ;
struct protoent *pproto ;
struct sockaddr_in sin ;
int sd, type;
/* Initialize sock address struct to 0 */
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY ; //Get connection from any IP address
/* Map the input service to port number */
if (pserv = getservbyname(service, transport)) {
sin.sin_port = (unsigned short)pserv->s_port ;
}
else if ( (sin.sin_port = htons( (unsigned short) atoi(service))) == 0) {
printf("Error in getting service port number.\n");
}
/* Map the transport protocol type to port number */
if ((pproto = getprotobyname(transport)) == 0)
printf("Error in getting protocol port number. \n");
/* Use protocol to choose socket type */
if (strcmp(transport, "udp"))
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
/* Allocate a socket */
sd = socket(PF_INET, type, pproto->p_proto);
if(sd < 0)
printf("Error in allocating a socket \n");
/* Bind a socket */
if (bind(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
printf("Error in binding the socket to port %s\n", service);
if (type == SOCK_STREAM && listen(sd, qlen) < 0)
printf("Can't listen to port \n");
return sd;
}
int passiveTCP(const char *service) {
return passiveSock(service, "tcp", 0);
}
int main(int argc, char *argv[]){
return 0;
}