C++ 通过服务器将对象传递给客户端
我正在尝试在linux中创建一个客户机-服务器应用程序。服务器应该向所有连接的客户端发送一个对象。 这是它的代码。 在这种情况下,当服务器发送对象时,服务器端的一切都保持正常,但客户机服务器在收到该对象后立即发生分段错误 服务器:C++ 通过服务器将对象传递给客户端,c++,linux,segmentation-fault,client-server,coredump,C++,Linux,Segmentation Fault,Client Server,Coredump,我正在尝试在linux中创建一个客户机-服务器应用程序。服务器应该向所有连接的客户端发送一个对象。 这是它的代码。 在这种情况下,当服务器发送对象时,服务器端的一切都保持正常,但客户机服务器在收到该对象后立即发生分段错误 服务器: #include "Question.h" #include <iostream> #include <string.h> using namespace std; #include<sys/socket.h> #include&
#include "Question.h"
#include <iostream>
#include <string.h>
using namespace std;
#include<sys/socket.h>
#include<sys/types.h>
#include<stdio.h>
#include<arpa/inet.h>
#include<time.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
int main() {
Question q1("Capital Of Pakistan?", "Lahore", "Karachi", "Quetaa", "Islamabad");
int socketID = 0, clientID[10] = {0}, totalClients = 3;
char sendBuffer[1024];
memset(sendBuffer, '0', sizeof(sendBuffer));
time_t time;
struct sockaddr_in servAddr;
cout << "Question is: \n" << q1.getQuestion()<<endl;
cout << q1.getOpt1() << endl << q1.getOpt2() << endl << q1.getOpt3() << endl << q1.getCorrect()<<endl;
cout << "\n\n --- Server starting up --- \n\n";
/*
* Creating Socket
*/
socketID = socket(AF_INET, SOCK_STREAM, 0);
if (socketID == -1) {
cerr << " Can't create Socket";
}
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(5000);
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
/*
* Binding IP
*/
int bindID;
bindID = bind(socketID, (struct sockaddr *) &servAddr, sizeof(servAddr)); // Casting sockaddr_in on sockaddr and binding it with socket id
if (bindID != -1) {
cout << " Bind SucessFull";
listen(socketID, 5);
cout << " Server Waiting for connections" << endl;
int i = 0;
while (1) {
clientID[i] = accept(socketID, (struct sockaddr *) NULL, NULL);
cout << "Got Client: " << i+1 << ","<<totalClients-(i+1)<<" to go" << endl;
cout << "ID: " << clientID[i]<<endl;
cout.flush();
snprintf(sendBuffer, sizeof(sendBuffer), "%.24s\n", ctime(&time));
write(clientID[i], sendBuffer, strlen(sendBuffer));
i++;
if (i >= totalClients)
break;
sleep(1);
}
cout << "Sending Question to All Clients...." << endl;
for(int j=0; j<totalClients; j++) {
cout << "Sending to ID " << clientID[j]<<endl;
write(clientID[j], &q1 , sizeof(q1));
cout << "Sent " << j << "...." << endl;
}
/*
* Closing all clients
*/
for (int k = 0; k < totalClients; k++) {
close(clientID[k]);
}
} else {
cerr << " Unable to Bind";
}
return 0;
}
#包括“问题.h”
#包括
#包括
使用名称空间std;
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(){
问题q1(“巴基斯坦首都?”、“拉合尔”、“卡拉奇”、“克塔阿”、“伊斯兰堡”);
intsocketid=0,clientID[10]={0},totalClients=3;
字符发送缓冲区[1024];
memset(sendBuffer,'0',sizeof(sendBuffer));
时间;
servAddr中的结构sockaddr_;
cout您需要“序列化”您的对象。这通常涉及将其转换为字符串,可以在发送对象的任何东西的另一端读取该字符串
这与将数据写入文件完全相同,您不希望存储对象,而是希望存储类中的“有效负载”或“内容”
您可以使用stringstream
形成数据的长字符串,并传递由此形成的字符串
比如:
class A
{
int x;
string s;
};
...
class A a;
stringstream ss;
ss << a.x << ", " << a.s << endl;
....
write(clientID[j], ss.str.c_str(), ss.str.length());
A类
{
int x;
字符串s;
};
...
甲级;
细流ss;
我不知道怎么做。请告诉我两行语法。
#ifndef QUESTION_H_
#define QUESTION_H_
#include <iostream>
using namespace std;
class Question {
private:
string question;
string opt1;
string opt2;
string opt3;
string correct;
public:
/*
* Constructors
*/
Question();
Question(string, string, string, string, string);
void setAll(string, string, string, string, string);
string getCorrect() const;
void setCorrect(string correct);
string getOpt1() const;
void setOpt1(string opt1);
string getOpt2() const;
void setOpt2(string opt2);
string getOpt3() const;
void setOpt3(string opt3);
void setQuestion(string question);
string getQuestion() const;
};
#endif /* QUESTION_H_ */
class A
{
int x;
string s;
};
...
class A a;
stringstream ss;
ss << a.x << ", " << a.s << endl;
....
write(clientID[j], ss.str.c_str(), ss.str.length());