Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C语言中,如何将字符串从服务器返回到客户端?_C++_C_Port_Stringstream - Fatal编程技术网

C++ 在C语言中,如何将字符串从服务器返回到客户端?

C++ 在C语言中,如何将字符串从服务器返回到客户端?,c++,c,port,stringstream,C++,C,Port,Stringstream,我需要发送一个字符串回客户端,其中包括车辆的成本和带有修改器的车辆(汽车造型)。我想向客户机返回一个包含sline和成本的字符串。有点像 你的越野车要花15万美元 以下段落包含必要的代码 #include <arpa/inet.h> #include <netdb.h> #include <netinet/in.h> #include <unistd.h> #include <iostream> #include <cs

我需要发送一个字符串回客户端,其中包括车辆的成本和带有修改器的车辆(汽车造型)。我想向客户机返回一个包含sline和成本的字符串。有点像

你的越野车要花15万美元

以下段落包含必要的代码

#include <arpa/inet.h>

#include <netdb.h>

#include <netinet/in.h>

#include <unistd.h>

#include <iostream>

#include <cstring>

#include <stdlib.h>

#include <stdio.h>

#include <string>

#include <sstream>



#define MAX_MSG 100

#define LINE_ARRAY_SIZE (MAX_MSG+1)



using namespace std;



int main()

