socket question (listen, accept)
-
I have a MFC (server) application that works with sockets. It should do the following: 1. Waits for an incomming client connection request (only 1). 2. Accept that connection request. 3. Send something to the client. 4. Receive something from the client. 5. Close the accepted connection. 6. ... go to 1. (wait for incomming client connection request) Only 1 client can send something to the server. So, I tried to imeplement it like this:
WSAStartup(...);
server = socket(...);
bind(server, ...);
listen(server, ...);
client = accept(server, ...);
send(client, ...);
recv(client, ...);
closesocket(client);
...
So... my question! This code runs in a own thread and should never ends (only when the user terminates the app). After ther
closesocket(client);
when I want to re-listen to some incomming connection request, have I to call onlyclient = accept(server,...);
orlisten(server,...); client = accept(server,...);
? What's the wright way:WSAStartup(...);
server = socket(...);
bind(server, ...);
place
while (TRUE) {
here ???listen(server, ...);
or place
while (TRUE) {
here ???client = accept(server, ...);
send(client, ...);
recv(client, ...);
closesocket(client);
} // while (TRUE) ...
`Daniel ;) --------------------------- **Never change a running system!**`
-
I have a MFC (server) application that works with sockets. It should do the following: 1. Waits for an incomming client connection request (only 1). 2. Accept that connection request. 3. Send something to the client. 4. Receive something from the client. 5. Close the accepted connection. 6. ... go to 1. (wait for incomming client connection request) Only 1 client can send something to the server. So, I tried to imeplement it like this:
WSAStartup(...);
server = socket(...);
bind(server, ...);
listen(server, ...);
client = accept(server, ...);
send(client, ...);
recv(client, ...);
closesocket(client);
...
So... my question! This code runs in a own thread and should never ends (only when the user terminates the app). After ther
closesocket(client);
when I want to re-listen to some incomming connection request, have I to call onlyclient = accept(server,...);
orlisten(server,...); client = accept(server,...);
? What's the wright way:WSAStartup(...);
server = socket(...);
bind(server, ...);
place
while (TRUE) {
here ???listen(server, ...);
or place
while (TRUE) {
here ???client = accept(server, ...);
send(client, ...);
recv(client, ...);
closesocket(client);
} // while (TRUE) ...
`Daniel ;) --------------------------- **Never change a running system!**`
The second place, between
listen
andaccept
. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
The second place, between
listen
andaccept
. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloThanks! Daniel ;) --------------------------- Never change a running system!
-
I have a MFC (server) application that works with sockets. It should do the following: 1. Waits for an incomming client connection request (only 1). 2. Accept that connection request. 3. Send something to the client. 4. Receive something from the client. 5. Close the accepted connection. 6. ... go to 1. (wait for incomming client connection request) Only 1 client can send something to the server. So, I tried to imeplement it like this:
WSAStartup(...);
server = socket(...);
bind(server, ...);
listen(server, ...);
client = accept(server, ...);
send(client, ...);
recv(client, ...);
closesocket(client);
...
So... my question! This code runs in a own thread and should never ends (only when the user terminates the app). After ther
closesocket(client);
when I want to re-listen to some incomming connection request, have I to call onlyclient = accept(server,...);
orlisten(server,...); client = accept(server,...);
? What's the wright way:WSAStartup(...);
server = socket(...);
bind(server, ...);
place
while (TRUE) {
here ???listen(server, ...);
or place
while (TRUE) {
here ???client = accept(server, ...);
send(client, ...);
recv(client, ...);
closesocket(client);
} // while (TRUE) ...
`Daniel ;) --------------------------- **Never change a running system!**`
Hi Daniel, the usual way is the following: First, start with CREATE a socket object. Then Call BIND (if you use MFC, CAsyncSocket::Create() will do that for you) After this, set your ServerSocket LISTEN. (and here is your loop...) Then comes the connection handling, that means you have to implement each acceptet connection in a own thread, if you want to have a multi connection server. After accepting (and creating your connection thread) go back. Hope this helps a little.. Cheers, Olli!
Olli Make it idiot proof and someone will make a better idiot......
:suss: :rolleyes: :suss: -
Hi Daniel, the usual way is the following: First, start with CREATE a socket object. Then Call BIND (if you use MFC, CAsyncSocket::Create() will do that for you) After this, set your ServerSocket LISTEN. (and here is your loop...) Then comes the connection handling, that means you have to implement each acceptet connection in a own thread, if you want to have a multi connection server. After accepting (and creating your connection thread) go back. Hope this helps a little.. Cheers, Olli!
Olli Make it idiot proof and someone will make a better idiot......
:suss: :rolleyes: :suss:Thanks, this will help! Daniel ;) --------------------------- Never change a running system!