Thread: error connecting with sockets

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    error connecting with sockets

    Hello,
    i made client and server applications, using sockets, to connect to each other in same machine. It works in my unversity but when i use them at home it doesnt work.

    I open the server app and it waits for client to connect, when i open client, server exits with error message. The error is in the function accept()... and its something like "Invalid Argument".

    I dont have router or firewall... using ubuntu 7.10... is this related to my operational system blocking the connection? or i can fix it in my program?

    Here is the code for server/client

    ./server
    ./client localhost

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #define SERVER_PORT 5432
    #define MAX_PENDING 5
    #define MAX_LINE 256
    
    int main() {
        struct sockaddr_in sin;
        char buf[MAX_LINE];
        int len;
        int s, new_s;
        /* build address data structure */
        bzero((char *)&sin, sizeof(sin));
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr = INADDR_ANY;
        sin.sin_port = htons(SERVER_PORT);
    
        /* setup passive open */
        if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
            perror("simplex-talk: socket");
            exit(1);
        }
        if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
            perror("simplex-talk: bind");
            exit(1);
        }
        listen(s, MAX_PENDING);
    
        /* wait for connection, then receive and print text */
        while(1) {
            if ((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0){
                perror("simplex-talk: accept");
                exit(1);
            }
            while (len = recv(new_s, buf, sizeof(buf), 0))
                fputs(buf, stdout);
    
            close(new_s);
        }
    }
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    #define SERVER_PORT 5432
    #define MAX_LINE 256
    
    int main(int argc, char * argv[]) {
        FILE *fp;
        struct hostent *hp;
        struct sockaddr_in sin;
        char *host;
        char buf[MAX_LINE];
        int s;
        int len;
    
        if (argc==2) {
            host = argv[1];
        }
        else {
            fprintf(stderr, "usage: simplex-talk host\n");
            exit(1);
        }
        /* translate host name into peer's IP address */
        hp = gethostbyname(host);
        if (!hp) {
            fprintf(stderr, "simplex-talk: unknown host: &#37;s\n", host);
            exit(1);
        }
        /* build address data structure */
        bzero((char *)&sin, sizeof(sin));
        sin.sin_family = AF_INET;
        bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
        sin.sin_port = htons(SERVER_PORT);
        /* active open */
        if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
            perror("simplex-talk: socket");
            exit(1);
        }
        if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
            perror("simplex-talk: connect");
            close(s);
            exit(1);
        }
        /* main loop: get and send lines of text */
        while (fgets(buf, sizeof(buf), stdin)) {
            buf[MAX_LINE-1] = '\0';
            len = strlen(buf) + 1;
            send(s, buf, len, 0);
        }
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Where do you set the len argument you're passing to accept?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    hm... accept will write in the variable len so i only need to declare it an int... right?

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    From here:
    The addrlen argument is a value-result argument: it should initially contain the size of the structure pointed to by addr; on return it will contain the actual length (in bytes) of the address returned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. Cross platform sockets
    By zacs7 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-27-2007, 05:16 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM