我尝试在同一台计算机上运行以下两个服务器和客户端程序。我使用端口5100运行服务器。然后我使用localhost 5100参数运行客户端。然后它工作。但是,当我通过同一网络在另一台计算机上的一台计算机和客户端上运行服务器时,客户端一直说ERROR连接:连接超时。当我运行客户端时,我发送了服务器机器的私有IP和端口号。我试图从客户端计算机ping服务器,所有数据包都被传输和接收,所以我知道两台计算机可以互相通信。以下所有代码均来自www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html。任何帮助/建议将不胜感激。谢谢。
这是client.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
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");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
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);
return 0;
}
这是server.c
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, 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");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on 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");
return 0;
}
答案 0 :(得分:2)
为了补充上一个答案,如果您的iptables通过链式策略阻止数据,请确保它们处于&#34; ACCEPT&#34;模式:
$stripResults = array_filter(array_map('trim', $results));
答案 1 :(得分:0)
以上代码在不同的机器上运行良好。您面临的问题可能与您的iptables或防火墙有关。使用iptables -L
查看服务器和客户端中的可用端口,并使用客户端和服务器端共用的端口之一。
例如,iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpts:21100:21299
ACCEPT tcp -- anywhere anywhere tcp dpts:21100:21299
ACCEPT udp -- anywhere anywhere udp dpt:8092
ACCEPT udp -- anywhere anywhere udp dpt:8091
ACCEPT tcp -- anywhere anywhere tcp dpt:diameter
ACCEPT tcp -- anywhere anywhere tcp dpt:irdmi
ACCEPT tcp -- anywhere anywhere tcp dpt:mysql
ACCEPT tcp -- anywhere anywhere tcp dpt:8092
在这里您可以看到端口8092是免费的。因此,当使用它时,它将起作用。或者在两台计算机上使用iptables -F
刷新您的iptables,然后重试。代码没有错。