C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 12-26-2003, 12:55 PM   #1
Registered User
 
Join Date: Aug 2003
Posts: 288
Server Client Messaging Program

Im trying to make a program that allows 1 server to have multiple clients connected to it.

So far I have the server set up quite well, it allows many clients to connect at once and the clients can all send data to the server.

Im having 1 little problem on each side, the server cant send data and the client side cant receive data, I have no idea how to go about fixing this since this is the first time I ever multithread.

For the server program, to send data to the client (assuming you have like 10 clients connected), how can you pick out 1 specific client and actually send the data to him?

Heres the code I use for the server side to send data:

Code:
bool SendtoClient(char *Buffer)
{
	unsigned long messageSize = strlen(Buffer);

	messageSize = htonl(messageSize);

	if (send(Client, (char*)&messageSize, sizeof(messageSize), 0) == SOCKET_ERROR)
		return false;

	messageSize = ntohl(messageSize);

	if (send(Client, Buffer, messageSize, 0) == SOCKET_ERROR)
		return false;
}
Client is a SOCKET, i guess it should point to the actual client socket?

This function is called when a user selects the Clients SOCKET address from a list, and clicks send. I think this function should work but im not really sure if your supposed to put the Server's SOCKET or the Client's SOCKET in the first parameter of the send function.

For the Client side i have similar code but instead of using the Server's SOCKET, i use the Client's SOCKET and it works..

Code:
bool SendtoServer(char *Buffer)
{
	unsigned long messageSize = strlen(Buffer);

	messageSize = htonl(messageSize);

	if (send(Client, (char*)&messageSize, sizeof(messageSize), 0) == SOCKET_ERROR)
		return false;

	messageSize = ntohl(messageSize);

	if (send(Client, Buffer, messageSize, 0) == SOCKET_ERROR)
		return false;
}
X PaYnE X is offline   Reply With Quote
Old 12-26-2003, 01:01 PM   #2
Registered User
 
Join Date: Aug 2003
Posts: 288
Also i was wondering if this function returns the SOCKET for the client or the server??

Code:
SOCKET StartClient(char *Address, unsigned short Port) 
{
	WSAData wsaData;

	if (WSAStartup(WINSOCK_VERSION, &wsaData) == SOCKET_ERROR) 
	{
		MessageBox(NULL, "Failed to Startup Winsock", "", MB_OK);
		return SOCKET_ERROR;
	}

	SOCKET Socket = socket(AF_INET, SOCK_STREAM, 0);

	if (Socket == SOCKET_ERROR) 
	{
		MessageBox(NULL, "Error Creating Socket", "", MB_OK);
		return SOCKET_ERROR;
	}

	struct hostent *HostEntry;

	if ((HostEntry = gethostbyname(Address)) == NULL) 
	{
		MessageBox(NULL, "Could not find host", "", MB_OK);
		return SOCKET_ERROR;
	}

	struct sockaddr_in Server;

	ZeroMemory(&Server, sizeof(Server));

	Server.sin_family = AF_INET;
	Server.sin_port = htons(Port);
	Server.sin_addr.s_addr = *(unsigned long*)HostEntry->h_addr;

	if (connect(Socket, (sockaddr*)&Server, sizeof(Server)) == SOCKET_ERROR) 
	{
		MessageBox(NULL, "Error connecting to server", "", MB_OK);
		return SOCKET_ERROR;
	}

	return Socket;
}
X PaYnE X is offline   Reply With Quote
Old 12-27-2003, 09:14 PM   #3
Yes, my avatar is stolen
 
anonytmouse's Avatar
 
Join Date: Dec 2002
Posts: 2,544
Quote:
Also i was wondering if this function returns the SOCKET for the client or the server??

SOCKET StartClient(char *Address, unsigned short Port)
This function creates a socket and connects it to Address.
For example, if Address equals "192.168.0.55":

When you send() on the returned socket the data will be sent to "192.168.0.55".

When you recv() on the returned socket you will receive data sent to you by "192.168.0.55".

It looks like you use this function in the client to create a connection to the server.

Quote:
Client is a SOCKET, i guess it should point to the actual client socket?
Yes, this should be the SOCKET returned from the call to accept(). Maybe you could post your accept() loop as this sounds like it may be the problem.

If the code is not too massive you may want to post it all as an attachment.
anonytmouse is offline   Reply With Quote
Old 01-04-2004, 05:20 PM   #4
Carnivore ('-'v)
 
Hunter2's Avatar
 
Join Date: May 2002
Posts: 2,865
Just a comment, I believe the proper value to check for after calling socket() is INVALID_SOCKET rather than SOCKET_ERROR.
Code:
if (Socket == INVALID_SOCKET) 
{
	MessageBox(NULL, "Error Creating Socket", "", MB_OK);
	return INVALID_SOCKET;
}
//and other instances of "return SOCKET_ERROR"
//should also be "return INVALID_SOCKET"
__________________
Just Google It. √

(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Hunter2 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming chat client, need some help (sockets & threads) lalilulelo17 Linux Programming 1 04-19-2008 04:01 AM
server client application - (i really need your help) sarahnetworking C Programming 3 03-01-2008 10:54 PM
Socket Programming Problem!!!! bobthebullet990 Networking/Device Communication 2 02-21-2008 07:36 PM
Commerical MMORPG Developer Opp Th3Guy Projects and Job Recruitment 19 01-22-2007 11:28 AM
Client abnormally terminates when server isn't found Brain Cell Networking/Device Communication 10 03-16-2005 04:29 AM


All times are GMT -6. The time now is 01:30 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22