sockets programming

This is a discussion on sockets programming within the Networking/Device Communication forums, part of the General Programming Boards category; hi i have a small problem this is the code i was given for creating a socket Code: if (argc ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Unhappy sockets programming

    hi i have a small problem this is the code i was given for creating a socket

    Code:
    if (argc != 2)     /* Test for correct number of arguments */
        {
            fprintf(stderr, "Usage:  %s <Server Port>\n", argv[0]);
            exit(1);
        }
    
        echoServPort = atoi(argv[1]);  /* First arg:  local port */
    
    
    
        /* Create socket for incoming connections */
        if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
            DieWithError("socket() failed");
          
        /* Construct local address structure */
        memset(&echoServAddr, 0, sizeof(echoServAddr));   /* Zero out structure */
        echoServAddr.sin_family = AF_INET;                /* Internet address family */
        echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
        echoServAddr.sin_port = htons(echoServPort);      /* Local port */
    
        /* Bind to the local address */
        if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
            DieWithError("bind() failed");
    
        /* Mark the socket so it will listen for incoming connections */
        if (listen(servSock, MAXPENDING) < 0)
            DieWithError("listen() failed");
    
        for (;;) /* Run forever */
        {
            /* Set the size of the in-out parameter */
            clntLen = sizeof(echoClntAddr);
    
            /* Wait for a client to connect */
            if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, 
                                   &clntLen)) < 0)
                DieWithError("accept() failed");
    
            /* clntSock is connected to a client! */
    
            printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
    
            HandleTCPClient(clntSock);
        }
        /* NOT REACHED */
    }
    this will ask a user for ip address, socket and message and send to a client (though im really not 100% sure how)

    this is the bit i need to change if i can
    Code:
        if (argc != 2)     /* Test for correct number of arguments */
        {
            fprintf(stderr, "Usage:  %s <Server Port>\n", argv[0]);
            exit(1);
        }
    you see i dont want it to ask for addresses, ports and messages i would like it to ask for a client name eg "R1" then conect to a predeteurmend ipaddress and port and then ask for a string (open or close) to be sent across the socket

    can any body tell me how to do this and also can i put the names and ip addresses in an array

    And finaly im i barking up the wrong tree
    hope you can help

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    That's code for a basic TCP-based server. It's not going to send anything. It's going to wait for clients to connect to the port on which it's listening, then do whatever HandleTCPClient() does with the socket it's provided.

    Here one good site for sockets development
    And here's another

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    on a different note

    hi a different problem i need the server to initiate the connection with the server all the code ive seen has the client conect to the server how to i do this

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    so the server initiating connection will work as a client in the particular session
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    k sorry i dont quite understand this bu im tring

    will this work

    have all ten of my clients keep trying to set up a connection and have my server only alow the connection to the one it needs to send a messasge to ?

    that way i can keep sending messages to different clients using a simple loop

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by kaijuu View Post
    have all ten of my clients keep trying to set up a connection and have my server only alow the connection to the one it needs to send a messasge to ?
    How does it know which client is the one it needs? I assume you would determine this by checking the client IP address. Ignoring the fact that this is technically flawed in a NAT environment, it's also impossible, because the only way to get the client address is to accept() the connection. So you'd be sitting in a loop repeatedly accept()'ing, checking the IP, and close()'ing the clients you don't want.

    No offense, but I think that sucks

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Unhappy

    ok i better explain what im tring to do here

    i have 10 clients and 1 server and im using sockets to connect them

    imagin them like fingers.

    my server asks which client the user would like to connect to and when this string is entered for example right 1, so the server will read a txt file to get the ip address for right 1.

    the server will then connect to the ip address for right 1, and then ask the user for a command (open or close), which will be sent to the client.

    the client will then open a small picture on to the client screen (a finger)

    this is all done in a loop with errors

    however i also need the client to connect to the server to send an ow message this will tell the server to send a close message to all or the right hand or left one (small loop)

    i keep having problems with this and its genraly bothering me

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by kaijuu View Post
    my server asks which client the user would like to connect to
    By definition, clients connect to servers, not vice versa. So this already makes no sense. If your "server" process A is connecting to a "client" B, then A is actually the client and B is the server.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    so can you sugesst a way to get similer results in a different way then

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    don't assume that 1 is server and other 10 are clients. let them all be both (client and server).

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, 06: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, 10:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21