Jump to content

Server socket sample help


faggus
 Share

1 post in this topic

Recommended Posts

i'm having problems with my simple server socket project (no, it's not a school project). when i build this and run it, i don't get any text in the textfields like i'm supposed to, the program just hangs. even with the debugging program it hangs without any errors as well. here's what i got so far:

 

 

SocketServer.h

 

#import <Cocoa/Cocoa.h>

#import <sys/socket.h>

#import <netinet/in.h>

#include <arpa/inet.h>

 

 

 

@interface SocketServer : NSObject {

 

int serverSocket, sSocket;

struct sockaddr_in socAddress;

int addressSize;

int intPortNumber;

 

 

IBOutlet NSTextField *txtReceive;

IBOutlet NSTextField *txtSend;

IBOutlet NSTextField *portNumber;

 

 

}

 

-(IBAction)openServerSocket:(id)sender;

-(IBAction)closeServerSocket:(id)sender;

-(IBAction)sendMessage:(id)sender;

 

@end

 

 

 

SocketServer.m

 

 

#import "SocketServer.h"

#import <sys/socket.h>

#import <netinet/in.h>

#include <arpa/inet.h>

#import <sys/types.h>

 

#define QUEUE 5

 

@implementation SocketServer

 

 

-(IBAction)openServerSocket:(id)sender

{

serverSocket = socket (AF_INET, SOCK_STREAM, 0);

addressSize = sizeof (struct sockaddr_in);

 

if(serverSocket < 0)

{

[txtReceive setStringValue:@"Error opening socket"];

}

else

{

[txtReceive setStringValue:@"Opening Socket..."];

}

 

socAddress.sin_addr.s_addr = INADDR_ANY;

socAddress.sin_port=htons(5838);

socAddress.sin_family = AF_INET;

 

if(bind(serverSocket, (struct sockaddr *)&socAddress, sizeof(socAddress)) <0)

{

[txtReceive setStringValue:@"Error binding to port"];

}

 

getsockname(serverSocket, (struct sockaddr *) &socAddress, (socklen_t *) &addressSize);

[txtReceive setStringValue:@"Opened socket"];

 

[txtReceive setStringValue:@"Creating listening queue"];

 

if(listen(serverSocket, QUEUE) <0)

{

[txtReceive setStringValue:@"Error creating listening queue"];

}

 

for (;:D

{

sSocket = accept(serverSocket, (struct sockaddr*)&socAddress, (socklen_t *)&addressSize);

 

}

}

 

 

 

yes, i read beej's guide to network programming and a few other socket explanations. i can't get why it won't open a socket. thanks

Link to comment
Share on other sites

 Share

×
×
  • Create New...