Results 1 to 2 of 2
  1. #1


    Member Since
    Sep 15, 2008
    Posts
    1
    help with sockets
    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

  2. #2


    Member Since
    Dec 24, 2006
    Location
    N. Ireland
    Posts
    901
    Specs:
    Mac Pro 2xQuad core 2.8GHZ
    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);
    		
    	}
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Java Mac LINUX connection using sockets
    By shellback3 in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 07-30-2012, 12:31 PM
  2. Question about Input/Output Streams (sockets)
    By mjparme in forum OS X - Development and Darwin
    Replies: 2
    Last Post: 01-08-2009, 09:33 AM
  3. Accessing Front USB sockets to replace.
    By mikeymac in forum Apple Desktops
    Replies: 1
    Last Post: 11-23-2008, 04:07 PM
  4. access to sockets
    By dedub in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 06-27-2007, 11:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •