Create a new Header file by going into File->New File->C->Header File. Call it NetworkController.h
Paste this in:
#import "NSObject.h"
@class NSString, NSTimer;
@interface NetworkController : NSObject {
struct __SCDynamicStore *_store;
NSString *_domainName;
unsigned int _waitingForDialToFinish:1;
unsigned int _checkedNetwork:1;
unsigned int _isNetworkUp:1;
unsigned int _isFatPipe:1;
unsigned int _edgeRequested:1;
NSTimer *_notificationTimer;
}
+ (id)sharedInstance;
- (void)dealloc;
- (id)init;
- (BOOL)isNetworkUp;
- (BOOL)isFatPipe;
- (id)domainName;
- (BOOL)isHostReachable:(id)fp8;
- (id)primaryEthernetAddressAsString;
- (void)registerCTServerRunLoopSource;
- (id)IMEI;
- (id)edgeInterfaceName;
- (BOOL)isEdgeUp;
- (void)bringUpEdge;
- (void)keepEdgeUp;
@end
Then make sure you link against the Message.framework by right clicking on your Target, going to Info, pressing Add, and selecting message.framework.
Then do:
[[NetworkController sharedInstance] keepEdgeUp];
[[NetworkController sharedInstance] bringEdgeUp];
Socket.m (custom Class I made for wrapping BSD sockets)
- (id)initWithUnixSocket:(int)socket{
self = [super init];
if (self){
sock = socket;
}
return self;
}
- (int)createWithDomain:(int)domain Type:(int)type Protocol:(int)protocol{
sock = socket(domain, type, protocol);
if (sock == -1){
return ERROR;
}
return SUCCESS;
}
- (int)connectToAddress:(const char *)address onPort:(unsigned int)port{
struct sockaddr_in data;
bzero(&data, sizeof(data));
data.sin_family = AF_INET;
data.sin_port = htons(port);
data.sin_addr.s_addr = inet_addr(address);
int result = connect(sock, (const struct sockaddr *)&data, sizeof(data));
if (result == 0){
return SUCCESS;
}
printf("%i", errno);
return ERROR;
}
- (int)bindOnPort:(unsigned int)port{
struct sockaddr_in data;
bzero(&data, sizeof(data));
data.sin_family = AF_INET;
data.sin_port = htons(port);
data.sin_addr.s_addr = INADDR_ANY;
int result = bind(sock, (const struct sockaddr *)&data, sizeof(data));
if (result == 0){
return SUCCESS;
}
return ERROR;
}
- (int)listenWithBackLog:(int)backLog{
int result = listen(sock, backLog);
if (result == 0){
listeningThread = [[NSThread alloc] initWithTarget:self selector:@selector(listeningThread:) object:nil];
[listeningThread start];
return SUCCESS;
}
return ERROR;
}
- (void)listeningThread:(id)object{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int incomingClient = accept(sock, NULL, NULL);
if (incomingClient != -1){
Socket *incomingSocket = [[Socket alloc] initWithUnixSocket:incomingClient];
incomingSocket.delegate = self.delegate;
if ([delegate respondsToSelector:@selector(connectionAccepted:)]){
[delegate connectionAccepted:incomingSocket];
}
}
[pool release];
}
- (int)sendData:(const char *)data{
int length = strlen(data);
int result = send(sock, data, length, 0);
if (result > 0){
return result;
}
return ERROR;
}
- (void)waitForData{
dataThread = [[NSThread alloc] initWithTarget:self selector:@selector(listenForData:) object:nil];
[dataThread start];
}
- (void)listenForData:(id)object{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BOOL waitingForData = YES;
while (waitingForData){
const char *buffer[1024];
int length = sizeof(buffer);
int result = recv(sock, &buffer, length, 0);
if (result > 0){
if ([delegate respondsToSelector:@selector(dataReceived:)]){
[delegate dataReceived:buffer];
}
}
}
[pool release];
}
- (void)close{
close(sock);
}
Server Code (Uses Socket.m)
if ([server createWithDomain:AF_INET Type:SOCK_STREAM Protocol:0] == SUCCESS){
[statusText setStringValue:STATUS_CREATED_SOCKET];
if ([server bindOnPort:PORT] == SUCCESS){
[statusText setStringValue:STATUS_BOUND];
if ([server listenWithBackLog:5] == SUCCESS){
[statusText setStringValue:STATUS_WAITING_FOR_CONNECTIONS];
}
}
}
Client Code (Uses Socket.m):
if ([client createWithDomain:AF_INET Type:SOCK_STREAM Protocol:0] == SUCCESS){
canConnect = YES;
}
if (connected == NO){
if (canConnect){
if ([client connectToAddress:[ipField.text UTF8String] onPort:PORT] == SUCCESS){
connected = YES;
}
}
}
[client connectToAddress:[ipField.text UTF8String] onPort:PORT] always fails on EDGE, unless a networked program is started before...