Socket

Discussion in 'Mac Programming' started by shtuceron, Jan 7, 2011.

  1. shtuceron macrumors newbie

    Joined:
    Dec 22, 2009
    #1
    Hello All!

    I try realize socket on mac os, but have fail, please if you know how only make receive data throw localhost write.

    Thanks
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Being UNIX (and BSD-derived), Mac OS X has the standard BSD socket functions such as accept, bind, listen, send, recv, etc.

    Alternatively you could use CFSocket from Core Foundation. Have a look at the CFSocket reference in the developer docs.

    Although CFNetwork is higher-level abstraction which might be better suited to your purposes. Check out the CFNetwork Programming Guide.

    The CocoaHTTPServer example, particular the TCPServer in it, gives a concreate example of using CFNetwork.
     
  3. shtuceron thread starter macrumors newbie

    Joined:
    Dec 22, 2009
    #3
    jiminaus thank you wary match, i am choose BSD socket and realize it, i can send and receive data in my program, but i want to receive data from other program (matlab), but i can not do this.

    in matlab i'm use this script: (with comment)

    Code:
    % Create TCP/IP object 't'. Specify server machine and port number. 
    t = tcpip('localhost', 4012);
    
    % Set size of receiving buffer, if needed. 
    set(t, 'InputBufferSize', 30000);
    
    % Open connection to the server. 
    fopen(t);
    
    % get(t, 'BytesAvailable')
    
    while (1 == 1)
        fwrite(t, 'a');
    end
    
    % Disconnect and clean up the server connection. 
    fclose(t); 
    delete(t); 
    clear t;
    
    in objective c this:

    Code:
            struct sockaddr_in localAddr, servAddr;
    	
    	struct hostent *h;
    	
    	int sd, rc, i;
    	
    	h = gethostbyname("localhost");
    	
    	servAddr.sin_family = h->h_addrtype;
    	
    	memcpy((char *)&servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    	
    	servAddr.sin_port = htons(4012);
    	
    	sd = socket(PF_INET, SOCK_STREAM, 0);
    	
    	localAddr.sin_family = AF_INET;
    	localAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    	localAddr.sin_port = htons(4012);
    	
    	rc = bind(sd, (struct sockaddr *)&localAddr, sizeof(localAddr));
    	
    	rc = connect(sd, (struct sockaddr *)&servAddr, sizeof(servAddr));
    	
    	char recvbuf[1];
    	
    	rc = recv(sd, recvbuf, 1, 0);
    
    i wait a lot of time but not receive any data, may be you help me again?

    Thanks.
     
  4. shtuceron thread starter macrumors newbie

    Joined:
    Dec 22, 2009
    #4
    find in web, what matlab is only client, and may be that i have failure?

    how you think?
     

Share This Page