gpt4 book ai didi

c - 'Destination unreachable' ('Port unreachable' ) 将数据发送到 127.0.0.1 (localhost) 上的 UDP 套接字时

转载 作者:行者123 更新时间:2023-12-03 11:52:19 25 4
gpt4 key购买 nike

我在 C 应用程序的套接字之间发送数据。我首先绑定(bind)“接收器”以监听 127.0.0.1 (localhost) 的某个端口(阻塞 - 在单独的线程中)。然后我开始向这个端口发送数据。
我从程序中得到以下打印输出:

*** Created server socket good. Server socket is 4 
(..)
port nr 4793 & ip 127.0.0.1***Bind succeed..
Waiting for recvfrom() to complete...
waiting to receive msg*** Start sending messages..
sendto() successs
sendto() successs
(..)
sendto() successs

使用环回接口(interface)运行 Wireshark,我注意到数据包被发送到正确的目标地址。
但是,我收到错误“无法访问目标”(“无法访问端口”)”。尽管关闭了防火墙,但我得到了同样的错误。

代码是否有错误;我该如何解决这个问题?
下面是代码的摘录:

头文件中的一些定义:
#define  PORT_NUM   4793    // Arbitrary port number for the server
#define IP_ADDR "127.0.0.1" // IP address of server1 (*** HARDWIRED ***)
#define SYMBOL_SIZE 1024 //todo..on a different position also

程序启动发送者和接收者:
    ret = init_sender();
if (ret != OF_STATUS_OK) {
OF_PRINT_ERROR(("ERROR, init_sender() failed\n"))
goto error;
}

// >>> Step #3 <<<
// Wait to receive a message from client..Since receiver is waiting-do this in separate thread.
pthread_t thread1;
int iret1;
char *message1 = "Thread 1";

iret1 = pthread_create( &thread1, NULL, init_receiver, (void*) message1);

ret = encode(); //this sends messages
if (ret != OF_STATUS_OK) {
OF_PRINT_ERROR(("ERROR, encode() failed\n"))
goto error;
}


if (ret != OF_STATUS_OK) {
OF_PRINT_ERROR(("ERROR, init_receiver() failed\n"))
goto error;
} //todo..ok after init_receiver?
ret = init_tx_simulator(); /* must be done after init_sender */
if (ret != OF_STATUS_OK) {
OF_PRINT_ERROR(("ERROR, init_tx_simulator() failed\n"))
goto error;
}

print_params();
//print_rx_stats();
#endif /* OF_DEBUG */
#endif

pthread_join (thread1, NULL); //We want the receiver thread to be finished before finishing it..

发件人:
of_status_t encode (void)

{

// Create a socket
// - AF_INET is Address Family Internet and SOCK_DGRAM is datagram
client_s = socket(AF_INET, SOCK_DGRAM, 0);
if (client_s < 0)
{
printf("*** ERROR - socket() failed \n");
exit(-1);
}
printf("*** Sender -- socket created \n");

printf("*** Start sending messages.. \n");



// >>> Step #2 <<<
// Fill-in server1 socket's address information
server_addr.sin_family = AF_INET; // Address family to use
server_addr.sin_port = htons(PORT_NUM); // Port num to use
server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use


//n is number of symbols to sent -- repair + original..
int j= 0;
for (j=0; j < 10 ; j++ )
{
retcode = sendto(client_s, (const void*) encoding_symbols_tab[j], symbol_size, 0,
(struct sockaddr *)&server_addr, sizeof(server_addr) );

if (retcode < 0)
{
// printf("*** ERROR - sendto() failed.. retcode is %d \n", retcode);
printf ("errno is %s",strerror (errno));
exit(-1);
}
else
printf("sendto() successs \n");

}

retcode = close(client_s);
if (retcode < 0)
{
printf("*** ERROR - close() failed \n");
exit(-1);

接收者:
    init_receive () {


// >>> Step #1 <<<
// Create a socket
// - AF_INET is Address Family Internet and SOCK_DGRAM is datagram
server_s = socket(AF_INET, SOCK_DGRAM, 0);
if (server_s < 0)
{
printf("*** ERROR - socket() failed \n");
exit(-1);
}
else
printf("*** Created server socket good. Server socket is %d \n", server_s);


// >>> Step #2 <<<
// Fill-in my socket's address information
server_addr.sin_family = AF_INET; // Address family to use
server_addr.sin_port = htons(PORT_NUM); // Port number to use
server_addr.sin_addr.s_addr = htonl(IP_ADDR); // Listen on this IP..
printf("***We have a server socket; and now we will try to bind it with the IP_ADDR-local host -- that we sent.. \n port nr %d & ip %s", PORT_NUM, IP_ADDR);

retcode = bind(server_s, (struct sockaddr *)&server_addr,
sizeof(server_addr));

if (retcode < 0)
{
printf("*** ERROR - bind() failed \n");
exit(-1);
}
else
printf("***Bind succeed.. \n");


waitToReceiveMessageFromClient (); //call it and wait to receive messages.

return OF_STATUS_OK;

no_mem:
return OF_STATUS_ERROR;
}

waitToReeiveMessageFromClient () {

int
waitToReceiveMessageFromClient ( void )
{
printf("Waiting for recvfrom() to complete... \n");
addr_len = sizeof(client_addr);
encoding_symbols_tab = (void*) calloc(MAX_N, sizeof(void*));
int i = 0;
//Run until we have finished all messages from the Sender.

while (numberMessagesReceivedCounter < tot_nb_source_symbols + tot_nb_repair_symbols )
{
printf ("waiting to receive msg");

encoding_symbols_tab[i] = (void*)calloc(1, SYMBOL_SIZE); //each
//change to non-blocking
//if(fcntl(server_s, F_SETFL, fcntl(server_s, F_GETFL) | O_NONBLOCK) < 0) {
// printf ("error for the moment");
//}

retcode = recvfrom(server_s, in_buf, sizeof(in_buf), 0,
(struct sockaddr *)&client_addr, &addr_len);

if (retcode < 0)
{
fprintf (stderr,"errno is %d", errno);
exit(-1);
}
else
{ //copy all symbols to its right location from received message.
printf("*** Received message.. \n");

int k= 0;
for (k= 0; k < sizeof (in_buf) ; k++ )
{
encoding_symbols_tab[numberMessagesReceivedCounter][k] = (char*) in_buf[k];
}
numberMessagesReceivedCounter++;

//Finished ready to do decoding.
if (numberMessagesReceivedCounter == tot_nb_source_symbols + tot_nb_repair_symbols )
{
receive_and_decode ();
break;
}

}
}
//add to buffer... numberMessagesReceivedCounter

}



}

最佳答案

这一行(在发件人中):

server_addr.sin_addr.s_addr = inet_addr(IP_ADDR);

是正确的。

此行(在接收器中):
server_addr.sin_addr.s_addr = htonl(IP_ADDR);

是错的。它甚至可以干净地编译(或者做到了?),这有点令人惊讶。

关于c - 'Destination unreachable' ('Port unreachable' ) 将数据发送到 127.0.0.1 (localhost) 上的 UDP 套接字时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9863609/

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