Advertisement

Simple question about algorithm

Started by April 12, 2004 09:20 PM
2 comments, last by Thrust 20 years, 10 months ago
Ok to most of you this may be a simple question, it may even be simple to do, I havent tried yet. Anyways if I wanted to make it so that I can send and recieve messages all the time between my server and client (only one client) could I just create an infinite loop that always accepts then prints out a message and a constant loop that will accept a cin, then send it? Pesudo code: Initialize winsock connect to each other while(program is running) { Always check for a recieved message } while (program is running) { allow a cin send message repeat } would that even be possiable to have two different loops running at the same time try doing two different things? Also another question related to this problem. winsock can only send a char right? I though that a character can only be 1 letter or number long inless in my code I define how large the message is. Ok what if I allowed for a cin that would go to a string, count the size of the string, and assign that the the message length. The only problem is how would I convert a strings length (probially saved in an Int) to a char? [SIG]
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
Regarding the question of having two loops operating at the same time - what you are talking about is referred to as multithreading. Basically, you have two different ''threads'' running simultaneously, one receiving and processing the messages and the other running the game. Note that there are several precautions that must be taken to ensure that multiple threads are not trying to access the same memory at the same time.
Advertisement
Alright, I made it so that there is one client for now. I have a problem with the server recieving the messages, here look. This is the client:

	int loop = 1;	int error2;	while (loop == 1)	{        #define maxSize 90000		char message[maxSize];		cin >> message;		int sizeofmessage = strlen(message);		sizeofmessage = htonl(sizeofmessage);		error2 = send(mySocket, (char*)&sizeofmessage,sizeof(sizeofmessage), 0);		if (error2 == SOCKET_ERROR)		{			cout<<"Error sending message size"<<endl;			return;		}		sizeofmessage = ntohl(sizeofmessage);		error2 = send(mySocket, message, sizeofmessage, 0);		if (error2 == SOCKET_ERROR)		{			cout<< "Error sending message"<<endl;			return;		}	} 

ok and here is the server:
		int error2;		int loop = 1;     	#define MAX_MESSAGE_SIZE 4096     	char buffer[MAX_MESSAGE_SIZE];		while (loop == 1)		{    	unsigned long messageSize;    	// receive the message size from our client    	error2 = recv(clientSocket, (char*)&messageSize, sizeof(messageSize), 0);	    // check for errors	    if (error2 == SOCKET_ERROR) 		{		cout<<"Error recieving message size..."<<endl;		}    	messageSize = ntohl(messageSize);    	error2 = recv(clientSocket, buffer, messageSize, 0);			if (error2 = SOCKET_ERROR)			{				cout <<"Error recieving message..."<<endl;				return;			}			cout <<buffer<<endl;		} 


Why the heck doesent it work? I finished it at 10something and its 1 now, I wanted to figure this out on my own but I cant figure it out. Please help.

[edited by - Thrust on April 13, 2004 1:04:39 AM]
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
Anybody please? I got up at 10 this morning and ive been working on it all day. Its 2 right now and I still dont know what the heck is wrong. Winsock is alot more complicated then it seems. There isnt an error sending the message size, there is an error recieving the message, but yet the code works out perfect.
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com

This topic is closed to new replies.

Advertisement