all repos — mgba @ 8e51ffbf2cd114f17a4c370d45862c3873a8aebe

mGBA Game Boy Advance Emulator

Add function for connecting to an already-open TCP port
Jeffrey Pfau jeffrey@endrift.com
Tue, 11 Feb 2014 00:19:29 -0800
commit

8e51ffbf2cd114f17a4c370d45862c3873a8aebe

parent

4d8a00c18068c49108998077af3d0083c3637e6c

1 files changed, 21 insertions(+), 0 deletions(-)

jump to
M src/util/socket.hsrc/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); }