gpt4 book ai didi

c - C套接字中需要多个连接

转载 作者:太空狗 更新时间:2023-10-29 11:33:50 24 4
gpt4 key购买 nike

我编写了以下代码作为连接两个程序的中间代码。在两个不同的系统上运行一个服务器程序和一个客户端程序。此代码有望充当这两个程序之间的中间层。

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

//Connect with program on server side
char * serv_con(char app_data[50])
{
int sock, bytes_recieved;
char send_data[1024];
char *recv_data = malloc(1024);
struct hostent *host;
struct sockaddr_in server_addr;
host = gethostbyname("10.47.3.249");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{

perror("Socket");
exit(1);

}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(3128);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{

perror("Connect");
exit(1);

}
bytes_recieved=recv(sock,recv_data,1024,0);
recv_data[bytes_recieved] = '\0';
send(sock, app_data, 50, 0);
return recv_data;
//close(sock);

}


//Connect with client app
char * cli_con(char ser_data[50])
{
int sock, connected, bytes_recieved , true = 1;
char send_data [1024];
char *recv_data = malloc(1024);
struct sockaddr_in server_addr,client_addr;
int sin_size;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{

perror("Socket");
exit(1);

}
if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1)
{

perror("Setsockopt");
exit(1);

}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{

perror("Unable to bind1");
exit(1);

}
if (listen(sock, 5) == -1)
{

perror("Listen");
exit(1);

}
sin_size = sizeof(struct sockaddr_in);
connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size1);
printf("\n I got a connection from (%s , %d)",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
bytes_recieved = recv(connected,recv_data,1024,0);
recv_data[bytes_recieved] = '\0';
send(connected, ser_data,50, 0);
//close(sock);

}


int main()
{

char *ser_data, *app_data;
int pid = fork();
while(1)
{

if(pid == 0)
app_data = serv_con(ser_data);

else
ser_data = cli_con(app_data);

}

}

在客户端应用程序运行之前,它工作正常。但是一旦客户端应用程序运行,代码就会退出并给出错误:

Unable to bind: Address already in use
I got a connection from (192.168.0.3 , 45691)

我应该在代码中做些什么修改来纠正这个错误?我在 Linux 上工作。提前致谢。

编辑:我删除了 close(sock) 中的注释,并在函数 cli_con 中添加了 close(connect)。客户端代码如下:

int sock, bytes_recieved;  
char send_data[1024],recv_data[1024];
struct hostent *host;
struct sockaddr_in server_addr;
host = gethostbyname("192.168.0.2");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5555);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
perror("Connect");
exit(1);
}
while(1)
{
//necessary codes
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
close(sock);
goto connct;
}
}

但是现在运行,第一个程序没有退出,甚至没有打印

I got a connection from (192.168.0.3 , 45691)

但只是继续运行而不打印任何消息。但另一方面,客户端退出显示错误:

Connect: Connection reset by peer

我现在该怎么办?

最佳答案

当客户端断开连接时,您创建一个新的服务器套接字并将其绑定(bind)到同一端口。如果服务器端套接字未关闭,则端口仍在使用中,因此 bind 失败。

通常,套接字程序的服务器端有一个围绕accept 的循环,以允许它处理来自许多客户端的连接。这样 bindlisten 只被调用一次。

while (connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size1)) {
printf("\n I got a connection from (%s , %d)",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
bytes_recieved = recv(connected,recv_data,1024,0);
recv_data[bytes_recieved] = '\0';
send(connected, ser_data,50, 0);
close(connected);
}

关于c - C套接字中需要多个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10630149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com