这就是我需要帮助完成的事情:提示用户并接受一个浮点数,提示用户并接受一个数学运算符(+,-*,/),提示用户并接受另一个浮点数。这是我的客户代码。我有一个服务器代码,我需要该程序将数据发送到该代码进行处理,然后显示结果。如果需要,我可以发布我的服务器代码。请帮忙!
#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <winsock.h>
// Function prototype
void StreamClient(char *szServer, short nPort);
// Helper macro for displaying errors
#define PRINTERROR(s) \
fprintf(stderr,"\n%s: %d\n", s, WSAGetLastError())
////////////////////////////////////////////////////////////
void main(int argc, char **argv)
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
short nPort;
//
// Check for the host and port arguments
//
if (argc != 3)
{
fprintf(stderr,"\nSyntax: TCPTimeClient ServerName PortNumber\n");
return;
}
nPort = atoi(argv[2]);
//
// Initialize WinSock and check the version
//
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
return;
}
//
// Go do all the stuff a datagram client does
//
StreamClient(argv[1], nPort);
//
// Release WinSock resources
//
WSACleanup();
}
////////////////////////////////////////////////////////////
void StreamClient(char *szServer, short nPort)
{
int nRet; // return code
char szBuf[256]; // client buffer area
char szSvr[256]; // server name
LPHOSTENT lpHostEntry; // host data structure
SOCKET theSocket; // client socket
SOCKADDR_IN saClient; // socket address structure
//
// Get local machine name
//
nRet = gethostname(szSvr, sizeof(szSvr));
//
// Check for errors
//
if (nRet == SOCKET_ERROR)
{
PRINTERROR("gethostname()");
return;
}
//
// Display an informational message
//
printf("Datagram Client [%s] sending to server [%s] on port %d...\n",
szSvr, szServer, nPort);
//
// Find the server
//
lpHostEntry = gethostbyname(szServer);
if (lpHostEntry == NULL)
{
PRINTERROR("gethostbyname()");
return;
}
//
// Create a TCP/IP datagram socket
//
theSocket = socket(AF_INET, // Address family
SOCK_STREAM, // Socket type
0); // Protocol
//
// Check for errors
//
if (theSocket == INVALID_SOCKET)
{
PRINTERROR("socket()");
return;
}
//
// Fill in the address structure of the server
//
saClient.sin_family = AF_INET;
saClient.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// ^ Client's address
saClient.sin_port = htons(nPort); // Port number from command line
//
// Connect to the server
//
nRet = connect(theSocket,
(struct sockaddr *)&saClient,
sizeof(saClient));
//
// Check for errors
//
if(nRet == SOCKET_ERROR)
{
PRINTERROR("Connect()");
return;
}
//
// Prepare some data to send to the server
//
sprintf(szBuf, "From the Client [%s]", szSvr);
//
// Send data to the server
//
nRet = send(theSocket, // Socket
szBuf, // Data buffer
(int)strlen(szBuf), // Length of data
0); // Flags
//
// Check for errors
//
if (nRet == SOCKET_ERROR)
{
PRINTERROR("send()");
closesocket(theSocket);
return;
}
//
// Zero out the incoming data buffer
//
memset(szBuf, 0, sizeof(szBuf));
//
// Wait for the reply
//
nRet = recv(theSocket, // Socket
szBuf, // Receive buffer
sizeof(szBuf), // Length of receive buffer
0); // Flags
//
// Check for errors
//
if (nRet == SOCKET_ERROR)
{
PRINTERROR("recv()");
closesocket(theSocket);
return;
}
//
// Display the data that was received
//
printf("\n%s", szBuf);
//
// Close the socket
//
closesocket(theSocket);
return;
}