Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Thrust

Simple question about algorithm

This topic is 5156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!