0

这就是我需要帮助完成的事情:提示用户并接受一个浮点数,提示用户并接受一个数学运算符(+,-*,/),提示用户并接受另一个浮点数。这是我的客户代码。我有一个服务器代码,我需要该程序将数据发送到该代码进行处理,然后显示结果。如果需要,我可以发布我的服务器代码。请帮忙!

#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;
}
4

1 回答 1

1

您的网络代码看起来不错,尽管我不是这方面的专家。提示用户输入非常容易。由于您在问题中使用了 C 风格的 I/O,因此我也会使用 C 风格的 I/O,即使您的问题被标记为C++.

要将输出写入控制台,请使用printf

printf("\nPlease enter a floating point number: ");

\n将输出换行符。)

要从控制台读取输出,请使用scanf

float number1;
scanf("%f", &number1);

这里的字符串"%f"表示我们正在读取一个浮点数。第二个参数是一个指向浮点数的指针,我们要在其中存储我们的答案。有关 的更多信息scanf,请参阅cplusplus-reference

您可以以类似的方式提示其他输入。完整代码:

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

在 的最新示例中printf,您还可以看到可以以类似于读取它们的方式将值写入输出,并且可以在单个命令中具有多个值。有关 的更多信息printf,请参阅cplusplus-reference

于 2013-03-17T15:03:28.163 回答