我有一个数据采集模块,我想从中收集以太网端口的数据。 我在步骤到达那里,目前我只想从connect
到客户端的服务器。我使用Beej的指南来获取基本的C代码。但是我只是不停收到这个连接错误connect: Connection refused.
C,插座:连接拒绝错误
这是我做的:
IP这里的网络是静态IP我已经配置。
的端口号设置为50000在服务器端和客户端我连接到该IP端口50000
我构建和运行服务器端的应用程序,然后尝试连接到它通过运行客户端应用程序。
一个关于服务器端,服务器端应用程序returns
开始之前,我的客户端应用程序怀疑,所以我应该保持它运行(while(1);
),这样我可以从客户端连接到它?
怎么回事我在这里忘了什么?帮帮我!
我粘贴了服务器端和客户端这里的非常轻微的修改(IP和端口号不同)Beej的C代码:
Server.c
/*
** server.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
// code for a server waiting for connections
// namely a stream socket on port 3490, on this host's IP
// either IPv4 or IPv6.
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP address
if ((rv = getaddrinfo(NULL, "50000", &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("socket");
continue;
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("bind");
continue;
}
break; // if we get here, we must have connected successfully
}
if (p == NULL)
{
// looped off the end of the list with no successful bind
fprintf(stderr, "failed to bind socket\n");
exit(2);
}
freeaddrinfo(servinfo); // all done with this structure
}
Client.c
/*
** client.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
// code for a client connecting to a server
// namely a stream socket to www.example.com on port 80 (http)
// either IPv4 or IPv6
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo("192.168.2.4", "50000", &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("connect");
continue;
}
break; // if we get here, we must have connected successfully
}
if (p == NULL)
{
// looped off the end of the list with no connection
fprintf(stderr, "failed to connect\n");
exit(2);
}
freeaddrinfo(servinfo); // all done with this structure
}
谢谢你的链接,他们是一个很好的阅读。 – HaggarTheHorrible 2012-07-16 03:38:08
@HeatfanJohn我检查了这些代码,但结果相同的问题。[server.c](https://paste.ubuntu.com/25599389/)[client.c](https://paste.ubuntu。 com/25599391 /) – alhelal 2017-09-23 13:40:30
@BandaMuhammadAlHelal当您的客户端尝试连接到端口FTP_PORT(21)时,您的服务器似乎在端口9999上侦听。更改您的客户端以使用端口9999连接到。 – HeatfanJohn 2017-09-25 17:14:24