无法从套接字读写

时间:2012-10-14 13:44:59

标签: objective-c sockets

任何人都可以告诉我为什么我无法阅读或写作......

int main (int argc, const char * argv[]) {
    char                         sendbuffer[1024]= "hello world"; 
    char                         recvbuffer[1024]= ""; 
    struct sockaddr_in      sin; 
    struct hostent           *host; 
    int                           s; 

    host = gethostbyname("localhost");  
    //84.108.152.212
    memcpy(&(sin.sin_addr), host->h_addr,host->h_length); 
    sin.sin_family = host->h_addrtype; 
    sin.sin_port = htons(4444);
    // Create socket port 888
    s = socket(AF_INET, SOCK_STREAM,0);
    // 1. Block for server accept 
    connect(s,  (struct sockaddr*)&sin,sizeof(sin));     
    // 2. Send "Hello world"       
    write(s, recvbuffer, sizeof(recvbuffer));
    read(s,sendbuffer,sizeof(sendbuffer));
    // Print received data
    NSLog(@"Received %s\n",recvbuffer); 

    close(s);

    return 0;
}

0 个答案:

没有答案