3 * Copyright (c) 2002 - 2007, Blue Static <http://www.bluestatic.org>
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 #import "SocketWrapper.h"
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 @implementation SocketWrapper
27 * Initializes the socket wrapper with a host and port
29 - (id)initWithPort
: (int)port
31 if (self = [super init
])
33 // create an INET socket that we'll be listen()ing on
34 int socketOpen
= socket(PF_INET
, SOCK_STREAM
, 0);
36 // create our address given the port
37 struct sockaddr_in address
;
38 address.sin_family
= AF_INET
;
39 address.sin_port
= htons(port
);
40 address.sin_addr.s_addr
= htonl(INADDR_ANY
);
41 memset(address.sin_zero
, '\0', sizeof(address.sin_zero
));
43 // bind the socket... and don't give up until we've tried for a while
45 while (bind(socketOpen
, (struct sockaddr
*)&address
, sizeof(address
)) < 0)
50 NSLog(@
"giving up now");
53 NSLog(@
"couldn't bind to the socket... trying again in 5");
58 // now we just have to keep our ears open
59 if (listen(socketOpen
, 0) == -1)
61 NSLog(@
"listen failed");
65 // accept a connection
66 struct sockaddr_in remoteAddress
;
67 socklen_t remoteAddressLen
= sizeof(remoteAddress
);
68 _socket
= accept(socketOpen
, (struct sockaddr
*)&remoteAddress
, &remoteAddressLen
);
72 NSLog(@
"could not accept() the socket");
76 // we're done listening now that we have a connection
83 * Close our socket and clean up anything else
93 * Reads from the socket and returns the result as a NSString (because it's always going to be XML). Be aware
94 * that the underlying socket recv() call will *wait* for the server to send a message, so be sure that this
95 * is used either in a threaded environment so the interface does not hang, or when you *know* the server
96 * will return something (which we almost always do).
98 * Data string returned is autorelease'd
100 - (NSString
*)receive
105 // do our initial recv() call to get (hopefully) all the data and the lengh of the packet
106 int recvd
= recv(_socket
, &buffer
, sizeof(buffer
), 0);
108 // the length of the packet
109 // packet is formatted in len<null>packet
110 int length
= atoi(buffer
);
112 // take the received data and put it into an NSData
113 NSMutableData
*data
= [NSMutableData data
];
115 // strip the length from the packet, and clear the null byte then add it to the NSData
116 for (int i
= sizeof(length
) - 1; i
>= 0; i
--)
120 [data appendBytes
: buffer length
: recvd
];
122 // check if we have a partial packet
123 if (length
+ sizeof(length
) > sizeof(buffer
))
125 while (recvd
< length
)
127 int latest
= recv(_socket
, &buffer
, sizeof(buffer
), 0);
130 NSLog(@
"socket closed or error");
132 [data appendBytes
: buffer length
: latest
];
137 // convert the NSData into a NSString
138 return [[[NSString alloc
] initWithData
: data encoding
: NSUTF8StringEncoding
] autorelease
];
142 * Sends a given NSString over the socket
144 - (void)send
: (NSString
*)data
146 data
= [NSString stringWithFormat
: @
"%@\0", data
];
147 int sent
= send(_socket
, [data UTF8String
], [data length
], 0);
150 NSLog(@
"error in sending");
152 if (sent
< [data length
])
154 // TODO - do we really need to worry about partial sends with the lenght of our commands?
155 NSLog(@
"FAIL: only partial packet was sent; sent %d bytes", sent
);