TCP Socket help please?!!

Discussion in 'Mac Programming' started by kevinrichardsuk, May 19, 2008.

  1. kevinrichardsuk macrumors member

    Joined:
    May 19, 2008
    #1
    Hi everyone,

    Been pulling my hair out for a week or so over this one.

    I'm from a VB background and am looking to write iPhone apps, currently bought 4 obj-C books and am learning slowly but surely..

    I'm looking to open a socket to a fixed IP address and port, and send and recieve short hexadecimal messages. I can't open a port though and it is very frustrating!!

    I have the iPhone wiTap example program but can't 'read' what is actually going on internally, although I believe it uses Bonjour or similar to connect and hence I'm kind of wasting my time. I've been trying to use the "serverWithAddress" function of TCPConnection but am being warned that it may not respond..

    Can anyone knock up a quick example showing a socket being created to a fixed IP address and port, some data being sent and some being recieved? It'll come in handy, more for learning than this task!!

    Thanks for any responses!
     
  2. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    You should be able to look at any one of a hundred GNU socket tutorials.

    Here's one from http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html

    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    #define PORT		5555
    #define MESSAGE		"Yow!!! Are we having fun yet?!?"
    #define SERVERHOST 	"churchy.gnu.ai.mit.edu"
    
    void 
    write_to_server (int filedes)
    {
      int nbytes;
    
      nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
      if (nbytes < 0)
        {
          perror ("write");
          exit (EXIT_FAILURE);
        }
    }
    
    
    int
    main (void)
    {
      extern void init_sockaddr (struct sockaddr_in *name,
    			     const char *hostname, unsigned short int port);
      int sock;
      struct sockaddr_in servername;
    
      /* Create the socket.   */
      sock = socket (PF_INET, SOCK_STREAM, 0);
      if (sock < 0)
        {
          perror ("socket (client)");
          exit (EXIT_FAILURE);
        }
    
      /* Connect to the server.   */
      init_sockaddr (&servername, SERVERHOST, PORT);
      if (0 > connect (sock,
    		   (struct sockaddr *) &servername,
    		   sizeof (servername)))
        {
          perror ("connect (client)");
          exit (EXIT_FAILURE);
        }
    
      /* Send data to the server.   */
      write_to_server (sock);
      close (sock);
      exit (EXIT_SUCCESS);
    }
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    void 
    init_sockaddr (struct sockaddr_in *name,
    	       const char *hostname, unsigned short int port)
    {
      struct hostent *hostinfo;
    
      name->sin_family = AF_INET;
      name->sin_port = htons (port);
      hostinfo = gethostbyname (serverhost);
      if (hostinfo == NULL) 
        {
          fprintf (stderr, "Unknown host %s.\n", hostname);
          exit (EXIT_FAILURE);
        }
      name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
    }
    
    
     
  3. kevinrichardsuk thread starter macrumors member

    Joined:
    May 19, 2008
    #3
    Thanks for the help.

    I've been trying to get this going today using CFNetwork (I now understand there are about 10 different ways to do this just to further confuse)

    Anyone have any experience with CFNetwork? Thanks again.
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    Probably easier just to use BSD sockets as it is better documented on the web and in books. Any Unix programming book will give you the fundamentals.
     
  5. kevinrichardsuk thread starter macrumors member

    Joined:
    May 19, 2008
    #5
    Ok thanks - I'll have a look at BSD sockets.

    I actually have a socket opened using CFNetwork now, it isn't perfect but I've learned a lot from it..

    Thanks guys.
     
  6. PepsiJon macrumors newbie

    Joined:
    Jun 8, 2008
    Location:
    Forest, Virginia
    #6
    iPhone TCP Socket

    Hi,

    Did you ever get your TCP Socket for the iPhone SDK working?

    I need to do the same thing and was having the same problem trying to get started as I saw in your post. I'd appreciate any info you may be able to share to help me get started. I just need a basic Socket connection to a port / address and send some simple binary data.


    Thanks!
     
  7. kevinrichardsuk thread starter macrumors member

    Joined:
    May 19, 2008
    #7
    PepsiJon,

    I neatly avoided the issue and moved onto other (more documented) components of the program.

    I have dodged the issue long enough though, I'll have to get the TCP stuff running this week at some point.

    Sorry.
     
  8. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #8
    TCP programming is extremely well documented. Any site describing BSD sockets will give more than enough information. This is the document I learnt from though:

    http://beej.us/guide/bgnet/output/print/bgnet_A4.pdf
     
  9. kevinrichardsuk thread starter macrumors member

    Joined:
    May 19, 2008
    #9
    I had a look at BSD sockets and, yes they are well documented.

    I'm using CFNetwork though, which isn't particularly well documented. I have parts of it running though, and it's been a good learning curve.

    Your linked doc looks useful though!
     
  10. PepsiJon macrumors newbie

    Joined:
    Jun 8, 2008
    Location:
    Forest, Virginia
    #10
    If you have some sample snips of code you could share (network/socket), I could use a jump start. You can send off list to jon at castlestudios.com


    Thanks!
     

Share This Page