Server-client: socket-numbers fails

Started by
9 comments, last by prux 15 years, 12 months ago
Hello. It might be a common-known "error" but I cannot find a sollution. I create the connection-socket every time: connectionSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); There are 2 machines on network: D: 192.168.1.6 with "1728" connectionSocket C: 192.168.1.3 with "1712" connectionSocket thats right. Now connect to server (asynchron). The FC_ACCEPT arrives: connectedSocket = accept (ConnectionSocket, addr,addrlen); the "connectedSocket" tells me who came. Normally it should be "1712" (C is coming) but it is "1716"!!! o.O Its not a big deal yet. The server stores all informations of the clients (its socket, custom datas, size of the custom datas, player name, etc), and some other data can be queried from the socket number (IP address) When an other client connets (let it be B), the server send all clients informations to it. But.... then it sends fail socket numbers. How does the client track down the others informations? All clients must know each other IP addresses. Do I send the IP address directly? If so, when a client wants to send a message to client (yes, not to server) for example its a private chat, how to do this without correct socket?
Advertisement
I am not sure what you're saying, because your terminology seems a little incorrect. The 'socketnumber' is just a file-like object that you can read or write from. You can't use it to identify who is on the other end. The call to accept() will return yet another file-like object which you can read or write from, and which will have nothing to do with the ones that already exist. The actual numbers themselves mean nothing. The way you find out who you're connected to is to read what accept() puts into the 2nd parameter.
A TCP socket (server) binds to an address and port that you specify. Let's say 192.162.1.20:1700.

When a client connects, it uses that IP and port.

When server accept()s a connection, it creates a new socket, and binds it to random port (over 1000, subject to platform specific behavior). On server, this is then the socket that represents a client. Under TCP, each connection requires its own socket.

But as far as client is concerned, it's connected to 192.162.1.20:1700.
Quote:When server accept()s a connection, it creates a new socket, and binds it to random port (over 1000, subject to platform specific behavior). On server, this is then the socket that represents a client. Under TCP, each connection requires its own socket.

But as far as client is concerned, it's connected to 192.162.1.20:1700.


This is not quite correct. It actually binds on the same port at the server. A TCP connection is the 4-tuple of (source ip address, source port, destination ip address, destination port). The destination port does not change when a connection is accepted. The random port comes on the client side, where connect() on an un-bound socket will choose a random, unused port on the client side.
enum Bool { True, False, FileNotFound };
Yes ok now I try to make it easier =)

so a client needs to send a message to other client. The winsock send function needs a socket to send to. This would be the socket of clients, this is why I need them... or Im wrong?
I'm still not sure what you're asking. The first computer speaks to the second via the socket it got from socket(). The second computer replies to the first via the socket it got from accept(). You don't ever really need to look at what number those 'sockets' are, nor should you expect these numbers to correspond across multiple computers. What uniquely identifies the connection is the remote host and remote port.
ok. Theres a "S" server and 4 clients (A,B,C,D). A wants to sent to C. How to do this
- A,B,C and D connect to S.
- When a client connects, they send a message: "Hello, I am X" (X = A,B,C or D)

Direct connection:
- When A wants to connect to C, it sends message to S, saying: "Give me address of C"
- Server searches its clients looking for C. If it finds it, they send the address in reply to A.

- A now uses this address to open a new connection to C
- A and C now talk directly

Indirect connection:
- A sends a message: "Tell C I said: 'Hello'"
- S searches its clients looking for C. If it finds it, it sends a message to them: "A says: 'Hello'"
wow I SEE THAT IS what I was looking for. But then C must be a sever too, must not?
Quote:Original post by prux
wow I SEE THAT IS what I was looking for. But then C must be a sever too, must not?


If you're doing peer-to-peer, then you have equivalent nodes. Each one is a server and client.

This topic is closed to new replies.

Advertisement