I'm trying to make a server sample in c++ using <sys/socket.h>
and Qt Creator gui builder but two weird behaviors are going on at socket layer of the program. First of, I run the server but at the first attempt I make to connect to it using telnet
is imediately closed
我正在尝试使用
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.尝试127.0.0.1 ...连接到127.0.0.1。逃脱角色是'^]'。外部主机关闭连接。
When I attempt connection for the second time, it works and the terminal waits for my input. The second thing is when I close the connection. If I rerun right after, in a matter of minutes, the program halts on bind
exiting and returning:
当我第二次尝试连接时,它工作,终端等待我的输入。第二件事是我关闭连接。如果我在几分钟内重新运行,程序会在绑定退出和返回时停止:
ERROR on binding: Address already in use
绑定时出错:地址已在使用中
So I suppose maybe a connection is being held after I break it using function call onCortarConexao()
or just stopping the debugger. Anyway, what am I missing?
所以我想在使用函数调用onCortarConexao()或者只是停止调试器之后可能会断开连接。无论如何,我错过了什么?
My code:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
On MainWindow.cpp:
void MainWindow::on_pushButton_clicked()
{
socket1 = new MSocket();
socket1->start();
}
void MainWindow::on_pushButton_3_clicked()
{
socket1->onCortarConexao();
}
Socket class:
#ifndef MSOCKET_H
#define MSOCKET_H
#include <QString>
#include <QObject>
#include <QThread>
#include <QList>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define SERVER_BUFFER 4096
#define PORTRUN 15000
class MSocket : public QThread
{
public:
MSocket();
void error(char *msg);
void onCortarConexao();
private:
int sockfd;
int newsockfd;
int portno;
int clilen;
int n;
char buffer[SERVER_BUFFER];
struct sockaddr_in serv_addr, cli_addr;
u_short port;
void run();
};
#endif // MSOCKET_H
Socket implementation:
void MSocket::run()
{
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
error("ERROR opening socket");
exit(-1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = PORTRUN;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = INADDR_ANY;
fprintf(stdout,"Iniciando servidor..");
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
error("ERROR on binding");
exit(-1);
}
listen(sockfd,5);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t*) sizeof(cli_addr));
if (newsockfd < 0){
error("ERROR on accept");
exit(-1);
}
bzero(buffer,SERVER_BUFFER);
n = read(newsockfd,buffer,SERVER_BUFFER-1);
if (n < 0)
error("ERROR reading from socket");
printf("Here is the message: %s",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0)
error("ERROR writing to socket");
}
void MSocket::onCortarConexao(){
printf("Encerrando socket");
close(newsockfd);
close(sockfd);
}
The complete code is at: https://github.com/FabioNevesRezende/BasicCppServer
完整的代码位于:https://github.com/FabioNevesRezende/BasicCppServer
Edit 1:
So, this is the list of packets of the communication between telnet and my Qt Server application it can be graphicaly seen in WireShark (.pcapng file). It contains 11 frames. The 6 first ones are from the first telnet, when it is imediately closed. As it seems on frame 4 and 5 where the application sends [FIN, ACK]
and the server responds to it by closing the connection. The frames 7,8,9 are the second attempt to connect and frames 10 and 11 is when I send the abc
to the server. As in the print screen:
因此,这是telnet和我的Qt Server应用程序之间通信的数据包列表,它可以在WireShark(.pcapng文件)中图形化。它包含11帧。 6个第一个来自第一个telnet,当它立即关闭时。正如第4帧和第5帧所示,应用程序发送[FIN,ACK],服务器通过关闭连接来响应它。帧7,8,9是第二次连接尝试,帧10和11是我将abc发送到服务器的时候。如在打印屏幕中:
The problem is I don't know why the application is sending this FIN
and where in the code it is.
问题是我不知道为什么应用程序正在发送此FIN以及它在代码中的位置。
4 个解决方案
#1
4
Use SO_REUSEADDR
with setsockopt
:
对setsockopt使用SO_REUSEADDR:
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
This allows the port to be reused by other sockets and gets around the address already in use
issue you're facing.
这允许端口被其他套接字重用,并绕过您正在使用的已经使用的地址。
#2
5
The previous answers have indicate that how to handle the read correctly and how to use SO_RESUEADDR. I want to point out why your program can just receive one message. because your run function doesn't have a loop, so it can be only performed once, then the thread exits. after accept a client's request, then the connection is establish, then you should enter a loop to wait the client's input, and write them back once the read function returns.
之前的答案表明如何正确处理读取以及如何使用SO_RESUEADDR。我想指出为什么你的程序只能收到一条消息。因为你的run函数没有循环,所以它只能执行一次,然后线程退出。在接受客户端的请求之后,然后建立连接,然后您应该输入一个循环来等待客户端的输入,并在读取函数返回后将其写回。
#3
3
Here's the first serious problem I found:
这是我发现的第一个严重问题:
n = read(newsockfd,buffer,SERVER_BUFFER-1);
if (n < 0)
error("ERROR reading from socket");
printf("Here is the message: %s",buffer);
There's no handling for read
returning zero. The %s
format specifier is only for strings. You can't use it for arbitrary, unchecked data.
读取返回零没有处理。 %s格式说明符仅适用于字符串。您不能将其用于任意未经检查的数据。
#4
3
On unix operating systems, system calls can sometimes fail with an errorno set to EINTR. This means that a signal was delivered to the process which interrupted the system call and you should retry it. Try something like this:
在unix操作系统上,系统调用有时会失败,并且errorno设置为EINTR。这意味着信号被传递到进程,这会中断系统调用,您应该重试它。尝试这样的事情:
while ((n = read(newsockfd,buffer,SERVER_BUFFER-1) < 0) {
if (errno != EINTR)
break;
}
if (n < 0)
error("ERROR reading from socket");
The same should be done for the call to write.
写入调用也应该这样做。
#1
4
Use SO_REUSEADDR
with setsockopt
:
对setsockopt使用SO_REUSEADDR:
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
This allows the port to be reused by other sockets and gets around the address already in use
issue you're facing.
这允许端口被其他套接字重用,并绕过您正在使用的已经使用的地址。
#2
5
The previous answers have indicate that how to handle the read correctly and how to use SO_RESUEADDR. I want to point out why your program can just receive one message. because your run function doesn't have a loop, so it can be only performed once, then the thread exits. after accept a client's request, then the connection is establish, then you should enter a loop to wait the client's input, and write them back once the read function returns.
之前的答案表明如何正确处理读取以及如何使用SO_RESUEADDR。我想指出为什么你的程序只能收到一条消息。因为你的run函数没有循环,所以它只能执行一次,然后线程退出。在接受客户端的请求之后,然后建立连接,然后您应该输入一个循环来等待客户端的输入,并在读取函数返回后将其写回。
#3
3
Here's the first serious problem I found:
这是我发现的第一个严重问题:
n = read(newsockfd,buffer,SERVER_BUFFER-1);
if (n < 0)
error("ERROR reading from socket");
printf("Here is the message: %s",buffer);
There's no handling for read
returning zero. The %s
format specifier is only for strings. You can't use it for arbitrary, unchecked data.
读取返回零没有处理。 %s格式说明符仅适用于字符串。您不能将其用于任意未经检查的数据。
#4
3
On unix operating systems, system calls can sometimes fail with an errorno set to EINTR. This means that a signal was delivered to the process which interrupted the system call and you should retry it. Try something like this:
在unix操作系统上,系统调用有时会失败,并且errorno设置为EINTR。这意味着信号被传递到进程,这会中断系统调用,您应该重试它。尝试这样的事情:
while ((n = read(newsockfd,buffer,SERVER_BUFFER-1) < 0) {
if (errno != EINTR)
break;
}
if (n < 0)
error("ERROR reading from socket");
The same should be done for the call to write.
写入调用也应该这样做。