Accepting incoming socket connections
No one in the multiplayer section would answer this the first time I asked, so here it is.
I am making a MUD that uses winsock32. I know how to make a connection to a remote computer, but on the server side, how do I accept that connection? I know it has something to do with the listen() function, but where/how do I call it? Also, how do I make the socket(s) on the server bind to a port number?
Another thing related to this is parsing the data sent and received by the sockets. On the client end, I have a very nice system for this, but as soon as it enters the Send() function (which I did not make), I have no idea what happens to it. How do I get it to come out on the server as it came out on the client?
Thanks for you help in advance.
www.trak.to/rdp
--------------------
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
Well, for the server the general algorithm is: (assumes tcp)
1. Create the socket
2. Bind() to the address and port on the machine you want (hint- use INADDR_ANY for all ip addresses)
3. listen() on the socket.
4. when listen returns(if blocking), it has a connection waiting, so...
5. accept() it. accept will return a new socket to use in communications with the client.
6. Send & recv on the new socket.
7. closesocket()
8. start at the listen
NOTES: this is single threaded and can only handle one connection at a time, but shows the general gist of it. Also, I don''t have the reference in front for the function params.
If you need some more info let me know. I''m doing a client/server library so it''s fairly fresh in my mind.
--Joe
1. Create the socket
2. Bind() to the address and port on the machine you want (hint- use INADDR_ANY for all ip addresses)
3. listen() on the socket.
4. when listen returns(if blocking), it has a connection waiting, so...
5. accept() it. accept will return a new socket to use in communications with the client.
6. Send & recv on the new socket.
7. closesocket()
8. start at the listen
NOTES: this is single threaded and can only handle one connection at a time, but shows the general gist of it. Also, I don''t have the reference in front for the function params.
If you need some more info let me know. I''m doing a client/server library so it''s fairly fresh in my mind.
--Joe
Joe
-flynboy
-flynboy
That was the best answer I have gotten yet
I am making a MUD, so I *kinda* need to have multiple connections. Could you tell me how to do that? Thanks in advance.
www.trak.to/rdp
I am making a MUD, so I *kinda* need to have multiple connections. Could you tell me how to do that? Thanks in advance.
www.trak.to/rdp
--------------------
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
That''s where the interesting part is.
There are a few different ways to handle it depending on:
1. The OS you''re running under
2. The expected number of connections
3. and so on...
The easiest (to explain, not necessarily implement) is to create a new thread to handle each new client socket. So, after accepting the connection, start a new thread and pass the socket to it, then continue to listen() for more connections.
The other way to is to use non-blocking sockets and the select() function to see which socket is ready to receive data and or connections.
The system I am currently using uses a hybrid. I have a main thread that handles any connections and sending data to clients, and a second thread that uses select() to receive data back from the sockets.
Joe
There are a few different ways to handle it depending on:
1. The OS you''re running under
2. The expected number of connections
3. and so on...
The easiest (to explain, not necessarily implement) is to create a new thread to handle each new client socket. So, after accepting the connection, start a new thread and pass the socket to it, then continue to listen() for more connections.
The other way to is to use non-blocking sockets and the select() function to see which socket is ready to receive data and or connections.
The system I am currently using uses a hybrid. I have a main thread that handles any connections and sending data to clients, and a second thread that uses select() to receive data back from the sockets.
Joe
Joe
-flynboy
-flynboy
The Berkeley Sockets API is portable across most operating systems. In order to handle multiple connections in that you code something like this:
struct sockaddr_in servaddr;
socket listenfd = socket(...);
bind(listenfd, ...);
listen(listenfd, ...);
FD_ZERO(&all_fd);
FD_SET(listenfd, &all_fd);
for (; {
nready = select(maxfd + 1, &all_fd, NULL, NULL, NULL, NULL);
if (FD_ISSET(listenfd, &all_fd) {
client_fd = accept(listenfd, ...)
/* spawn new thread or whatever to handle client */
}
}
With Winsock you can replace the select loop with WSAAsyncSelect. Then you assign a message and a message handler to WSAAsyncSelect.
In Winsock2 you can also use WSAEventSelect, which works by signaling on events rather than windows messages.
struct sockaddr_in servaddr;
socket listenfd = socket(...);
bind(listenfd, ...);
listen(listenfd, ...);
FD_ZERO(&all_fd);
FD_SET(listenfd, &all_fd);
for (; {
nready = select(maxfd + 1, &all_fd, NULL, NULL, NULL, NULL);
if (FD_ISSET(listenfd, &all_fd) {
client_fd = accept(listenfd, ...)
/* spawn new thread or whatever to handle client */
}
}
With Winsock you can replace the select loop with WSAAsyncSelect. Then you assign a message and a message handler to WSAAsyncSelect.
In Winsock2 you can also use WSAEventSelect, which works by signaling on events rather than windows messages.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement