can't get server socket working

C

cerr

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!
 
J

Jorgen Grahn

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
 
W

woodbrian77

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
 
S

Sergio

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.
 
J

Jorgen Grahn

....
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
 
I

Ian Collins

Jorgen said:
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 :)
 
M

Melzzzzz

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.
 
J

Jorgen Grahn

Jorgen Grahn wrote: ....

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
 
S

Sergio

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top