{

  int listenSocket, connectSocket, i;

  unsigned short int listenPort;

  socklen_t clientAddressLength
;

  struct sockaddr_in clientAddress, serverAddress;

  char line[LINE_ARRAY_SIZE];





  cout << "Enter port number to listen on (between 1500 and 65000): ";

  cin >> listenPort;



  // Create socket for listening for client connection

  // requests.

  listenSocket = socket(AF_INET, SOCK_STREAM, 0);

  if (listenSocket < 0) {

    cerr << "cannot create listen socket";

    exit(1);

  }



  // Bind listen socket to listen port. First set various

  // fields in the serverAddress structure, then call

  // bind().



  // htonl() and htons() convert long integers and short

  // integers (respectively) from host byte order (on x86

  // this is Least Significant Byte first) to network byte

  // order (Most Significant Byte first).

  serverAddress.sin_family = AF_INET;

  serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);

  serverAddress.sin_port = htons(listenPort);



  if (bind(listenSocket,

           (struct sockaddr *) &serverAddress,

           sizeof(serverAddress)) < 0) {

    cerr << "cannot bind socket";

    exit(1);

  }



  // Wait for connections from clients. This is a

  // non-blocking call; i.e., it registers this program with

  // the system as expecting connections on this socket, and

  // then this thread of execution continues on.

  listen(listenSocket, 5);



  while (1) {

    cout << "Waiting for TCP connection on port " << listenPort << " ...\n";



    // Accept a connection with a client that is requesting

    // one. The accept() call is a blocking call; i.e., this

    // thread of execution stops until a connection comes

    // in. connectSocket is a new socket that the system

    // provides, separate from listenSocket. We *could*

    // accept more connections on listenSocket, before

    // connectSocket is closed, but this program doesn't do

    // that.

    clientAddressLength = sizeof(clientAddress);

    connectSocket = accept(listenSocket,

                           (struct sockaddr *) &clientAddress,

                           &clientAddressLength);

    if (connectSocket < 0) {

      cerr << "cannot accept connection ";

      exit(1);

    }



    // Show the IP address of the client.

    // inet_ntoa() converts an IP address from binary form to the

    // standard "numbers and dots" notation.

    cout << "  connected to " << inet_ntoa(clientAddress.sin_addr);



    // Show the client's port number.

    // ntohs() converts a short int from network byte order (which is

    // Most Significant Byte first) to host byte order (which on x86,

    // for example, is Least Significant Byte first).

    cout << ":" << ntohs(clientAddress.sin_port) << "\n";



    // Read lines from socket, using recv(), storing them in the line

    // array.  If no messages are currently available, recv() blocks

    // until one arrives.

    // First set line to all zeroes, so we'll know where the end of

    // the string is.

    memset(line, 0x0, LINE_ARRAY_SIZE);



    while (recv(connectSocket, line, MAX_MSG, 0) > 0) {

      cout << "  --  " << line << "\n";



      // Convert line to upper case.

      for (i = 0; line[i] != '\0'; i++)

        line[i] = toupper(line[i]);





      // creating an object to direct line to a string array

      std::string delimiter[2];

      int i = 0;

      double cost = 0;

      std::string carType;

      std::string carStyling;

      std::string sline;

      sline = line;

      stringstream ssin(sline);



          while (ssin.good() && i < 2){

            ssin >> delimiter[i];

            ++i;

      }

          for(i = 0; i < 2; i++){

            cout << delimiter[i] << endl;

      }



          if(i==0) {

            carType = delimiter[0];



              if(carType.compare("SEDAN")==0){

                sline = "Your Sedan";

                cost = 100000;

                std::copy(sline.begin(), sline.end(), line);

                line[sline.size()] = '\0';

              }

              else if(carType.compare("MPV")==0){

                sline = "MPV";

                cost = 120000;

                std::copy(sline.begin(), sline.end(), line);

                line[sline.size()] = '\0';

              }

              else if(carType.compare("SUV")==0){

                sline = "SUV";

                cost = 140000;

                std::copy(sline.begin(), sline.end(), line);

                line[sline.size()] = '\0';

              }

              else if(carType.compare("LUXURY")==0){

                sline = "LUXURY";

                cost = 180000;

                std::copy(sline.begin(), sline.end(), line);

                line[sline.size()] = '\0';

              }



         if(i==2) {

            carStyling = delimiter[1];



              if(carStyling.compare("SPORTY")==0){

                sline += "Sporty";

                cost = cost * 1.5;

              }

              else if(carStyling.compare("OFFROAD")==0){

                sline += "Offroad";

                cost = cost * 1.3;

              }



        }



      }





      // Send converted line back to client.

      if (send(connectSocket, line, strlen(line) + 1, 0) < 0)

        cerr << "Error: cannot send modified data";



      memset(line, 0x0, LINE_ARRAY_SIZE);  // set line to all zeroes

    }

  }

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大值为100
#定义行数组大小(最大值+1)
使用名称空间std;
int main()
{
int listenSocket,connectSocket,i;
无符号短整型列表端口;
socklen\u t客户端地址长度
;
客户端地址、服务器地址中的结构sockaddr_;
字符行[行数组大小];
cout>listenPort;
//创建用于侦听客户端连接的套接字
//请求。
listenSocket=socket(AF_INET,SOCK_STREAM,0);
如果(listenSocket<0){
cerr h_地址列表[0],主机信息->h_长度);
serverAddress.sin_port=htons(serverPort);
如果(连接)(socketDescriptor,
(结构sockaddr*)和服务器地址,
sizeof(服务器地址))<0){

cerr您可以通过复制字节将std::string sline打包到发送缓冲区:

memcpy(line, sline.c_str(), strlen(sline.c_str()))
发送它,然后在客户端以同样的方式解包

编辑: 请为您的服务器尝试下面的代码,这是您想要的吗

#include <arpa/inet.h> 
#include <netdb.h> 
#include <netinet/in.h> 
#include <unistd.h> 
#include <iostream> 
#include <cstring> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string> 
#include <sstream> 

#define MAX_MSG 100 
#define LINE_ARRAY_SIZE (MAX_MSG+1) 

using namespace std; 

int main() 
{ 
  int listenSocket, connectSocket, i; 
  unsigned short int listenPort; 
  socklen_t clientAddressLength; 
  struct sockaddr_in clientAddress, serverAddress; 
  char line[LINE_ARRAY_SIZE]; 

  cout << "Enter port number to listen on (between 1500 and 65000): "; 
  cin >> listenPort; 

  listenSocket = socket(AF_INET, SOCK_STREAM, 0); 

  if (listenSocket < 0) { 
    cerr << "cannot create listen socket"; 
    exit(1); 
  } 

  serverAddress.sin_family = AF_INET; 
  serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); 
  serverAddress.sin_port = htons(listenPort); 

  if (bind(listenSocket, (struct sockaddr *) &serverAddress, 
           sizeof(serverAddress)) < 0) { 
    cerr << "cannot bind socket"; 
    exit(1); 
  } 

  listen(listenSocket, 5); 

  while (1) { 
    cout << "Waiting for TCP connection on port " << listenPort << " ...\n"; 
    clientAddressLength = sizeof(clientAddress); 
    connectSocket = accept(listenSocket, (struct sockaddr *) &clientAddress, 
                           &clientAddressLength); 

    if (connectSocket < 0) { 
      cerr << "cannot accept connection "; 
      exit(1); 
    } 
    cout << "  connected to " << inet_ntoa(clientAddress.sin_addr); 
    cout << ":" << ntohs(clientAddress.sin_port) << "\n"; 

    memset(line, 0x0, LINE_ARRAY_SIZE); 
    while (recv(connectSocket,line, MAX_MSG, 0) > 0) { 

    cout << "  --  " << line << "\n"; 

    std::string delimiter[2]; 
    int i = 0; 
    double cost = 0; 
    std::string carType; 
    std::string carStyling; 
    std::string sline; 
    sline = line; 
    stringstream ssin(sline); 

    while (ssin.good() && i < 2){ 
      ssin >> delimiter[i]; 
      ++i; 
    } 
    sline = ""; 

    for(i = 0; i < 2; i++){ 
      cout << delimiter[i] << endl; 
    } 
    sline += "Your "; 
    carType = delimiter[0]; 

    if(carType.compare("Sedan")==0){ 
      sline += "Sedan"; 
      cost = 100000; 
    } 
    else if(carType.compare("MPV")==0){ 
      sline += "MPV"; 
      cost = 120000; 
    } 
    else if(carType.compare("SUV")==0){ 
      sline += "SUV"; 
      cost = 140000; 
    } 
    else if(carType.compare("Luxury")==0){ 
      sline += "Luxury"; 
      cost = 180000; 
    } 

    carStyling = delimiter[1]; 

    if(carStyling.compare("Sporty")==0){ 
      sline += " Sporty "; 
      cost = cost * 1.5; 
    } 

    else if(carStyling.compare("Offroad")==0){ 
      sline += " Offroad "; 
      cost = cost * 1.3; 
    } 

    sline += "will cost "; 
    std::ostringstream ss; 
    ss << cost; 
    sline += ss.str(); 

    sline.copy(line, sline.length()); 

    if (send(connectSocket, line, strlen(line) + 1, 0) < 0) 
      cerr << "Error: cannot send modified data"; 
      memset(line, 0x0, LINE_ARRAY_SIZE);  // set line to all zeroes 
    } 
  } 
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大值为100
#定义行数组大小(最大值+1)
使用名称空间std;
int main()
{ 
int listenSocket,connectSocket,i;
无符号短整型列表端口;
socklen_t clientaddress长度;
客户端地址、服务器地址中的结构sockaddr_;
字符行[行数组大小];
cout>listenPort;
listenSocket=socket(AF_INET,SOCK_STREAM,0);
如果(listenSocket<0){

CERP不编译为C,因为使用C++构造。为什么你要标记它?另外:你的问题到底是什么?“怎么做X?”似乎太宽泛了。虽然用一行空行将代码分成段落有利于可读性,但您可能在另一个方向上走得有点远。您显示的代码真的复制粘贴正确吗?我想说的是,所有这些空行使代码更难阅读,更难理解正在发生的事情,并使我们无法理解ll远远超出了需要(这对于获得概述来说从来都不是一件好事)请格式化代码,使缩进正确,在每个空的代码之后没有空行。@如果代码是用C编译器编译的,则是C,如果用C++编译器编译的话,它是C++。因为你的代码在C编译器中编译不出来,就像我以前所说的,为什么你要标记你的任务?ion with C?当长度可以通过
sline.length()
获得时,为什么要使用
strlen
?而且这个额外的
memcpy
也不需要,因为你可以在调用
send
时直接使用
sline.C_str()
。你是对的,但如果他想发送“字符串和成本”作为一个例子。也许最好的方法是找出一些发送协议。例如,打包以缓冲传入字符串的长度,然后是字符串,然后是四个字节int,以节省成本。然后在另一边解包。@PawełDymowski它编译得很好,但并不是真正打印我想要的。只返回我写的任何内容的大写字母。@Thadohtut uppe因为您有://Convert line to大写。对于(i=0;line[i]!='\0';i++)line[i]=toupper(line[i]);@thadoeht,如果您想将成本发送给客户端,您还需要将其复制到sendung缓冲区。
#include <arpa/inet.h> 
#include <netdb.h> 
#include <netinet/in.h> 
#include <unistd.h> 
#include <iostream> 
#include <cstring> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string> 
#include <sstream> 

#define MAX_MSG 100 
#define LINE_ARRAY_SIZE (MAX_MSG+1) 

using namespace std; 

int main() 
{ 
  int listenSocket, connectSocket, i; 
  unsigned short int listenPort; 
  socklen_t clientAddressLength; 
  struct sockaddr_in clientAddress, serverAddress; 
  char line[LINE_ARRAY_SIZE]; 

  cout << "Enter port number to listen on (between 1500 and 65000): "; 
  cin >> listenPort; 

  listenSocket = socket(AF_INET, SOCK_STREAM, 0); 

  if (listenSocket < 0) { 
    cerr << "cannot create listen socket"; 
    exit(1); 
  } 

  serverAddress.sin_family = AF_INET; 
  serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); 
  serverAddress.sin_port = htons(listenPort); 

  if (bind(listenSocket, (struct sockaddr *) &serverAddress, 
           sizeof(serverAddress)) < 0) { 
    cerr << "cannot bind socket"; 
    exit(1); 
  } 

  listen(listenSocket, 5); 

  while (1) { 
    cout << "Waiting for TCP connection on port " << listenPort << " ...\n"; 
    clientAddressLength = sizeof(clientAddress); 
    connectSocket = accept(listenSocket, (struct sockaddr *) &clientAddress, 
                           &clientAddressLength); 

    if (connectSocket < 0) { 
      cerr << "cannot accept connection "; 
      exit(1); 
    } 
    cout << "  connected to " << inet_ntoa(clientAddress.sin_addr); 
    cout << ":" << ntohs(clientAddress.sin_port) << "\n"; 

    memset(line, 0x0, LINE_ARRAY_SIZE); 
    while (recv(connectSocket,line, MAX_MSG, 0) > 0) { 

    cout << "  --  " << line << "\n"; 

    std::string delimiter[2]; 
    int i = 0; 
    double cost = 0; 
    std::string carType; 
    std::string carStyling; 
    std::string sline; 
    sline = line; 
    stringstream ssin(sline); 

    while (ssin.good() && i < 2){ 
      ssin >> delimiter[i]; 
      ++i; 
    } 
    sline = ""; 

    for(i = 0; i < 2; i++){ 
      cout << delimiter[i] << endl; 
    } 
    sline += "Your "; 
    carType = delimiter[0]; 

    if(carType.compare("Sedan")==0){ 
      sline += "Sedan"; 
      cost = 100000; 
    } 
    else if(carType.compare("MPV")==0){ 
      sline += "MPV"; 
      cost = 120000; 
    } 
    else if(carType.compare("SUV")==0){ 
      sline += "SUV"; 
      cost = 140000; 
    } 
    else if(carType.compare("Luxury")==0){ 
      sline += "Luxury"; 
      cost = 180000; 
    } 

    carStyling = delimiter[1]; 

    if(carStyling.compare("Sporty")==0){ 
      sline += " Sporty "; 
      cost = cost * 1.5; 
    } 

    else if(carStyling.compare("Offroad")==0){ 
      sline += " Offroad "; 
      cost = cost * 1.3; 
    } 

    sline += "will cost "; 
    std::ostringstream ss; 
    ss << cost; 
    sline += ss.str(); 

    sline.copy(line, sline.length()); 

    if (send(connectSocket, line, strlen(line) + 1, 0) < 0) 
      cerr << "Error: cannot send modified data"; 
      memset(line, 0x0, LINE_ARRAY_SIZE);  // set line to all zeroes 
    } 
  } 
}