Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

shtuceron

macrumors newbie
Original poster
Dec 22, 2009
16
0
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
 
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.
 
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.
 
find in web, what matlab is only client, and may be that i have failure?

how you think?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.