不知何故,套接字是-1。它不应该是因为我的客户端程序连接并发送信息,但是被SIGPIPE信号停止。当我调试服务器时,我看到accept返回-1。我不明白为什么。如果你不,请回答。
client.cpp
using namespace std;
using namespace msg;
using namespace dataExchange;
int main() {
Connection con("127.0.0.1", 123456);
Annoucement ann;
ann.set_typ(Annoucement::AKCJA);
con.send(ann);
}
server.cpp
using namespace std;
using namespace msg;
using namespace dataExchange;
int main() {
Server server(123456);
while(1) {
Connection con = server.accept();
Annoucement ann = con.receive();
cout << ann.typ() << endl;
}
}
connect.cpp
using namespace msg;
using namespace google::protobuf;
using namespace google::protobuf::io;
namespace dataExchange {
class SocketMaintenance {
protected:
sockaddr_in addr;
int s;
public:
SocketMaintenance(const unsigned int port) {
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
s = socket(AF_INET, SOCK_STREAM, 0);
}
SocketMaintenance(const sockaddr_in Addr, const int Socket) {
addr = Addr;
s = Socket;
}
SocketMaintenance(const SocketMaintenance& source) {
addr = source.addr;
s = source.s;
}
~SocketMaintenance() {
close(s);
}
void write(const char* buffer, const int n) {
::write(s, buffer, n);
}
};
class Connection : public SocketMaintenance {
FileOutputStream* raw_output;
FileInputStream* raw_input;
char buffer[1024];
public:
Connection(const char* IP, const unsigned int port) : SocketMaintenance(port) {
if(inet_pton(AF_INET, IP, &addr.sin_addr)<=0) {
throw WrongAddress();
}
if(connect(s, (sockaddr*)&addr, sizeof(addr))<0) {
throw ConnectFailed();
}
raw_output = new FileOutputStream(s);
raw_input = new FileInputStream(s);
}
Connection(const SocketMaintenance& source) : SocketMaintenance(source) {}
~Connection() {
delete raw_output;
}
void send(const Message& msg) throw(EmptyMessage) {
CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
int n = msg.ByteSize();
if(n<=0) throw EmptyMessage();
coded_output->WriteVarint32(n);
delete coded_output;
raw_output->Flush();
msg.SerializeToArray(buffer, n);
SocketMaintenance::write(buffer, n);
}
Annoucement receive() throw() {
CodedInputStream* coded_input = new CodedInputStream(raw_input);
google::protobuf::uint32 n;
coded_input->ReadVarint32(&n);
char *b;
int m;
coded_input->GetDirectBufferPointer((const void**)&b, &m);
Annoucement ann;
ann.ParseFromArray(b, n);
return ann;
}
};
class Server : public SocketMaintenance {
public:
Server(const unsigned int port) : SocketMaintenance(port) {
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(s, (struct sockaddr *)&addr, sizeof(addr))<0) {
throw BindFailed();
}
if(listen(s, 5)<0) {
throw ListenFailed();
}
}
Connection accept() throw() {
sockaddr_in client;
socklen_t client_len;
int client_socket = ::accept(s, (sockaddr*)&client, &client_len);
return Connection(SocketMaintenance(client, client_socket));
}
};
}
在我将其重新排列为类之前,我已经测试了这段代码。然后它完美无缺。
答案 0 :(得分:1)
猜测,在那里你正在按值传递一个套接字类对象,它的析构函数正在过早地关闭套接字。我无法通过代码来弄清楚是否是这种情况,但有些意见:
答案 1 :(得分:1)
我看到的一个明显问题是您的端口无效。 sockaddr_in::sin_port
是uint16_t
,其最大值可能是65536.最好的情况是,您在调用htons
时截断您的端口,这应该会导致57920。
关于接受失败的原因,-1
表示发生了错误,您应该检查errno
的值以了解原因。
来自man 2 accept
:
返回值 成功时,accept()返回一个非负整数,即a 接受套接字的描述符。 出错时,返回-1,并返回错误 设置得当。