- 论坛徽章:
- 5
|
本帖最后由 seanking1987 于 2017-04-25 16:05 编辑
服务端代码:- <blockquote>#include <stdio.h>
复制代码 #include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <net/if.h>
#define ip_server "2001:da8:e000::1:1:89"
#define ip_port 3322
int main(int argc, char *argv[])
{
struct sockaddr_in6 name;
struct ipv6_mreq mreq;
int sock;
int val;
int rc=0;
int recv_len=0;
char recvbuf[256];
struct sockaddr_in6 from_addr6;
socklen_t addr_len=0;
fd_set readfdset;
struct timeval tv;
memset(&name,0,sizeof (name));
name.sin6_family = AF_INET6;
name.sin6_port = htons(ip_port);
rc=inet_pton(AF_INET6,ip_server, &name.sin6_addr);
if(rc!=1)
{
printf("open_udp_socket_v6 error,inet_pton error!\n" ;
return -1;
}
//name.sin6_addr = in6addr_any;
if((sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
printf("open_udp_socket error(socket error)\n" ;
return -1;
}
val = 1;
/*bind过程中本地地址可重复使用*/
if(setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)) < 0)
{
close(sock);
printf("open_udp_socket_v6 error(setsockopt error)\n" ;
return -1;
}
if(bind(sock,(struct sockaddr*)&name,sizeof(name)) < 0)
{
close(sock);
printf("open_udp_socket_v6 error(bind error)\n" ;
return -1;
}
while(1)
{
FD_ZERO(&readfdset);
FD_SET(sock,&readfdset);
tv.tv_sec = 0;
tv.tv_usec = 1000000;
rc=select(sock+1,&readfdset,NULL,NULL,&tv);
if(rc<0)
{
printf("receive failed(select error)\n" ;
continue;
}
if(FD_ISSET(sock,&readfdset))
{
addr_len = sizeof(from_addr6);
memset(&from_addr6, 0, addr_len);
memset(recvbuf, 0, sizeof(recvbuf));
recv_len = recvfrom(sock,recvbuf, sizeof(recvbuf) - 1, 0,(struct sockaddr *)&from_addr6, &addr_len);
if(recv_len < 0)
{
printf("receive error(recvfrom error)\n" ;
continue;
}
printf("get length:%d,message is %s\n",recv_len,recvbuf);
}
}
}
客户端:
- <blockquote>#include <time.h>
复制代码 #include <time.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <net/if.h>
#include <pthread.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <sys/socket.h>
extern int errno;
#define server_ip "2001:da8:e000::1:1:89"
#define server_port 3322
#define client_ip "2001:da8:e000::1:1:90"
#define client_port 2233
void errorPrint()
{
char *errMsg= strerror(errno);
printf("errno:%d,errorMsg:%s\n",errno,errMsg);
}
int main(void)
{
struct sockaddr_in6 server;
struct sockaddr_in6 client;
int sock;
int val;
int rc=0;
char source_ip[40]={0};
char dest_ip[40]={0};
short src_port=1111;
short dest_port=2222;
char sendbuf[40]={0};
char recvbuf[1024];
int send_len=0;
int recv_len=0;
memset(&server,0,sizeof(client));
memset(&client,0,sizeof(client));
server.sin6_family = AF_INET6;
client.sin6_family = AF_INET6;
server.sin6_port = htons(server_port);
client.sin6_port = htons(client_port);
rc=inet_pton(AF_INET6,client_ip ,&client.sin6_addr);
if(rc!=1)
{
printf("open_udp_socket_v6 error,inet_pton error!\n" ;
return -1;
}
rc=inet_pton(AF_INET6,server_ip,&server.sin6_addr);
if(rc!=1)
{
printf("open_udp_socket_v6 error,inet_pton error!\n" ;
return -1;
}
if((sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
printf("open_udp_socket_v6 error(socket error)\n" ;
return -1;
}
val = 1;
/*bind过程中本地地址可重复使用*/
if(setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *)&val,sizeof(val)) < 0)
{
close(sock);
printf("open_udp_socket_v6 error(setsockopt error)\n" ;
return -1;
}
if(bind(sock,(struct sockaddr*)&client,sizeof(client)) < 0)
{
close(sock);
printf("open_udp_socket_v6 error(bind error)\n");
return -1;
}
if(connect(sock, (struct sockaddr *)&server, sizeof(server)) == -1)
{
errorPrint();
close(sock);
printf("open_udp_socket_v6 error(connect error)\n");
return -1;
}
strcpy(sendbuf,"testmess!");
send_len=strlen(sendbuf);
rc=sendto(sock,sendbuf,send_len,0,(struct sockaddr*)&server,sizeof(server));
if(rc!=send_len)
{
errorPrint();
close(sock);
printf("open_udp_socket_v6 error(sendto error)\n");
return -1;
}
/*
while(1)
{
memset(recvbuf,0,sizeof(recvbuf));
recv_len=recv(sock,recvbuf,1024,0);
printf("recv_len is %d\n",recv_len);
if(recv_len==0)
continue;
if(recv_len<0)
{
printf("recv error,errno:%s\n",strerror(errno));
continue;
}
}
*/
return 0;
}
目前的问题是:
1.当客户端给服务端发包时候,通过抓包能够看到客户端发来的包,但是程序收不到报文,抓包如下:
- 15:38:51.427517 IP6 (hlim 64, next-header UDP (17) payload length: 17) 2001:da8:e000::1:1:90.infocrypt > 2001:da8:e000::1:1:89.3322: [udp sum ok] UDP, length 9
复制代码 PS:如果是客户端,服务端在同一台主机,可以接收到报文
- get length:9,message is testmess!
复制代码 2.当客户端放开注释的最后一段recv代码时候,报错:
- recv_len is -1
- recv error,errno:Permission denied
复制代码
|
|