我有一个数据采集模块,我想从以太网端口收集数据.我步骤到达那里,目前我只想connect从客户端服务器.我用过Beej的指南来获取基本的C代码.但我只是继续得到这个连接错误connect: Connection refused.
这就是我做的:
这里提到的网络IP是我配置的STATIC IP.
服务器端的端口号设置为50000,客户端的端口号设置为端口50000的IP.
我构建并运行服务器端应用程序,然后尝试通过运行客户端应用程序连接到它.
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
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
您的服务器代码丢失listen(),accept()代码通过调用listen()然后执行accept()接受新连接来"等待" 连接.您使用的示例不显示如何执行此操作吗?通常,您还将为每个新连接分叉一个新线程.
有关更多信息,请参阅http://www.linuxhowtos.org/C_C++/socket.htm.
以下是更完整实施的链接:http://www.thegeekstuff.com/2011/12/c-socket-programming/
是的,您需要保持服务器程序运行。在您的服务器程序中,您已经使用socket()并绑定到一个地址创建了套接字bind(),现在您需要开始侦听传入的连接。这是通过listen()调用完成的。一旦套接字侦听传入连接,您必须使用accept()调用来实际接受连接并获取套接字以与该特定客户端进行通信。
举个简单的例子,在freeaddrinfo你可以添加以下代码之后:
listen(sockfd, 8); /* allow up to 8 pending connections, i.e. created but not accepted */
while (1) {
int clientfd;
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(struct sockaddr_in);
clientfd = accept(sockfd, &client_addr, &client_addr_len);
/* communicate with client by send()/recv() or write()/read() on clientfd */
close(clientfd);
}
Run Code Online (Sandbox Code Playgroud)
这段代码的缺点是一次只能处理一个客户端。有几种方法可以处理多个并发客户端:多个进程使用fork()、多个线程或轮询。在我看来,这些方法中的每一种都超出了这个问题的范围。