Why do I get unlimited [] after a message?
I want to figure the problem out myself, the only problem is I dont know what would cause that to happen. Is that what a stack overflow is? If so what causes them?
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
you''re not really giving much info, also can''t even say if you are in the right forum.
from the few words I''d asume your environment expects a \0 terminated string, but this is just a guess.
Need more info.
from the few words I''d asume your environment expects a \0 terminated string, but this is just a guess.
Need more info.
-----The scheduled downtime is omitted cause of technical problems.
Are you trying to call delete[] on a received message? Is that it? Need....more...info!
2 + 2 = 5 for extremely large values of 2
Here is my server code
ok now when it recieves the error and prints it it keeps posting the [] after it, and it never stops! I have been trying everything I could think of, so alot of the code is really messed up. Once I figure out the problem im just going to rewrite it.
[edited by - Thrust on June 14, 2004 1:10:02 PM]
//creating the server for my online chat program#include <iostream>#include <winsock2.h>#include <string>using namespace std;//Makeing the main function a void because it doesent return anythingvoid main() { cout <<"Online chat server: Version 0.1"<< endl; cout <<""<<endl; cout <<"Starting Server..."<<endl; //Winsock always return an error so you need a variable to put it into int error; WSAData wsaData; // and the call to start up winSock error = WSAStartup(MAKEWORD(2, 2), &wsaData); if (error == SOCKET_ERROR) { cout <<"Error starting winSock..."<<endl; return; } cout <<"Winsock loaded..."<<endl; //the socket im going to use int mySocket; //now turn it into a socket mySocket = socket(AF_INET, SOCK_STREAM, 0); if (mySocket == SOCKET_ERROR) { cout <<"Error creating socket..."<<endl; return; } cout <<"Socket created..."<<endl; struct sockaddr_in server; // fill the address structure with appropriate data server.sin_family = AF_INET;// for internet server.sin_port = htons(8765); // the port server.sin_addr.s_addr = INADDR_ANY;// ? dont remember // bind the socket error = bind(mySocket, (sockaddr*)&server, sizeof(server)); if (error == SOCKET_ERROR) { cout <<"Err0r binding socket"<<endl; return; } //listen for connection error = listen(mySocket, 5); if (error == SOCKET_ERROR) { cout <<"Error listening for socket"<<endl; return; } cout <<"Waiting for a client to join..."<<endl; //OK, mySocket is the socket for connections. When somebody is accepted //they need to be on a different socket: int clientSocket; clientSocket = accept(mySocket, 0, 0); if (clientSocket == SOCKET_ERROR) { cout<<"error accepting a client..."<<endl; } cout<<"Client accepted...:"<<endl; cout<<""<<endl; cout<<""<<endl; int error2; int loop = 1; int messageSize; #define maxSize 90000 char message[maxSize]; while (loop == 1) { // 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); cout<<messageSize<<endl; error2 = recv(clientSocket,message, sizeof (messageSize), 0); message[maxSize] = '\0'; cout <<message<<endl; }}
ok now when it recieves the error and prints it it keeps posting the [] after it, and it never stops! I have been trying everything I could think of, so alot of the code is really messed up. Once I figure out the problem im just going to rewrite it.
[edited by - Thrust on June 14, 2004 1:10:02 PM]
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
June 14, 2004 02:05 PM
I did not look at this long enough to be sure exactly how it works or if this is right, but on your last call to "recv" a couple lines from the bottom you are passing in a sizeof (messageSize). It seems like you would want to just pass in messageSize, because the size of messageSize is probably always 4.
June 14, 2004 02:07 PM
if the message doesn''t automatiacally null terminate itself you might want to null terminate it a messageSize instead of maxSize.
blah....ignore my post i can't read code today 
-me
[edited by - Palidine on June 14, 2004 3:18:30 PM]

-me
[edited by - Palidine on June 14, 2004 3:18:30 PM]
Random obvious bugs:
First, use messageSize, not sizeof(messageSize), in the second recv().
Second, make sure that you make messageSize() to maxSize-1 if it's greater than maxSize-1, else you allow recv() to overwrite your buffer.
Third, when you zero-terminate, if you declare an array of size N and then write at index [N] that writes beyond the array.
First, use messageSize, not sizeof(messageSize), in the second recv().
Second, make sure that you make messageSize() to maxSize-1 if it's greater than maxSize-1, else you allow recv() to overwrite your buffer.
Third, when you zero-terminate, if you declare an array of size N and then write at index [N] that writes beyond the array.
enum Bool { True, False, FileNotFound };
Here is my analysis of your code. I hope it helps!
Your first call to recv():
error2 = recv(clientSocket,(char*)&messageSize, sizeof(messageSize), 0);
Ok, It appears that you want to check the size of the sent data with your first call to recv().
The recv function call looks like this:
int recv(
SOCKET s,
char* buf,
int len,
int flags
);
This means that your call is trying to stuff the entire message into the integer called messageSize. When the recv call returns, it returns the number of bytes that were recieved. So if I am assuming this all correctly, you really only need one call to recv. It might look something like this:
messageSize = recv(clientSocket, (char *)&message, maxSize, 0);
You might then want to check for errors in the recv, like you had before. (if (messageSize == SOCKET_ERROR))
Then you wanted to append a '\0' to the end of the recieved data. In the char array message, the last element that was filled with data from the recv would be message[messageSize-1]. So you want the next element to be the '\0'. So do something like this:
message[messageSize] = '\0';
Then you could do the cout'ing of the size and the message itself.
Also, you do not need to do any ntohl on the message size, because it is a returned value from recv, already in the host byte order.
Here is my modified code for your loop that works: Note: I had to change to printf instead of cout, but that's just because I didn't want to mess with my IDE not liking iostream. You can probably still use cout, iostream and endl.
Your first call to recv():
error2 = recv(clientSocket,(char*)&messageSize, sizeof(messageSize), 0);
Ok, It appears that you want to check the size of the sent data with your first call to recv().
The recv function call looks like this:
int recv(
SOCKET s,
char* buf,
int len,
int flags
);
This means that your call is trying to stuff the entire message into the integer called messageSize. When the recv call returns, it returns the number of bytes that were recieved. So if I am assuming this all correctly, you really only need one call to recv. It might look something like this:
messageSize = recv(clientSocket, (char *)&message, maxSize, 0);
You might then want to check for errors in the recv, like you had before. (if (messageSize == SOCKET_ERROR))
Then you wanted to append a '\0' to the end of the recieved data. In the char array message, the last element that was filled with data from the recv would be message[messageSize-1]. So you want the next element to be the '\0'. So do something like this:
message[messageSize] = '\0';
Then you could do the cout'ing of the size and the message itself.
Also, you do not need to do any ntohl on the message size, because it is a returned value from recv, already in the host byte order.
Here is my modified code for your loop that works: Note: I had to change to printf instead of cout, but that's just because I didn't want to mess with my IDE not liking iostream. You can probably still use cout, iostream and endl.
// receive the message size from our client messageSize = recv(clientSocket,(char*)&message, maxSize, 0); // check for errors if (error2 == SOCKET_ERROR) { printf("error"); break; } messageSize = messageSize; printf("%d\n",messageSize); message[messageSize] = '\0'; printf("%s\n",message); //should check to see if disconnected before continueing loop
Ok thanks for all of the help. Now that I have fixed that problem I get another one. Since I can only send chars how would I detect a space in a message? I recieve messages but they always truncate at the first space in a message. Also how would it be possiable so send and recieve messages at the same time, like a chat room? I have two wile loops but they wont work at the same time.
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement