Hallo,
ich habe ein kleines Client-Programm geschrieben. Der client bekommt Daten vom Server und gibt sie als String aus. Ich möchte nun den Client so umprogrammieren, daß die empfagsdaten in eine Textdatei geschrieben werden. Kann jemand mir dabei helfen? Der Quellcode sieht so aus.
// Client socket
// Receives data from the server socket
#include "stdafx.h"
#include "clientSocket.h"
#include <cstdlib>
#include <time.h>
#include <iostream>
#include <fstream>
CWinApp theApp;
using namespace std;
float floatSwap( float f );
int longSwap(int i);
short shortSwap( short s );
const int numDataStreams = 12;
int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
cout << "Starting up TCP client\r\n";
const char* servername="127.0.0.1";
SOCKET client;
struct hostent *hp;
WSADATA wsaData;
int counter = 0;
//The sockaddr_in specifies the address of the socket
//for TCP/IP sockets. Other protocols use similar structures.
struct sockaddr_in serverInfo;
unsigned int address;
//WSAStartup initializes the program for calling WinSock.
//The first parameter specifies the highest version of the
//WinSock specification, the program is allowed to use.
int wsaret=WSAStartup(0x101,&wsaData);
//WSAStartup returns zero on success.
//If it fails we exit.
if(wsaret!=0)
{
cout << "Unable to initialize WinSock library.\n";
return 1;
}
//the socket function creates our client socket
client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//If the socket() function fails we exit
if(client==INVALID_SOCKET)
{
cout << "Cannot establish connection: "
<< WSAGetLastError() << '\n';
WSACleanup();
return 1;
}
if(inet_addr(servername)==INADDR_NONE)
{
hp=gethostbyname(servername);
}
else
{
address=inet_addr(servername);
hp=gethostbyaddr((char*)&address,sizeof(address),AF_INET);
}
if(hp==NULL)
{
closesocket(client);
return 1;
}
//Now we populate the sockaddr_in structure
serverInfo.sin_family=AF_INET;
serverInfo.sin_addr.s_addr=*((unsigned long*)hp->h_addr);
serverInfo.sin_port=htons(2055);
if(connect(client,(struct sockaddr*)&serverInfo,sizeof(serverInfo)))
{
cout << "Cannot establish connection: "
<< WSAGetLastError() << '\n';
closesocket(client);
return 1;
}
// Data exchange
long rc = 0;
while(rc!=SOCKET_ERROR)
{
float inbuf[numDataStreams];
fd_set fsRead;
FD_ZERO(&fsRead);
FD_SET(client, &fsRead);
timeval sTimeoutVal;
sTimeoutVal.tv_sec = (long)10; //seconds
sTimeoutVal.tv_usec = (long)0; //milliseconds
//wait up to 3 seconds for the socket to receive data
int retval = select(FD_SETSIZE, &fsRead, (fd_set *) NULL, (fd_set *)
NULL, &sTimeoutVal);
if(retval != 1)
{
//connection timed out
//close socket and remove a winsock reference count
cout << "There is no data to receive! \n Probably the LabView tool has to be restarted! \n We close the socket!\n";
return 1;
}
// Data exchange
long rc=recv(client,(char*) inbuf, numDataStreams*sizeof(float),0);
if( rc!=0 && rc!=SOCKET_ERROR )
{
for ( int i = 0; i < numDataStreams; i++ )
{
// inbuf = floatSwap( inbuf );
cout << inbuf[ i ] << "\n";
}
counter = counter + 1;
cout << counter << "\n";
cout << "\n";
cout << "\n";
}
else
{
cout << "There is no data to receive! \n End of file? \n We close the socket!\n";
return 1;
}
}
closesocket(client);
//originally this function probably had some use
//currently this is just for backward compatibility
//but it is safer to call it as I still believe some
//implementations use this to terminate use of WS2_32.DLL
WSACleanup();
return 0;
}