我正在尝试连接到我从其他远程设备制作的本地UNIX服务器。服务器已启动并正在侦听指定的端口。我还添加了一个新的防火墙规则来打开该端口,但我的客户端仍然无法连接。它显示ERROR CONNECTION REFUSED
这是我的服务器代码
int main() {
int fd, i,svclient,rval,msg;
int clients[10], num_clients;
fd_set read_set,write_set;
char buf[100];
struct sockaddr_in addr;
if ( (fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket error");
exit(-1);
}
bzero((char *) &addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(4001);
//strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)-1);
//strcpy(addr.sun_path, NAME);
if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("bind error");
exit(-1);
}
printf("Bind complet...\n");
if (listen(fd, 20) == -1) {
perror("listen error");
exit(-1);
}
num_clients = 0;
int size = sizeof(fd);
while (1) {
int clientfd;
struct sockaddr_in client_addr;
int addrlen=sizeof(client_addr);
FD_ZERO(&read_set);
FD_SET(fd, &read_set);
for (i = 0; i < num_clients; i++) { //at first this part will not excute
FD_SET(clients[i], &read_set);
}
select(fd + num_clients + 1, &read_set, NULL, NULL, NULL);
if (FD_ISSET(fd, &read_set)) {
if ( (clients[num_clients++] = accept(fd,(struct sockaddr*)&client_addr,&addrlen)) == -1) {
perror("accept error");
continue;
}
/*printf("incoming message..................... !\n \n");*/
printf("%s:%d connected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
}
for (i = 0; i < num_clients; i++) {
if (FD_ISSET(clients[i], &read_set)) {
msg = read(clients[i], buf, sizeof(buf));
if(msg > 0){
buf[msg] = 0;
int savedclnt = clients[i];
printf("%s \n \n", buf);
/*for(int p=0;p<num_clients;p++)
{
if( clients[p]!= savedclnt){
write(clients[p],buf,msg);
}
}*/
}
}
}
}
}
和我的客户
int main( )
{
struct uci_context *uci;
uci = uci_init();
int sockfd;
int ret;
struct sockaddr_in dest;
struct addrinfo hint, *res = NULL;
struct hostent *host;
char *hostip;
char *string;
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
puts("Unble to create socket");
exit(1);
}
hostip = ucix_get_option(uci, "pack_mon", "pack_monitoring", "address");
string = ucix_get_option(uci, "pack_mon", "pack_monitoring", "port");
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(atoi(string));
memset(&hint, '\0', sizeof hint);
hint.ai_family = PF_UNSPEC;
hint.ai_flags = AI_NUMERICHOST;
printf(" %s- %s\n", hostip, string );
if(isdigit(hostip[0])){
ret = getaddrinfo(hostip, NULL, &hint, &res);// this is more efficient than inet_addr
if (ret) {
exit(1);
}
}else if( (host = gethostbyname(hostip)) != 0){
strncpy((char*)&dest.sin_addr , (char*)host->h_addr , sizeof dest.sin_addr);
}else{
exit(1);
printf("cannot resolve ip address");
}
if ( connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) < 0 )
{
printf("%d\n", connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) < 0);
perror("hmmmm" );
exit(1);
}else{
printf("%d\n", connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) < 0);
printf("Port number %s is open.....\n",string);
}
char *message;
message = "help";
write(sockfd,message,strlen(message));
close(sockfd);
freeaddrinfo(res);
return 0;
}
防火墙规则
sudo iptables -A INPUT -p tcp --dport 4001 -j ACCEPT
Iptable输出:
〜/ Desktop $ sudo iptables -L -v --line-numbers
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 8 440 ACCEPT tcp -- any any anywhere anywhere tcp dpt:4001
2 3 120 ACCEPT tcp -- any any anywhere anywhere tcp dpt:5001
3 177K 128M ufw-before-logging-input all -- any any anywhere anywhere
4 177K 128M ufw-before-input all -- any any anywhere anywhere
5 801 58737 ufw-after-input all -- any any anywhere anywhere
6 208 7160 ufw-after-logging-input all -- any any anywhere anywhere
7 208 7160 ufw-reject-input all -- any any anywhere anywhere
8 208 7160 ufw-track-input all -- any any anywhere anywhere
9 0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:http
10 0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:ssh
11 0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:4999
Chain FORWARD (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ufw-before-logging-forward all -- any any anywhere anywhere
2 0 0 ufw-before-forward all -- any any anywhere anywhere
3 0 0 ufw-after-forward all -- any any anywhere anywhere
4 0 0 ufw-after-logging-forward all -- any any anywhere anywhere
5 0 0 ufw-reject-forward all -- any any anywhere anywhere
6 0 0 ufw-track-forward all -- any any anywhere anywhere
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 154K 19M ufw-before-logging-output all -- any any anywhere anywhere
2 154K 19M ufw-before-output all -- any any anywhere anywhere
3 3925 241K ufw-after-output all -- any any anywhere anywhere
4 3925 241K ufw-after-logging-output all -- any any anywhere anywhere
5 3925 241K ufw-reject-output all -- any any anywhere anywhere
6 3925 241K ufw-track-output all -- any any anywhere anywhere
Chain ufw-after-forward (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-after-input (1 references)
num pkts bytes target prot opt in out source destination
1 232 18312 ufw-skip-to-policy-input udp -- any any anywhere anywhere udp dpt:netbios-ns
2 0 0 ufw-skip-to-policy-input udp -- any any anywhere anywhere udp dpt:netbios-dgm
3 0 0 ufw-skip-to-policy-input tcp -- any any anywhere anywhere tcp dpt:netbios-ssn
4 0 0 ufw-skip-to-policy-input tcp -- any any anywhere anywhere tcp dpt:microsoft-ds
5 27 9365 ufw-skip-to-policy-input udp -- any any anywhere anywhere udp dpt:bootps
6 0 0 ufw-skip-to-policy-input udp -- any any anywhere anywhere udp dpt:bootpc
7 334 23900 ufw-skip-to-policy-input all -- any any anywhere anywhere ADDRTYPE match dst-type BROADCAST
Chain ufw-after-logging-forward (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 LOG all -- any any anywhere anywhere limit: avg 3/min burst 10 LOG level warning prefix "[UFW BLOCK] "
Chain ufw-after-logging-input (1 references)
num pkts bytes target prot opt in out source destination
1 208 7160 LOG all -- any any anywhere anywhere limit: avg 3/min burst 10 LOG level warning prefix "[UFW BLOCK] "
Chain ufw-after-logging-output (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-after-output (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-before-forward (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- any any anywhere anywhere ctstate RELATED,ESTABLISHED
2 0 0 ACCEPT icmp -- any any anywhere anywhere icmp destination-unreachable
3 0 0 ACCEPT icmp -- any any anywhere anywhere icmp source-quench
4 0 0 ACCEPT icmp -- any any anywhere anywhere icmp time-exceeded
5 0 0 ACCEPT icmp -- any any anywhere anywhere icmp parameter-problem
6 0 0 ACCEPT icmp -- any any anywhere anywhere icmp echo-request
7 0 0 ufw-user-forward all -- any any anywhere anywhere
Chain ufw-before-input (1 references)
num pkts bytes target prot opt in out source destination
1 20690 2045K ACCEPT all -- lo any anywhere anywhere
2 155K 126M ACCEPT all -- any any anywhere anywhere ctstate RELATED,ESTABLISHED
3 3 1434 ufw-logging-deny all -- any any anywhere anywhere ctstate INVALID
4 3 1434 DROP all -- any any anywhere anywhere ctstate INVALID
5 0 0 ACCEPT icmp -- any any anywhere anywhere icmp destination-unreachable
6 0 0 ACCEPT icmp -- any any anywhere anywhere icmp source-quench
7 0 0 ACCEPT icmp -- any any anywhere anywhere icmp time-exceeded
8 0 0 ACCEPT icmp -- any any anywhere anywhere icmp parameter-problem
9 0 0 ACCEPT icmp -- any any anywhere anywhere icmp echo-request
10 4 1342 ACCEPT udp -- any any anywhere anywhere udp spt:bootps dpt:bootpc
11 1351 103K ufw-not-local all -- any any anywhere anywhere
12 542 44077 ACCEPT udp -- any any anywhere 224.0.0.251 udp dpt:mdns
13 0 0 ACCEPT udp -- any any anywhere 239.255.255.250 udp dpt:1900
14 809 59217 ufw-user-input all -- any any anywhere anywhere
Chain ufw-before-logging-forward (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-before-logging-input (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-before-logging-output (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-before-output (1 references)
num pkts bytes target prot opt in out source destination
1 20701 2046K ACCEPT all -- any lo anywhere anywhere
2 129K 17M ACCEPT all -- any any anywhere anywhere ctstate RELATED,ESTABLISHED
3 3925 241K ufw-user-output all -- any any anywhere anywhere
Chain ufw-logging-allow (0 references)
num pkts bytes target prot opt in out source destination
1 0 0 LOG all -- any any anywhere anywhere limit: avg 3/min burst 10 LOG level warning prefix "[UFW ALLOW] "
Chain ufw-logging-deny (2 references)
num pkts bytes target prot opt in out source destination
1 3 1434 RETURN all -- any any anywhere anywhere ctstate INVALID limit: avg 3/min burst 10
2 0 0 LOG all -- any any anywhere anywhere limit: avg 3/min burst 10 LOG level warning prefix "[UFW BLOCK] "
Chain ufw-not-local (1 references)
num pkts bytes target prot opt in out source destination
1 23 1510 RETURN all -- any any anywhere anywhere ADDRTYPE match dst-type LOCAL
2 736 50285 RETURN all -- any any anywhere anywhere ADDRTYPE match dst-type MULTICAST
3 592 51499 RETURN all -- any any anywhere anywhere ADDRTYPE match dst-type BROADCAST
4 0 0 ufw-logging-deny all -- any any anywhere anywhere limit: avg 3/min burst 10
5 0 0 DROP all -- any any anywhere anywhere
Chain ufw-reject-forward (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-reject-input (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-reject-output (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-skip-to-policy-forward (0 references)
num pkts bytes target prot opt in out source destination
1 0 0 DROP all -- any any anywhere anywhere
Chain ufw-skip-to-policy-input (7 references)
num pkts bytes target prot opt in out source destination
1 593 51577 DROP all -- any any anywhere anywhere
Chain ufw-skip-to-policy-output (0 references)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- any any anywhere anywhere
Chain ufw-track-forward (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-track-input (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-track-output (1 references)
num pkts bytes target prot opt in out source destination
1 3755 225K ACCEPT tcp -- any any anywhere anywhere ctstate NEW
2 110 12370 ACCEPT udp -- any any anywhere anywhere ctstate NEW
Chain ufw-user-forward (1 references)
num pkts bytes target prot opt in out source destination
Chain ufw-user-input (1 references)
num pkts bytes target prot opt in out source destination
1 8 480 ACCEPT tcp -- any any anywhere anywhere tcp dpt:5001
2 0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:4001
Chain ufw-user-limit (0 references)
num pkts bytes target prot opt in out source destination
1 0 0 LOG all -- any any anywhere anywhere limit: avg 3/min burst 5 LOG level warning prefix "[UFW LIMIT BLOCK] "
2 0 0 REJECT all -- any any anywhere anywhere reject-with icmp-port-unreachable
Chain ufw-user-limit-accept (0 references)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- any any anywhere anywhere
Chain ufw-user-logging-forward (0 references)
num pkts bytes target prot opt in out source destination
Chain ufw-user-logging-input (0 references)
num pkts bytes target prot opt in out source destination
Chain ufw-user-logging-output (0 references)
num pkts bytes target prot opt in out source destination
Chain ufw-user-output (1 references)
num pkts bytes target prot opt in out source destination
它在那个港口的倾听
tcp 0 0 0.0.0.0:4001 0.0.0.0:* LISTEN 1001 138595 18347/m
tcp6 0 0 :::80 :::* LISTEN 0 18805 -
答案 0 :(得分:0)
命令的-A(追加)选项和-I(插入)选项之间存在差异。附加规则时,可能会有另一个现有规则,禁止连接。
尝试使用-I选项。