我最近在建立 SSH 隧道时遇到了问题。事实证明,Apache 和 Nginx 都无法绑定到任何端口(即使以 root 身份和/或超出 <1024 范围)。来自这里我有一个小测试程序(就在下面)示例代码;我的版本输出如下),希望我能找到错误。但是,运行./server 6789
和./client localhost 6789
(在同一台机器上的两个终端中)没有出现错误。只是没有连接,所以server
阻塞在accept()
:
Server:
socket().
bzero().
htons().
bind().
listen().
Client:
socket().
gethostbyname().
bzero().
bcopy().
htons().
ERROR connecting: Connection timed out
输出语句是我的。它们是我唯一更改过的东西,在每次命令成功后打印出来。
Apache 和 Nginx 的行为类似,你只是无法连接到服务器。但是,使用 SSH 会出现错误:
$ ssh -4 -vvv -NL 5901:"$server":5901 "$user@$server"
[...]
debug1: Authentication succeeded (publickey).
Authenticated to <server> ([<ip>]:22).
debug1: Local connections to LOCALHOST:5901 forwarded to remote address <server>:5901
debug3: channel_setup_fwd_listener_tcpip: type 2 wildcard 0 addr NULL
debug1: Local forwarding listening on 127.0.0.1 port 5901.
bind: Cannot assign requested address
channel_setup_fwd_listener_tcpip: cannot listen to port: 5901
Could not request local forwarding.
debug2: fd 3 setting TCP_NODELAY
debug3: ssh_packet_set_tos: set IP_TOS 0x10
debug1: Requesting [email protected]
(如果没有-4
,输出略有不同,但基本相同。)
Anki 的行为类似:
$ anki
Exception in thread Thread-1:
Traceback (most recent call last):
File "threading.py", line 916, in _bootstrap_inner
File "aqt/mediasrv.py", line 57, in run
File "socketserver.py", line 453, in __init__
File "aqt/mediasrv.py", line 38, in server_bind
File "socketserver.py", line 467, in server_bind
OSError: [Errno 99] Cannot assign requested address
知道这里的问题是什么或我如何才能找出错误吗?
它已在 Devuan Jessie 上运行,在另一台运行 Devuan ASCII 的 PC 上也运行正常。我已经检查过其他安装中是否缺少某个包,但没有找到任何相关的东西(我有netbase
)。
谢谢!
服务器.cpp:
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
puts("Server:");
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
puts("socket().");
bzero((char *) &serv_addr, sizeof(serv_addr));
puts("bzero().");
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
puts("htons().");
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
puts("bind().");
listen(sockfd,5);
puts("listen().");
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
puts("accept().");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}
客户端.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
puts("Client:");
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
puts("socket().");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
puts("gethostbyname().");
bzero((char *) &serv_addr, sizeof(serv_addr));
puts("bzero().");
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
puts("bcopy().");
serv_addr.sin_port = htons(portno);
puts("htons().");
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);
return 0;
}
答案1
输出ip addr show lo
表明环回接口没有运行,因此可以快速修复
sudo ifup lo