[英]C++ TCP Client - accept a floating point number and a mathematical operator
This is what I need help accomplishing: Prompt the user and accept a floating point number, Prompt the user and accept a mathematical operator (+, - *, /), Prompt the user and accept another floating point number. 这是我需要完成的帮助:提示用户并接受浮点数,提示用户并接受数学运算符(+,-*,/),提示用户并接受另一个浮点数。 This is my client code.
这是我的客户代码。 I have a server code which I need this program to send the data to for processing and then display the results.
我有一个服务器代码,我需要该程序将该数据发送到进行处理,然后显示结果。 I can post my server code if it's needed.
如果需要,我可以发布服务器代码。 Please help!
请帮忙!
#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;
}
Your networking code looks good, although I'm not an expert on that part. 您的网络代码看起来不错,尽管我不是这方面的专家。 Prompting the user for input is quite easy.
提示用户输入非常容易。 Since you used C-style I/O in your question, I will use C-style I/O as well, even though your question is tagged
C++
. 由于您在问题中使用了C风格的I / O,因此即使您的问题被标记为
C++
,我也将使用C风格的I / O。
To write output to the console, use printf
: 要将输出写入控制台,请使用
printf
:
printf("\nPlease enter a floating point number: ");
( \\n
will output a newline.) (
\\n
将输出换行符。)
To read output from the console, use scanf
: 要从控制台读取输出,请使用
scanf
:
float number1;
scanf("%f", &number1);
Here the string "%f"
indicates that we are reading a floating point number. 这里的字符串
"%f"
表示我们正在读取一个浮点数。 The second argument is a pointer to a floating point number in which we want to store our answer. 第二个参数是一个指向要在其中存储答案的浮点数的指针。 For more information about
scanf
, see the cplusplus-reference . 有关
scanf
更多信息,请参见cplusplus-reference 。
You can prompt for the other input in a similar fashion. 您可以以类似方式提示其他输入。 Complete code:
完整的代码:
printf("\nPlease enter a floating point number: ");
float number1;
scanf("%f", &number1);
printf("\nPlease enter a mathematical operator (+,-,*,/): ");
char mathOperator;
scanf("%c", &mathOperator);
if( mathOperator != '+' && mathOperator!= '-' && mathOperator!= '*' && mathOperator!= '/' )
{
//your error handling here
}
printf("\nPlease enter a floating point number: ");
float number2;
scanf("%f", &number2);
printf("\nCalculating %f %c %f...", &number1, &mathOperator, &number2);
//now send data to server
In the latest example of printf
, you can also see that you can write values to the output in a fashion similar to how you read them, and that you can have multiple values in a single command. 在最新的
printf
示例中,您还可以看到可以以类似于读取值的方式将值写入输出,并且在单个命令中可以具有多个值。 For more information on printf
, see the cplusplus-reference . 有关
printf
更多信息,请参见cplusplus-reference 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.