Winsock question

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);
		}
	}
}
if I use blocking sockets, is it going to be one connection at a time? Or, am I not understanding something?
Not necessarily and probably.

Non-blocking I/O calls kick of the I/O operation and frees you up to do something else. You'll need to check in some time later to see how well the operation went. You could use that time to deal with other I/O requests, for example.

You can service multiple conversations using this multiplexing models, and when done properly makes the best use of the computer (that's what nginx does for example). However, that's not what you need right now.

It's conventional to start a new thread for each new connection. So your code would look something like:
1
2
3
4
5
6
7
8
while (!quit)
{
    sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
    SOCKET client = accept(sock_LISTEN, (sockaddr*)&addr, &addrlen);

    // create a new thread and pass in clientsock and addr
}


You may find this helpful.
http://www.cplusplus.com/forum/unices/91734/#msg493002
I'll check it out. Thanks!

So, the part that I'm missing is Multitasking and Threads.
Last edited on
Topic archived. No new replies allowed.