Skip navigation.
 
mlProblem with bsd sockets
FROM : Quentin Mathé
DATE : Fri Jan 03 20:22:07 2003

When I run the code below in Cocoa application with Project Builder, I
get an error : socket already connected everytime, even when my
internet connection is turned off. I have found no explanation about
this problem in the book Unix network programming I hope someone can
help me

    int sockfd; // socket file descriptor
    struct sockaddr_in serverAddress;
    char buffer[201];
    int n;

    if (sockfd = socket(AF_INET, SOCK_STREAM, 0) < 0) {
        perror("socket");
        exit(1);
    }

    bzero(&serverAddress, sizeof(serverAddress));
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(80);

    inet_pton(AF_INET, "17.112.152.32", &serverAddress.sin_addr);

    if (connect(sockfd, (struct sockaddr*)&serverAddress,
sizeof(serverAddress)) < 0) {
        perror("connect");
    }

    while (n = read(sockfd, buffer, 200) > 0) {
        buffer[n] = 0;
        printf(buffer);
    }

    if (n < 0) {
        perror("read");
    }

    close(sockfd);

--
Quentin Mathi
<email_removed>
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
mlProblem with bsd sockets Quentin Mathé Jan 3, 20:22
mlRe: Problem with bsd sockets Robert Tillyard Jan 3, 20:44