Why isn't this working? (sockets)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using namespace std;
using namespace sf;

void main ()
{
	UdpSocket socket;
	socket.setBlocking(false);
        unsigned short const testport= 32701;

	char conn;
	cin>>conn;
	if (conn=='s')
	{
               unsigned int mxmsgsize=2056;
	       socket.bind(testport);
	       char buffer[mxmsgsize];
	       size_t received=0;
	       IpAddress sender;
	       unsigned short const senderport;
	       while (true)
	      {
                     socket.receive(buffer, sizeof(buffer), received, sender, senderport);
	             if (received>1)
	             cout<<buffer<<"   ["<<received<<']'<<endl;
              }
	}
	else
	{
	     //socket.bind(32704);
	       string message= "connected to "+IpAddress::getPublicAddress().toString();
	       while (true)
	       {
                      socket.send(message.c_str(), message.size()+1, "xx.xxx.xxx.xx", testport);
               }
        }
}

"xx.xxx.xxx.xx" is the public IP of my computer, the computer sending the message has a different public IP. What am I doing wrong?
Last edited on
bump
Have you gotten this working locally yet?
Yes
If the receiver is behind a router he may need to forward the port.
Thanks. Not sure how to do that though..
Many routers have a page you can connect to t log in and change settings. For most netgear routers, it's routerlogin.net - the user name is usually admin and the default password is generally on the router itself written down for you.
What socket library are you using? Are you allowed to rebind a socket?
@kbw it looks like he's using SFML sockets
Got it.

There are a number of things wrong with the program.
1. The indentation is not correct, making errors difficult to spot.
2. Only the server should bind to a port (so a client can find it).
3. You haven't specified what network the server listens on, I presume it's defaulting to all (INADDR_ANY), but you need to be clear on what's being used.
4. You should use the loopback address (localhost) to get your communications working then test against other addresses that your computer may have.
5. Love the magic numbers: 2056, 32701, 32704. What do they mean?
Thanks, fixed the indentation.
Right, but I commented out socket.bind(32704);, still not working.
How would I determine that?
The program works when I replace public IP with localhost or local IP.
Port 32701 and 32704 aren't reserved for anything according to wikipedia, and I just used them because I had already added a rule for ports 32700-32750 to my firewall. As for 2056.. I have no idea.
Last edited on
DrZoidberg wrote:
Port 32701 and 32704 aren't reserved for anything according to wikipedia, and I just used them because I had already added a rule for ports 32700-32750 to my firewall. As for 2056.. I have no idea.
You generally don't have to worry about that list on Wikipedia unless you want to develop an application designed to be running all the time like those other applications - e.g. you wouldn't want your commercial software to not work when the user has Skype running. In general you can use any port you want between 1 1025 and 65534.

kbw is saying though that you don't explicitly explain anywhere what those numbers are for. You can fix it simply by writing e.g.
1
2
3
4
5
unsigned short const MyGamePort = 32701;

//...

socket.bind(MyGamePort);
Last edited on
Oh, all right that makes sense. Thanks L B, fixed that issue as well.
LB wrote:
In general you can use any port you want between 1 and 65534.
You should definitely not use any port between 0 and 1024.
Wasn't aware, fixed post. I only knew 0 and 65535 were reserved.
Last edited on
Topic archived. No new replies allowed.