![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
weird output in my server program
Hello. I am working on a client/server program, and everything connects fine. When I send a message from the client to the server, the server outputs the variable inmessage 2x, but the value of inmessage becomes ' ' the 2nd time. FYI, I am on Windows XP SP 2 for the client and server, and my compiler is Microsoft Visual Studio 2005. Here is the server code, followed by the client code (edited for posting here):
#include <iostream>
#include <windows.h>
#include <winsock2.h>
using namespace std;
#pragma comment(lib,"WS2_32.lib")
int main(int argc, char* argv[])
{
char message[4096];
char inmessage[4096];
int error;
WSAData wsaData;
error=WSAStartup(MAKEWORD(2,2), &wsaData); // Startup winsock
if(error==SOCKET_ERROR)
{
cout << "Could not start up winsock!" << endl;
cin >> error;
return 0;
}
cout << endl << "Winsock started" << endl;
int mysocket=socket(AF_INET,SOCK_STREAM,0); // Create a socket
if(mysocket==SOCKET_ERROR)
{
cout << "Error opening socket" << endl;
cin >> error;
return 0;
}
cout << "Socket Opened" << endl;
struct sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(7654);
server.sin_addr.s_addr=INADDR_ANY;
error=bind(mysocket, (sockaddr*)&server,sizeof(server)); // Bind socket to port
if(error==SOCKET_ERROR)
{
cout << "Error binding socket to port" << endl;
cin >> error;
return 0;
}
error=listen(mysocket,5); // Listen for connections
if(error== SOCKET_ERROR)
{
cout << endl << "Error Listening for connection" << endl;
cin >> error;
return 0;
}
int clientsocket;
clientsocket=accept(mysocket,0,0); // Accept connections from clients
if(clientsocket== SOCKET_ERROR)
{
cout << endl << "Error accepting connection";
cin >> error;
return 0;
}
int nbytes;
while(1)
{
strcpy(message,"Hello person");
nbytes=send(clientsocket,message,sizeof(message),0);
if(nbytes == SOCKET_ERROR)
{
cout << "Error sending data" << endl;
cin >> error;
return 0;
}
nbytes=recv(clientsocket,inmessage,sizeof(inmessage),0);
if(nbytes == SOCKET_ERROR)
{
cout << "Error receiving message" << endl;
cin >> error;
return 0;
}
else if(nbytes!=0)
{
cout << "inmessage: '" << inmessage << "'" << endl;
// output: inmessage: '<value_of_inmessage>'
// followed by: inmessage: '' on the line below it
}
}
return 0;
}#include <iostream>
#include <windows.h>
#include <winsock2.h>
#pragma comment(lib, "WS2_32.lib")
int main(int argc, char* argv[])
{
char message[4096];
char inmessage[4096];
int error;
WSAData wsaData;
error=WSAStartup(MAKEWORD(2,2), &wsaData); // Startup winsock
if(error==SOCKET_ERROR)
{
cout << endl << "Error starting winsock" << endl;
cin >> error;
return 0;
}
SOCKET mysocket=socket(AF_INET, SOCK_STREAM, 0); // Create a socket
if(mysocket==SOCKET_ERROR)
{
cout << endl << "Error Opening Socket" << endl;
cin >> error;
return 0;
}
cout << endl << "Socket Opened";
char server_name[40];
cout << endl << "What is the ip address of the server that you would like to connect to? ";
cin >> server_name;
cout << "server ip address= " << server_name;
struct hostent *host_entry;
host_entry = gethostbyname(server_name); // Try to locate the host
if(host_entry==NULL)
{
cout << endl << "Could not find a host" << endl;
cin >> error;
return 0;
}
// Setup the information required to connect to the host
struct sockaddr_in server;
server.sin_family = AF_INET; // Server is either on a local network or
// the world wide web
server.sin_port = htons((unsigned short) 7654);
server.sin_addr.s_addr = *(unsigned long *) host_entry->h_addr;
error=connect(mysocket, (sockaddr *)&server, sizeof(server)); // Connect to the server
if(error==SOCKET_ERROR)
{
cout << endl << "Error connecting to server" << endl;
cin >> error;
return 0;
}
int nbytes;
while(1)
{
nbytes = recv(mysocket,inmessage,sizeof(inmessage),0);
if(nbytes == SOCKET_ERROR)
{
cout << "error receiving message" << endl;
cin >> error;
return 0;
}
cout << "inmessage: '" << inmessage << "'" << endl;
cout << "message? " << endl;
cin.ignore();
cin.getline(message,sizeof(message),'\n');
cout << "message: '" << message << "'" << endl;
nbytes = send(mysocket,message,sizeof(message),0);
if(nbytes == SOCKET_ERROR)
{
cout << "Error sending message" << endl;
cin >> error;
return 0;
}
}
cin.ignore();
cin.get();
return 0;
}Here is some sample input/output: input on client: hello world output on server: inmessage: 'hello world' inmessage: '' output on client: message: 'hello world' Can someone please tell me why it is outputting the second line on the server? Thank you for your time and effort. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
No analysis, just off the top of my head: crap in the buffer, possibly sent by the other end, possibly a local artifact. Why don't you redirect the message to a file and look at it with a hex editor. Probably a good clue, right there.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
Redirect the message client side, right?
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The problem is on the server's console, right? On the server. Put that output into a file stream instead of into the cout stream, either by redirection or other means. Shoot, you might just put a HEX iomanip thangy into the cout stream and see it on the console.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
I tried both server side and client side, but both returned the message, without any additions whatsoever, and both files were the same. Any other ideas?
EDIT: I output to the file in ASCII and Binary formats. Although I don't think that matters when viewing the file in a hex editor, I thought I would let you know. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Care to cut and paste the hex dump from the side that shows this:
inmessage: 'hello world' inmessage: ''
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
not a problem, here it is:
server: 00000000:68 65 6c 6c 6f 20 77 6f 72 6c 64 hello world client: 00000000:68 65 6c 6c 6f 20 77 6f 72 6c 64 hello world also, please note that I used "Select All", not me highlighting the stuff. EDIT: if you would like, I can upload the files. |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
else if(nbytes!=0)
{
cout << "inmessage: '" << inmessage << "'" << endl;
// output: inmessage: '<value_of_inmessage>'
// followed by: inmessage: '' on the line below it
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
I deleted the elseif statement, but I still get the same output. I tried that, and on the client side (nbytes after send), it says "1460". On the server side (nbytes after recv), it also says "1460" for the first one, and then "2636" for the inmessage: '' case.
I will also go and check the last error to see if nbytes is returning some other value. EDIT: Upon calling WSAGetLastError, both the client and server have error code 0, which I presume means no error. |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You're not terminating the data in the receive buffer before giving it to the stream, which likes to see such things when dealing with a C-string. It's really a wonder it isn't running all the way to Trenton, NJ, detouring into the grass, and barfing on it's shoes. I say that with less than 100% surety. I'm looking, but I'm old and blind, and I don't see it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|