Ok, we have a problem, here's some code, any help would be greatly appreciated.
The client in this code, sends a char to the server, the char is the ID that asks for it to send a float back. The code runs fine, but instead of, displaying our float we sent (which is 1.0), it displays 0. We also display our buffer array, but it says the value is !!. The out put of the program is at the end of this code...
//here the client code (not all of it mind you, just the send and recieve stuff)
int nBytes;//number of bytes we expect to recieve... #define MAX_SIZE 13//max size of our packet float val1 = 0.0f;//the valuue of the float we recieve float val2 = 0.0f//float2 float val3 = 0.0f//float3 char buffer[13];//our buffer char id=19;//the ID that the server wil evaluate memcpy(buffer, (char*)&id,1);//copy our message id into the buffer size = htonl(size);//makes sure size is still kool with the server//send the size the server should expect....and make sure there were no errors sending if ((nBytes = send(mySocket, (char*)&size, sizeof(size), 0)) == SOCKET_ERROR) { printf("Send Failed!\n");//say we do have an error } size = ntohl(size);//make sure our size is still kool //send the actual id if ((nBytes = send(mySocket,buffer, size, 0)) == SOCKET_ERROR) { printf("Send Failed!\n"); } //recieve the size nBytes = recv(mySocket, (char*)&size, sizeof(size), 0); //recieve the float nBytes = recv(mySocket, buffer, size, 0);//copy the id, first float, second and third memcpy(&id, buffer,1); memcpy(&val, buffer+1, 4); memcpy(&val, buffer+5, 4); memcpy(&val, buffer+9, 4); //do we have an error if (nBytes == SOCKET_ERROR) { printf("Recv Failed!\n"); } else { printf("Reply Received The Number Is : %d\n", val); } Sleep(9000);
//heres the server code int nBytes;//number of bytes we will recieve #define MAX_SIZE 13//max size of our buffer char buffer[MAX_SIZE];//our buffer to send unsigned long size;//how much we will recieve//recieve size from the client nBytes = recv(clientSocket, (char*)&size, sizeof(size), 0);//did we get an error? and deal with it if we did if (nBytes == SOCKET_ERROR) { int error = WSAGetLastError(); if (error == WSAECONNRESET) { WaitForSingleObject(mutexHandle, INFINITE); FD_CLR(clientSocket, &masterSet); ReleaseMutex(mutexHandle); closesocket(clientSocket); printf("client on %d disconnected\n", clientSocket); continue; } else { printf("Recv Failed!\n"); gQuitFlag = true; break; } } if (nBytes == 0) {//if we dont have anything WaitForSingleObject(mutexHandle, INFINITE); FD_CLR(clientSocket, &masterSet); ReleaseMutex(mutexHandle); closesocket(clientSocket); printf("client on %d disconnected\n", clientSocket); continue; } size = ntohl(size);//make sure size is kool with the client float val1; float val2; float val3; char id = 19;//random id we chose for send floats nBytes = recv(clientSocket, buffer, size, 0);//recieve the id //if we have an error if (nBytes == SOCKET_ERROR) { printf("Recv Failed!\n"); gQuitFlag = true; break; } val1 = 1.0f;//we chose 1.0 randomly for this program val2 = 1.0f; val3 = 1.0f; memcpy(buffer, &id, 1);//copy id to our buffer memcpy(buffer+1,(char*)&val1,sizeof(val));//copy our first float memcpy(buffer+5,(char*)&val2,sizeof(val));//copy our second memcpy(buffer+9,(char*)&val3,sizeof(val));//copy our third send(clientSocket, (char*)&size, sizeof(size), 0);//send the size of our packet send(clientSocket, buffer, 13, 0);//send the actual packet printf("Float sent to client %d. \n", clientSocket);//ran good printf("Message Received from client on %d : %s\n", clientSocket, buffer);//worked out ok
the output of the program is...
on the client it says...
client started
enter any key to recieve a float:
reply recieved: the number is 0
on the server it says...
server started
client on 84 connected
Float sent to client on 84 : !! <----that is the value of buffer
client on 84 disconnected
Thanks in advance for any help, and sorry if its a stupid mistake. But thats how we newbies are.
-Steers
[Edited by - Steers on December 15, 2004 5:54:27 PM]