can't get server socket working

Discussion in 'C++' started by cerr, Oct 29, 2013.

  1. cerr

    cerr Guest

    Hi,

    I'm trying to get a server socket working but for some reason this doesn't allow any connections on 127.0.0.1:1234. What might be wrong here? No errors are being reported:

    #include <iostream>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <string.h>
    #include <stdio.h>

    #define BUF_SIZE 500
    using namespace std;

    int getserversocket(int port);

    int main (void)
    {
    socklen_t peer_addr_len;
    ssize_t nread;
    int sockhandle, s;
    struct sockaddr_storage peer_addr;
    char buf[BUF_SIZE];

    cout << "MyServer 1.0" << endl;
    if (sockhandle = getserversocket(1234) >= 0){
    cout << "Socket created!" << endl;
    } else {
    cout << "Could not bind" << endl;
    }
    while(sockhandle >= 0) {
    peer_addr_len = sizeof(struct sockaddr_storage);
    nread = recvfrom(sockhandle, buf, BUF_SIZE, 0,
    (struct sockaddr *) &peer_addr, &peer_addr_len);
    if (nread == -1)
    continue; /* Ignore failed request */

    char host[NI_MAXHOST], service[NI_MAXSERV];

    s = getnameinfo((struct sockaddr *) &peer_addr,
    peer_addr_len, host, NI_MAXHOST,
    service, NI_MAXSERV, NI_NUMERICSERV);
    if (s == 0)
    cout << "Received "<< (long)nread << "bytes from " << host << ":" << service << endl;
    else
    fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));

    if (sendto(sockhandle, buf, nread, 0,
    (struct sockaddr *) &peer_addr,
    peer_addr_len) != nread)
    cout << "Error sending response" << endl;
    }


    }

    int getserversocket(int port)
    {
    struct addrinfo hints;
    struct addrinfo *result, *rp;
    int addrinfo, sock;
    char port_chr[5];
    sprintf(port_chr,"%d",port);

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
    hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
    hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
    hints.ai_protocol = 0; /* Any protocol */
    hints.ai_canonname = NULL;
    hints.ai_addr = NULL;
    hints.ai_next = NULL;

    addrinfo = getaddrinfo(NULL, port_chr, &hints, &result);
    if (addrinfo != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(addrinfo));
    exit(EXIT_FAILURE);
    }

    for (rp = result; rp != NULL; rp = rp->ai_next) {
    sock = socket(rp->ai_family, rp->ai_socktype,
    rp->ai_protocol);
    if (sock == -1)
    continue;

    if (bind(sock, rp->ai_addr, rp->ai_addrlen) == 0)
    break; /* Success */

    close(sock);
    }
    freeaddrinfo(result); /* No longer needed */

    if (rp == NULL) { /* No address succeeded */
    return -1;
    }
    else
    return sock;
    }

    Any assistance would be appreciated, Thank you!
     
    cerr, Oct 29, 2013
    #1
    1. Advertising

  2. cerr

    Jorgen Grahn Guest

    On Tue, 2013-10-29, cerr wrote:
    > Hi,
    >


    > I'm trying to get a server socket working but for some reason this
    > doesn't allow any connections on 127.0.0.1:1234. What might be wrong
    > here? No errors are being reported:


    [snip socket stuff, no or very little C++]

    > Any assistance would be appreciated, Thank you!


    IME if you're on Unix, the strace(1) utility shows you very quickly
    what goes wrong in simple syscall-related situations like this one.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 29, 2013
    #2
    1. Advertising

  3. cerr

    Guest

    On Tuesday, October 29, 2013 11:31:12 AM UTC-5, cerr wrote:
    > Hi,
    >
    > I'm trying to get a server socket working but for some reason this doesn't allow any connections on 127.0.0.1:1234. What might be wrong here? No errors are being reported:


    > while(sockhandle >= 0) {
    > peer_addr_len = sizeof(struct sockaddr_storage);
    > nread = recvfrom(sockhandle, buf, BUF_SIZE, 0,
    > (struct sockaddr *) &peer_addr, &peer_addr_len);
    > if (nread == -1)
    > continue; /* Ignore failed request */
    >


    I tried running and recvfrom failed repeatedly.

    I have some similar code here:

    http://webEbenezer.net/misc/udp_server.cc
    http://webEbenezer.net/misc/cmwAmbassador.cc

    Those files are available in the archive here:

    http://webEbenezer.net/build_integration.html

    Thanks for asking.


    Brian
    Ebenezer Enterprises - In G-d we trust.
    http://webEbenezer.net
     
    , Oct 29, 2013
    #3
  4. cerr

    Sergio Guest

    On 29/10/2013 17:31, cerr wrote:
    > Hi,
    >
    > I'm trying to get a server socket working but for some
    > reason this doesn't allow any connections on
    > 127.0.0.1:1234. What might be wrong here? No errors
    > are being reported:


    > int getserversocket(int port)
    > {

    [...]
    > char port_chr[5];
    > sprintf(port_chr,"%d",port);

    [...]
    >
    > addrinfo = getaddrinfo(NULL, port_chr, &hints, &result);

    [...]
    > Any assistance would be appreciated, Thank you!


    The getaddrinfo function requires the *service name* as second
    parameter, not the port number (read better the man page). If you want
    to use a port number you cannot use getaddrinfo (this is not its purpose).

    S.
     
    Sergio, Oct 29, 2013
    #4
  5. cerr

    Jorgen Grahn Guest

    On Tue, 2013-10-29, Sergio wrote:
    > On 29/10/2013 17:31, cerr wrote:
    > > Hi,
    > >
    > > I'm trying to get a server socket working but for some

    ....
    > The getaddrinfo function requires the *service name* as second
    > parameter, not the port number (read better the man page). If you want
    > to use a port number you cannot use getaddrinfo (this is not its purpose).


    Well, you /can/ use a port number, but you have to specify it as a
    string. But that's also described in the man page.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 29, 2013
    #5
  6. cerr

    Ian Collins Guest

    Jorgen Grahn wrote:
    > On Tue, 2013-10-29, cerr wrote:
    >> Hi,
    >>

    >
    >> I'm trying to get a server socket working but for some reason this
    >> doesn't allow any connections on 127.0.0.1:1234. What might be wrong
    >> here? No errors are being reported:

    >
    > [snip socket stuff, no or very little C++]
    >
    >> Any assistance would be appreciated, Thank you!

    >
    > IME if you're on Unix, the strace(1) utility shows you very quickly
    > what goes wrong in simple syscall-related situations like this one.


    s/Unix/Linux/

    More advanced Unix systems use dtrace :)

    --
    Ian Collins
     
    Ian Collins, Oct 29, 2013
    #6
  7. cerr

    Melzzzzz Guest

    On Tue, 29 Oct 2013 09:31:12 -0700 (PDT)
    cerr <> wrote:

    > Hi,
    >
    > I'm trying to get a server socket working but for some reason this
    > doesn't allow any connections on 127.0.0.1:1234. What might be wrong
    > here? No errors are being reported:
    >
    > if (sockhandle = getserversocket(1234) >= 0){
    > cout << "Socket created!" << endl;
    > } else {
    > cout << "Could not bind" << endl;
    > }
    >

    This is not what you want.
    What you want is actually this:
    if ((sockhandle = getserversocket(1234)) >= 0){

    Besides that, your program works as intended.

    --
    Sig.
     
    Melzzzzz, Oct 29, 2013
    #7
  8. cerr

    Melzzzzz Guest

    On Tue, 29 Oct 2013 11:53:52 -0500
    Paavo Helde <> wrote:

    > cerr <> wrote in
    > news::
    >
    > > Hi,
    > >
    > > I'm trying to get a server socket working but for some reason this
    > > doesn't allow any connections on 127.0.0.1:1234. What might be wrong
    > > here? No errors are being reported:
    > > hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
    > > hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */

    >
    > Not the best place to ask, this has nothing to do with C++.


    Problem is C++ related... fact that is uses sockets library,
    clobbers real problem ;)

    --
    Sig.
     
    Melzzzzz, Oct 29, 2013
    #8
  9. cerr

    Jorgen Grahn Guest

    On Tue, 2013-10-29, Ian Collins wrote:
    > Jorgen Grahn wrote:

    ....
    >> IME if you're on Unix, the strace(1) utility shows you very quickly
    >> what goes wrong in simple syscall-related situations like this one.

    >
    > s/Unix/Linux/
    >
    > More advanced Unix systems use dtrace :)


    Ah! I always make a point of writing "Unix" instead of "Linux" to
    remind people that the world is that much larger ... but sometimes it
    ends up being misleading instead.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 29, 2013
    #9
  10. cerr

    Sergio Guest

    On 29/10/2013 23:59, Jorgen Grahn wrote:> Well, you /can/ use a port
    number, but you have to specify it as a
    > string. But that's also described in the man page.


    My bad, you are right. So it is me that has to read the man page better :)

    S.
     
    Sergio, Oct 30, 2013
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,874
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    977
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    557
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    674
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    726
Loading...

Share This Page