English 中文(简体)
C. 书状中需要多重联系
原标题:Need multiple connections in C socket

我写了以下法典,作为连接两个方案的中间体。 有一个服务器方案,和一个客户方案,分两个不同的系统。 预计该法典将在这两个方案之间发挥中间作用。

#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] =   ;
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("
 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] =   ;   
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)

我在法典中应当作什么修改来纠正这一错误? 我正在着手工作。 提前感谢。

EDIT: I have removved the comment from close(sock)and added close(connect) in the function cli_con. The code on the client side is given below:

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就失效。

通常,袖珍方案的服务器部分有一个环绕accept的路段,以便处理许多客户的联系。 仅一次使用<条码>。

while (connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size1)) {
    printf("
 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] =   ;   
    send(connected, ser_data,50, 0);
    close(connected);
}
问题回答

这意味着你在5,000港再次听话,但已经有一个听从该港口的方案(可能是你早先的方案,它适当地关闭了港口)。 将港口数目改为另一个价值,或关闭港口的任何听觉。

既然你重新排在了赖恩之下,你就可以把“网状-nlp”作为根基,看看港口开放哪些方案。

One pointer for you is that, you should close both the listen socket and the socket which you obtain from the accept call.

close(connected);
close(sock);

此外,在审案卷使用后,需要一些时间才能由顾问办公室释放,因此,可能会出现“已经使用的压力”错误。 如果贵国的《刑法典》部分内容得到适当执行,你可以再次适当检查。

此外,由于你可以试图在你的主要职能中增加听众的制作代码,并将之作为使自己发挥作用的理由。 通常遵循的机制是制作一个倾听书,并利用它接受客户的多种联系。

我希望,在你原来的法典中,你有适当的记忆分配、初始化等,用于数据与复制;数据。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签