• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Socket communications
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Socket communications


  • Subject: Socket communications
  • From: "Maryanna Rogers" <email@hidden>
  • Date: Wed, 8 Aug 2007 19:41:56 -0400

Hi again,

I've got two simple applications that I'm trying to get to talk over a
socket. After much digging, it appears relatively straightforward.
Sadly, I can't get the client to connect. ("a.b.c.d" is the ip of the
server).

The server does this:

- (void) setupService {
    ...
    sockfd = socket( AF_INET, SOCK_STREAM, 0 );
    memset( &addr, sizeof(struct sockaddr_in));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("a.b.c.d");
    addr.sin_port = 3421;
    bind( sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr));
    listen( sockfd, 5 );
    listeningSocket = [[NSFileHandle alloc] initWithFileDescriptor:sockfd];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(handleIncomingConnection:)
name:NSFileHandleConnectionAcceptedNotification object:nil];

    // Accept connections in background and notify
    [listeningSocket acceptConnectionInBackgroundAndNotify];
}

And the client attempts to connect like this:

- (void)connect {
    int server_sock;
    struct sockaddr_in server_addr;
    ...
    server_sock = socket(AF_INET, SOCK_STREAM, 0);
    memset(&server_addr, 0,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = 3421;
    server_addr.sin_addr.s_addr = inet_addr("a.b.c.d");
    connect(server_sock, (struct sockaddr *) &server_addr,
sizeof(server_addr));
    NSFileHandle *handle = [[NSFileHandle alloc]
initWithFileDescriptor:server_sock];
}

I don't need to do manage the run loop for the server or anything, do
I? The NSNotificationCenter should call me back when a client
connects, right? Is there anything wrong with the above approach?

Thanks!

~m
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Socket communications
      • From: Andrew Farmer <email@hidden>
  • Prev by Date: mysterious problem with first responder
  • Next by Date: RE: [OT] Re: Cocoa and messages to nil, revisited
  • Previous by thread: mysterious problem with first responder
  • Next by thread: Re: Socket communications
  • Index(es):
    • Date
    • Thread