Hi,


So I have a very simple server/client program using winsock. In C++. Using MingW as a compiler.

This is the code part which does the connecting:

for(;;)
{
	if(Connect = accept(Listen, (SOCKADDR*)&Server, &size))
	{
		std::cout<<"\nConnection was reached";
	}
}

How can I send and receive simple text from server to clients?

Also is a busy loop like the "for" really necessarily in this case? Can't it be done in another way?


Thanks.

Recommended Answers

All 10 Replies

Refer this link. It has most of the code that you need to implement a simple server/client to send/receive text.

You don't always have to do the busy for loop. You could just open the socket in blocking mode and wait until text arrives (research blocking). However, this will cause the receiver to halt its execution until something comes its way, so if you plan to do something else with your program as it waits for data, you should consider using a separate thread to do the waiting part.

As an alternative, Winsock has the WM_SOCKET_NOTIFY event that can be used to get notifications when an event for that socket has occurred. This will allow you to do other things, and switch to the data reading part when it actually arrives. But designing this a bit involved if you have no experience with the Windows API.

OK, so from that link.

This is what I use to send stuff, server wise.

send(new_fd, "Hello, world!", 13, 0)

And this, inside the same loop on the client, to receive it.

numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)

Is that correct?


And will just a single "send" in the server send it directly to "all" clients or do I have to do other things?

Run it and see if it's correct.

A single send will send it only to one client.
To send to multiple clients, you have to connect multiple clients and have the server do a send to each client.

So I guess I must tag/ID them in some way. And than in a for loop send them the data, correct?

Question is how?

I can do say int clients_id[50]=0; and than use that in my "connecting loop" lets call it, what I posted above.

But than how do I tag a connecting client to an ID?

int a=-1,clients_id[50];
for(;;)
{
	if(Connect = accept(Listen, (SOCKADDR*)&Server, &size))
	{
		a = a +1;
		client_id[a] = what?
	}
}

I guess this is close to what it should be or how it works right?

You can use the socket file descriptor returned by accept to distinguish between multiple clients connected to the server.

Could you help me out with a working example?

I looked through the winsock2.h too, as you suggested first. But didn't find anything.

As for the accept , how would I do that? I am a bit lost.

Post your attempt. Let's see what the problem is.

Well the best I can do is:

client_id[a] = accept(Listen, (SOCKADDR*)&Server, &size)

Can't find the problem with only one line of code.
Post the whole program.
The one you mentioned in your first post, and the one modified to accept multiple clients.

So I have a very simple server/client program using winsock. In C++. Using MingW as a compiler.

Alright, sure.

#include <iostream>
#include <winsock2.h>
#include <Windows.h>

int main(int argc, char *argv[])
{

int a=-1,clients_id[50];

WSAData wsa;
WORD Version = MAKEWORD(2,1);

WSAStartup(Version, &wsa);

SOCKET Listen = socket(AF_INET, SOCK_STREAM, 0);
SOCKET Connect = socket(AF_INET, SOCK_STREAM, 0);

SOCKADDR_IN Server;

Server.sin_addr.s_addr = inet_addr("127.0.0.1");
Server.sin_family = AF_INET;
Server.sin_port = htons(100);

bind(Listen, (SOCKADDR*)&Server, sizeof(Server));

listen(Listen, 32);

int size = sizeof(Server);

std::cout<<"Listening...";

for(;;)
{
	if(Connect = accept(Listen, (SOCKADDR*)&Server, &size))
	{
		std::cout<<"\nConnection was reached";
                a = a +1;
                client_id[a] = accept(Listen, (SOCKADDR*)&Server, &size) ???
	}
}

WSACleanup();
std::cin.get();
return 0;
}

I need to do 2 things. ID The client and than be able to send all of them data, the same data.

Also, is it possible to use a forward DNS instead of an IP address? For clients to connect to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.