Repository navigation
Expand file tree
/
Copy pathSocketConnection.cpp
More file actions
executable file
·160 lines (133 loc) · 4.58 KB
/
Copy pathSocketConnection.cpp
File metadata and controls
executable file
·160 lines (133 loc) · 4.58 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <resolv.h>
/*
* This is a very simple example client socket connection.
* This implementation is limited to Linux (POSIX) sockets only.
* A Microsoft Windows implementation is not supplied in this example.
*/
int OpenConnection(const char *hostname, int port)
{
int sd, err;
struct addrinfo hints = {}, *addrs;
char port_str[16] = {};
hints.ai_family = AF_INET; // Since your original code was using sockaddr_in and
// PF_INET, I'm using AF_INET here to match. Use
// AF_UNSPEC instead if you want to allow getaddrinfo()
// to find both IPv4 and IPv6 addresses for the hostname.
// Just make sure the rest of your code is equally family-
// agnostic when dealing with the IP addresses associated
// with this connection. For instance, make sure any uses
// of sockaddr_in are changed to sockaddr_storage,
// and pay attention to its ss_family field, etc...
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
sprintf(port_str, "%d", port);
err = getaddrinfo(hostname, port_str, &hints, &addrs);
if (err != 0)
{
fprintf(stderr, "%s: %s\n", hostname, gai_strerror(err));
}
for(struct addrinfo *addr = addrs; addr != NULL; addr = addr->ai_next)
{
sd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (sd == -1)
{
err = errno;
break; // if using AF_UNSPEC above instead of AF_INET/6 specifically,
// replace this 'break' with 'continue' instead, as the 'ai_family'
// may be different on the next iteration...
}
if (connect(sd, addr->ai_addr, addr->ai_addrlen) == 0)
break;
err = errno;
close(sd);
sd = -1;
}
freeaddrinfo(addrs);
if (sd == -1)
{
fprintf(stderr, "%s: %s\n", hostname, strerror(err));
}
return sd;
}
int SocketConnect(const char *cpIpAddress, int iPort) {
unsigned char ucBuffer[sizeof(struct in6_addr)];
if(inet_pton(AF_INET, cpIpAddress, ucBuffer) <= 0) {
printf("IP address %s cannot be converted.\n", cpIpAddress);
return -1;
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(iPort);
server_addr.sin_addr = *((struct in_addr *) ucBuffer);
int iSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(iSocket < 0) {
printf("Cannot create socket. Error %i errno %i.\n", iSocket, errno);
return iSocket;
}
// 3 secs receive timeout setup
struct timeval tv;
tv.tv_sec = 3;
tv.tv_usec = 0;
setsockopt(iSocket, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *) &tv, sizeof(struct timeval));
// send time out should never occur on normal OS configurations but just in case set the timeout to 5 seconds
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(iSocket, SOL_SOCKET, SO_SNDTIMEO, (struct timeval *) &tv, sizeof(struct timeval));
int enable = 1;
setsockopt(iSocket, IPPROTO_TCP, TCP_NODELAY, (char *) &enable, sizeof(enable));
// wait 3 seconds for connection to get ready
int iRetries = 3;
if(connect(iSocket, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) < 0) {
printf("Cannot connect to server. errno %i.\n", errno);
close(iSocket);
return -1;
}
return iSocket;
}
void SocketClose(int iSocket)
{
// sanity check
if(iSocket >= 0) {
shutdown(iSocket, SHUT_RD);
close(iSocket);
}
}
int SocketSendData(int iSocket, const unsigned char * ucBuffer, int iLength)
{
// sanity check
if(iSocket < 0) {
return iSocket;
}
int iSentBytes = 0;
while(iLength)
{
long result = send(iSocket, ucBuffer, iLength, 0);
if(result <= 0) {
return -1;
}
iSentBytes += result;
ucBuffer += result;
iLength -= result;
}
return iSentBytes;
}
long SocketRecvData(int iSocket, unsigned char * ucBuffer, int iLength)
{
// sanity check
if(iSocket < 0) {
return iSocket;
}
return recv(iSocket, ucBuffer, iLength, 0);
}