2016-04-04 23 views
2

başarısız Asio UDP soketleri (daha az sürümünü artırmak)Asio UDP yuvası

asio::io_service service; 
asio::ip::udp::socket sock(service); 
asio::ip::udp::endpoint endpoint(asio::ip::address::from_string("127.0.0.1"), 20100); 

sock.connect(endpoint); 
sock.send(buffer("testing\n")); 

std::string buffer; 
size_t length = sock.receive(asio::buffer(buffer)); <--- spawn exception 

ile gönderme/alma için bu kodu çalıştı ancak aşağıdaki hata var alırsınız: Burada yanlış

An existing connection was forcibly closed by the remote host 

Something? Herhangi bir yardım için teşekkürler!

+0

@EJP Bu mümkündür -> http://stackoverflow.com/questions/34224443/an-existing-connection-was-forcibly-closed-by-the-remote-host-when-listening-f – leon22

+0

Can you can Lütfen 20100 numaralı bağlantı noktasına bağlı bir UDP soketinin olduğunu doğrulayın. Bağlı bir UDP soketi için, hedef adreste alıcı yoksa, 'gönder()' i çağırdıktan sonra, 'send()' ve 'take()' işlevlerine yapılan çağrılar 'ECONNREFUSED' ile başarısız olabilir. –

+0

@TannerSansbury Bunu nasıl doğrulayabilirim? (TCP soketleri kullandığımda hiç sorunum yok, ancak hız nedeniyle UDP'ye geçmem gerekiyor) – leon22

cevap

1

UDP sunucunuzu nasıl oluşturduğunuzu bilmiyorum, ancak bunun yanlış bir şey olduğunu düşünüyorum.

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

int main() 
{  
    struct sockaddr_in addr; 
    int fd, cnt, ret; 

    if ((fd=socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 
     printf("error"); 
     exit(1); 
    } 

    memset(&addr, 0, sizeof(addr)); 
    addr.sin_family=AF_INET; 
    addr.sin_addr.s_addr=inet_addr("127.0.0.1"); 
    addr.sin_port=htons(9090); // address which is not bound 

    ret = connect(fd,(struct sockaddr *)&addr,sizeof(addr)); 
    char buf[512]; 

    cnt = send(fd, "hello", sizeof("hello"), 0); 
    if(cnt < 0) 
     perror("send:"); 
    cnt = recv(fd, buf, 512, 0); // error: Connection refused 
    if(cnt < 0) 
     perror("recv:"); 

    return 0; 
} 

hiçbir udp soket portu send() başarır 9090. ama recv() bağlıdır localhost veya herhangi barındırıcımda udp port 9090 veri göndermek için deneyin: Sana olsun hata mesajı açıklamak için bir örnek program yazmak başarısız olur. man page for udp göre:

All fatal errors will be passed to the user as an error return even when the socket is not connected. This includes asynchronous errors received from the network. You may get an error for an earlier packet that was sent on the same socket. This behaviour differs from many other BSD socket implementations which don't pass any errors unless the socket is connected. Linux's behaviour is mandated by According to the rfc 1122

diyor ki:

UDP MUST pass to the application layer all ICMP error messages that it receives from the IP layer. Conceptually at least, this may be accomplished with an upcall to the ERROR_REPORT routine

send() başarır, ancak bir ICMP hata mesajı neden sonra recv() Olabilir Bu hatayı (görür, (eğer tcpdump'i görebilir) aynı yuvada () gönderilen önceki bir paket için bir hata alın, bu yüzden başarısız olur.