Hey.
I'm now learning how to make simple server-client applications, and I'm having hard time understanding how it's supposed to be so I have a few general questions:
So, basically if I use blocking sockets, is it going to be one connection at a time? Or, am I not understanding something?
So, I have a listening socket that waits for a connections. Then, when a connection is received, I accept it and transfer it to the handling socket for interacting with the client. Now, if my code is now busy interacting with the client, and is for example looping around some client interaction code, then it's not available for the listening socket. Is it, then, not listening at this point, and will return to listen mode when the client disconnects?
Here's some code that I'm playing around with, but my question is not necessarily about this code. It's just a simple server code that I use. I really just want to understand how it's supposed to work.
All answers will be appreciated.
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 37 38 39 40 41 42 43
|
#include "stdafx.h"
#include "winsocks.h"
int addition(int a) {
return a+50;
}
void main() {
long SUCCESSFUL;
WSADATA WinSockData;
WORD DLLVERSION;
int MyPort = 7177;
char num[600],num2[600];
DLLVERSION = MAKEWORD(2,1);
SUCCESSFUL = WSAStartup(DLLVERSION, &WinSockData);
SOCKADDR_IN ADDRESS;
int AddressSize = sizeof(ADDRESS);
SOCKET sock_LISTEN;
SOCKET sock_CONNECTION;
sock_CONNECTION = socket(AF_INET, SOCK_STREAM, NULL);
ADDRESS.sin_addr.s_addr = INADDR_ANY;
ADDRESS.sin_family = AF_INET;
ADDRESS.sin_port = htons(MyPort);
sock_LISTEN = socket (AF_INET, SOCK_STREAM, NULL);
bind(sock_LISTEN, (SOCKADDR*)&ADDRESS,sizeof(ADDRESS));
listen(sock_LISTEN,SOMAXCONN);
cout << "Listening on port " << MyPort << "..." << endl;
for (;;) {
if (sock_CONNECTION = accept(sock_LISTEN, (SOCKADDR*)&ADDRESS, &AddressSize)) {
cout << inet_ntoa(ADDRESS.sin_addr) << " connected." << endl;
send(sock_CONNECTION, "Welcome! Now, go away!", 46, NULL);
}
}
}
|