New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

help with sockets


Post Reply New Thread Subscribe

 
Thread Tools
faggus

 
Member Since: Sep 15, 2008
Posts: 1
faggus is on a distinguished road

faggus is offline
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)openServerSocketid)sender;
-(IBAction)closeServerSocketid)sender;
-(IBAction)sendMessageid)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)openServerSocketid)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 (;
{
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
QUOTE Thanks
Del

 
Member Since: Dec 24, 2006
Location: N. Ireland
Posts: 901
Del has a spectacular aura about
Mac Specs: Mac Pro 2xQuad core 2.8GHZ

Del is offline
I have reposted this for the original poster this time using the code tags for clarity

Code:
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 (;;)
	{
		sSocket = accept(serverSocket, (struct sockaddr*)&socAddress, (socklen_t *)&addressSize);
		
	}
}
QUOTE Thanks

Post Reply New Thread Subscribe


« Does starting Terminal.app at login open up security risks? | C++ to Terminal Question »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
access to sockets dedub OS X - Development and Darwin 0 06-27-2007 10:28 PM
Not all experiences are bad James Apple Desktops 14 03-16-2005 06:41 PM
The lights coming on but nobodies home.............my G4 is dead! ClaireGriffiths Apple Desktops 3 03-01-2005 09:43 AM

All times are GMT -4. The time now is 07:48 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?