Hi, Everyone, I'm writing a piece of code to test the performance of
connected udp socket.
sometimes It returns "connection refused" error, errno is 111 when I
try to write data to the peer(use raw socket), and send failed.

I tried 4 scenarios
1. server open udp socket, client use connect udp socket -----> ok, no
packets lost.
2. server open raw socket, client use connectless udp sock -----> ok,
no packets lost.
2. server open raw socket, client use connect udp sock -----> packets
lost.


The server code:

int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;
socklen_t len=sizeof(cliaddr);
int count = 1;
setbuf(stdout, NULL);


/* check args */
if(argc != 3) {
printf("usage: bindedIPaddress udpreceive \n");
exit(1);
}

//sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */
sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP); /* create a socket
*/

/* init servaddr */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0){
printf("inet_pton error for %s\n", argv[1]);
exit(1);
}
servaddr.sin_port = htons(GTP_U_PORT);

if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) ==
-1){
perror("sockfd, bind error");
exit(1);
}

char mesg[MAXLINE];
memset(mesg,0,MAXLINE);
int n;
for(;
{
len=sizeof(cliaddr);

n = recvfrom(sockfd, mesg, MAXLINE, 0, (struct sockaddr
*)&cliaddr, &len);

if ( n < 0 ){
printf("receive error, errno:%d, %s\n", errno,
strerror(errno) );
}else {
printf("%5d: recv message from 0x%x, len:%d\n",count++,
ntohl(cliaddr.sin_addr.s_addr), n);
}
}

return 0;
}


the client code:

int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr,cliaddr;
int ret;
char mesg[MAXLINE] = {0};
int len = -1;
int n = atoi(argv[3]);
int i;

struct timeval start;
struct timeval end;
int most = 0;
int least = 999999;
int average = 0;

setbuf(stdout, NULL);

/* check args */
if((argc != 4)) {
printf("usage: udpsend srcIPaddress dstIPaddress n\n");
exit(1);
}

/* init cliaddr */
bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
if(inet_pton(AF_INET, argv[1], &cliaddr.sin_addr) <= 0){
printf("[%s] is not a valid IPaddress\n", argv[1]);
exit(1);
}
cliaddr.sin_port = htons(CLI_PORT);

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(bind(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr)) == -1)
{
perror("sockfd, bind error");
exit(1);
}

/* init servaddr */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(GTP_U_PORT);
if(inet_pton(AF_INET, argv[2], &servaddr.sin_addr) <= 0) {
printf("[%s] is not a valid IPaddress\n", argv[2]);
exit(1);
}

if ( -1 == connect(sockfd, (struct sockaddr *)&servaddr,
sizeof(servaddr)) ){
printf("sockfd, connect to server failed.\n");
close(sockfd);
exit(1);
}

for ( i = 0; i < n; ++i ){
gettimeofday(&start , NULL);

len = sendto(sockfd,mesg, MAXLINE,0,0, 0 );
gettimeofday(&end , NULL);
if ( len == -1 ){
printf("sendto err! len: %d, errno:%d, %s\n", len, errno,
strerror(errno));
}else if (len == 0){
printf("why here?, len: %d, errno:%d, %s\n", len, errno,
strerror(errno));
} else {
printf("success to send data, len: %d\n", len);
}

average = (average * i + (end.tv_sec - start.tv_sec) * 1000000 +
(end.tv_usec - start.tv_usec))/(i+1);

if ( most < ((end.tv_sec - start.tv_sec) * 1000000 +
(end.tv_usec - start.tv_usec)) )
most = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec -
start.tv_usec);

if ( least > ((end.tv_sec- start.tv_sec) * 1000000 +
(end.tv_usec - start.tv_usec)) )
least = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec
- start.tv_usec);
}

printf("\n\nsend packages number: %d, time used, most: %d, least:
%d, average: %d\n\n", i, most, least, average );

close(sockfd);

return 0;
}