2 * This code is free software; you can redistribute it and/or modify it under
3 * the terms of the new BSD License.
5 * Copyright (c) 2009, Sebastian Staudt
11 #import "SteamCondenserException.h"
15 @implementation TCPSocket
17 -(void) connectWithHost:(NSHost*) aHost andPort:(unsigned short) aPort {
21 struct sockaddr_in remoteSocket;
22 remoteSocket.sin_family = AF_INET;
23 remoteSocket.sin_port = htons(remotePort);
25 NSEnumerator* addressEnumerator = [[remoteHost addresses] objectEnumerator];
26 while(address = [addressEnumerator nextObject]) {
27 if(inet_pton(AF_INET, [address cStringUsingEncoding:NSASCIIStringEncoding], &remoteSocket.sin_addr) == 1) {
32 fdsocket = socket(AF_INET, SOCK_STREAM, 0);
34 @throw [[SteamCondenserException alloc] initWithMessage:[NSString stringWithFormat:@"Error creating socket: %s", strerror(errno)]];
37 if(connect(fdsocket, (struct sockaddr*) &remoteSocket, sizeof(remoteSocket)) < 0) {
38 @throw [[SteamCondenserException alloc] initWithMessage:[NSString stringWithFormat:@"Error connecting socket: %s", strerror(errno)]];
42 fcntl(fdsocket, F_SETFL, O_NONBLOCK);
45 fcntl(fdsocket, F_SETFL, 0);