This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketConnection.cpp
103 lines (87 loc) · 2.68 KB
/
SocketConnection.cpp
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
#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 <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 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)
{
int result = send(iSocket, ucBuffer, iLength, 0);
if(result <= 0) {
return -1;
}
iSentBytes += result;
ucBuffer += result;
iLength -= result;
}
return iSentBytes;
}
int SocketRecvData(int iSocket, unsigned char * ucBuffer, int iLength)
{
// sanity check
if(iSocket < 0) {
return iSocket;
}
return recv(iSocket, ucBuffer, iLength, 0);
}