- 论坛徽章:
- 0
|
客户端测试代码: 在私网
#include <iostream>
using namespace std;
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
struct sockaddr_in Get_ServerAddr(char *ip,int port)
{
struct hostent *host;
struct sockaddr_in ServerAddr1;
bzero(&(ServerAddr1.sin_zero),8);
ServerAddr1.sin_family =AF_INET;
ServerAddr1.sin_port =htons(port);
if(inet_aton(ip,&ServerAddr1.sin_addr)==0) //假如是域名
{
host=gethostbyname(ip);
if(host==NULL)
{
cout<<"不合法的域名"<<endl;
}
if(host->h_addrtype!=AF_INET)
{
cout<<"不合法的域名"<<endl;
}
ServerAddr1.sin_addr=*(struct in_addr *)(host->h_addr_list[0]);
}
return ServerAddr1;
}
int main()
{
int fd=socket(AF_INET,SOCK_DGRAM, 0);
struct sockaddr_in fLocalAddr,ServerAddr,remote;
memset(&fLocalAddr, 0, sizeof(fLocalAddr));
fLocalAddr.sin_family = AF_INET;
fLocalAddr.sin_port = htons(8080);
fLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
char buf[1024];
int len;
ServerAddr=Get_ServerAddr("218.28.20.150",8080);
int n;
socklen_t fromlen = sizeof(ServerAddr);
strcpy(buf,"hello\r\n");
int err =bind(fd,(sockaddr *)&fLocalAddr, sizeof(fLocalAddr));
sendto(fd,buf, strlen(buf), 0, (const struct sockaddr*)&ServerAddr,sizeof(ServerAddr));
//先给服务器发送一个消息
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
while(1)
{
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
select(fd+1,&rfds,NULL,NULL,NULL); //然后一直接收
len=recvfrom(fd,buf,1023,0,(sockaddr *)&remote,&fromlen);
buf[len]='\0';
cout<<"recv: "<<buf<<endl;
cout<<"ip======="<<inet_ntoa(remote.sin_addr)<<" port===="<<ntohs(remote.sin_port)<<endl;
}
return 0;
}
服务器端测试代码: 在公网
#include <iostream>
using namespace std;
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
struct sockaddr_in fLocalAddr,remote,fLocalAddr1;
pthread_t tt;
void *t_main(void *arg)
{
char buf1[1024];
strcpy(buf1,"22222222222222222222222222222222222222\r\n");
int fd=socket(AF_INET,SOCK_DGRAM, 0); //新建了socket,也对那个地址发,但是对方收不到
/* memset(&fLocalAddr1, 0, sizeof(fLocalAddr1)); //bind 不 bind 无所谓吧?
fLocalAddr1.sin_family = AF_INET;
fLocalAddr1.sin_port = htons(8081);
fLocalAddr1.sin_addr.s_addr = htonl(INADDR_ANY);
int err =bind(fd,(sockaddr *)&fLocalAddr1, sizeof(fLocalAddr1));*/
while(1)
{
memset(buf1,0,1024);
strcpy(buf1,"22222222222222222222222222222222222222\r\n");
sendto(fd,buf1, strlen(buf1), 0, (const struct sockaddr*)&remote,sizeof(remote));
sleep(1);
}
}
int main()
{
int fd1=socket(AF_INET,SOCK_DGRAM, 0);
memset(&fLocalAddr, 0, sizeof(fLocalAddr));
fLocalAddr.sin_family = AF_INET;
fLocalAddr.sin_port = htons(8080);
fLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
int err =bind(fd1,(sockaddr *)&fLocalAddr, sizeof(fLocalAddr));
char buf[10240];
socklen_t fromlen = sizeof(fLocalAddr);
int len;
len=recvfrom(fd1,buf,10230,0,(sockaddr *)&remote,&fromlen); //服务器先接收客户端一个消息,保存它的地址
buf[len]='\0';
cout<<"ip======="<<inet_ntoa(remote.sin_addr)<<" port===="<<ntohs(remote.sin_port)<<endl;;
strcpy(buf,"1111111111111111111111111111111\r\n");
pthread_create(&tt,NULL,&t_main,NULL);
while(1)
{
sendto(fd1,buf, strlen(buf), 0, (const struct sockaddr*)&remote,sizeof(remote));
//接收客户端消息的那个socket可以回复发通
sleep(1);
}
return 0;
}
[[i] 本帖最后由 lvliangliang 于 2006-10-19 10:04 编辑 [/i]] |
|