Networking in c++ (winsock)

Hello, I've been learning networking with winsock for a while and created simple console server and client. The client gets input from the keyboard, sends it to the server, and the server receives the data and sends it back to the client (that's the general purpose of it). The problem is that I've written it with blocked sockets as it is the winsock default and I can only connect 1 client to the server and work with it solely.
My question is, how is it possible to make my server listen, connect and react to more than 1 client at a time? Is it possible without multithreading or do I have to implement a code for multithreading and access each client func in a different thread?
Would be glad to get some help here.

P.S, I got another question related to winsock and c++.
Is it possible for me to program something in c# and use my c++ networking classes inside? (it's actually much simpler than implementing c# networking).

Thanks in advance
Xathael
if you want to process more than one thing simultaneous you need a thread. You may do it with another process, but that's like multi threading an a higher level.

Is it possible for me to program something in c# and use my c++ networking classes inside?
No, you cannot use C++ classes in C#. What you can do is creating a C interface for your C# program. The problem will be passing variables because of the different kind of access. You'd need a CLI layer for that.

http://en.wikipedia.org/wiki/Common_Language_Infrastructure
Thanks man. Any chance you can give me some suggested tutorial to multithreading? Everything I found in google was incredibly complicated, long and time consuming or the boost site that I couldn't make it work.
Thanks
edit:
I've actually made a very simple threading program now using some tutorial i found on page 2 on google
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <windows.h>
#include  <stdlib.h>
#include <iostream>
#include <winsock.h>

using namespace std;

DWORD WINAPI Task1(LPVOID lpParam)
{
	while (true)
	{
		cout << "I'm pissed cos threading works here much worth than in    c#" << endl;
	}
}


int main()
{
	DWORD thread;
	CreateThread(NULL, 0, Task1, NULL, 0, &thread);
	Sleep(1000);
}


The problem is that I don't see this threading working in a server since only with the sleep function the other thread works, which means there can't be simultaneous work of threads with this example. What should I do?
but it works. in your server program the accept function will pause your program while the client thread(s) are doing their processing.

Note that this way the client thread might be shutdown while it's still processing. But that might be ok because a server shouldn't be shutdown
Xathael, I have not done what your doing but if you were willing to share you code I would love to learn more and maybe work on it with you. You can send me a private msg if you like.


The client gets input from the keyboard, sends it to the server, and the server receives the data and sends it back to the client


I don't know anything about winsock, or blocked sockets but have some experience with general networking.

To discuss what your doing, would it be possible for the client or server to open and close the connection when not in use?

That means only one could connect at a time but it's probably so fast you might not run into a problem, if you code the client to retry every second for 10 seconds.

The only other way I can think of is to have your server listen on multiple ports, say 5000-5032 and the client would try one, then another until it found a open connection.

The network address would look like 10.10.10.10:5000

Anther idea came to mind, if you have the server listen on one address, such as 10.10.10.10:5000, it then tells each client that wants to talk to it which port is free, so that 5000 is always free except for a brief fraction of a second. Then the client would connect to the given address and could keep the connection open until it's done.
Last edited on
Topic archived. No new replies allowed.