Add function for connecting to an already-open TCP port
Jeffrey Pfau jeffrey@endrift.com
Tue, 11 Feb 2014 00:19:29 -0800
1 files changed,
21 insertions(+),
0 deletions(-)
jump to
M
src/util/socket.h
→
src/util/socket.h
@@ -52,6 +52,27 @@ }
return sock; } +static inline Socket SocketConnectTCP(int port, uint32_t destinationAddress) { + Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (sock < 0) { + return sock; + } + + struct sockaddr_in bindInfo = { + .sin_family = AF_INET, + .sin_port = htons(port), + .sin_addr = { + .s_addr = htonl(destinationAddress) + } + }; + int err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(struct sockaddr_in)); + if (err) { + close(sock); + return -1; + } + return sock; +} + static inline Socket SocketListen(Socket socket, int queueLength) { return listen(socket, queueLength); }