forked from drouyang/memcached_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.c
111 lines (81 loc) · 2.71 KB
/
conn.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "conn.h"
extern int verbose;
struct conn* createConnection(const char* ip_address, int port, int protocol, int naggles) {
struct conn* connection = malloc(sizeof(struct conn));
memset(connection, 0, sizeof(struct conn));
if(protocol == UDP_MODE) {
connection->sock = openUdpSocket(ip_address, port);
connection->protocol = UDP_MODE;
} else {
connection->sock = openTcpSocket(ip_address, port);
connection->protocol = TCP_MODE;
if(!naggles) {
//Disable naggle's algorithm
int flag = 1;
int result = setsockopt(connection->sock, /* socket affected */
IPPROTO_TCP, /* set option at TCP level */
TCP_NODELAY, /* name of option */
(char *) &flag, /* the cast is historical cruft */
sizeof(int)); /* length of option value */
if (result < 0){
printf("couldn't set tcp_nodelay\n");
exit(-1);
}
}
}
static int uid_gen;
connection->uid = uid_gen;
uid_gen++;
if (verbose)
printf("Created connection on fd %d, uid %d\n", connection->sock, connection->uid);
return connection;
}//End createConnection()
int openTcpSocket(const char* ipAddress, int port) {
//Create a socket
int sock = socket(AF_INET, SOCK_STREAM, 0);
if( sock < 0 ){
printf("ERROR: Couldn't create a socket\n");
exit(-1);
}
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
//Use IPv4
server.sin_family = AF_INET;
//Convert IP address to network order
if( inet_pton(AF_INET, ipAddress, &server.sin_addr.s_addr) < 0){
printf("IP Address error\n");
exit(-1);
}
//Use the standard memcached port
int pport= (port!=0) ? port : MEMCACHED_PORT;
server.sin_port = htons(pport);
int error = connect(sock, (struct sockaddr *)&server, sizeof(server));
if(error < 0){
printf("Connection error\n");
exit(-1);
}
if (verbose)
printf("TCP connected\n");
return sock;
}//End openTcpSocket()
int openUdpSocket(const char* ipAddress, int port) {
int sock = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in server;
memset(&server, 0, sizeof(struct sockaddr));
server.sin_family = AF_INET;
int pport= (port!=0) ? port : MEMCACHED_PORT;
server.sin_port = htons(pport);
//Convert IP address to network order
if( inet_pton(AF_INET, ipAddress, &server.sin_addr.s_addr) < 0){
printf("IP Address error\n");
exit(-1);
}
int error = connect(sock, (struct sockaddr *)&server, sizeof(server));
if(error < 0){
printf("Couldn't connect with udp\n");
exit(-1);
}
if (verbose)
printf("UDP connected\n");
return sock;
}//End openUdpSocket()