#include "CComm.h"
//---------全局变量-----------//
//端口号列表
struct pTable{
char cName[10];
int iport;
}Port_Number[4];
//路由表
struct routTable {
int DesNode; //目的结点
char NextNode; //邻结点
char node1;
char node2;
char node3;
char node4;
char node5;
int iHop;
int iAge;
int iPort;
}RoutTable[10];
char router_Array[10][10];
int Tag=0;
char msg[5];
char send_To;
int tempport=0;
char temp_Array[70];
CComm::CComm()
{
//构造函数
ListenSocket = INVALID_SOCKET; // 开始设置为INVALID_SOCKET
#ifdef _WIN32 // 如果是win32系统
WORD VersionRequested = MAKEWORD(1,1); //获取Socket版本号
WSADATA wsaData; //协商时使用的变量
WSAStartup(VersionRequested, &wsaData); // 启动winsock服务
if ( wsaData.wVersion != VersionRequested )
{
printf("Wrong version or WinSock not loaded\n");
fflush(0);
}
#endif
}
//析构函数
CComm::~CComm()
{
if ( ListenSocket != INVALID_SOCKET )
closesocket( ListenSocket ); // 如果已经创建、则关闭
#ifdef _WIN32 // 调用WSACleanup函数释放资源
WSACleanup();
#endif
}
bool CComm::SendMessage( char *Msg, int Len, char *host, short port )
{
signed int Sent;
hostent *hostdata;
if ( atoi(host) ) // 是否IP地址为标准形式
{
u_long ip = inet_addr( host );
hostdata = gethostbyaddr( (char *)&ip, sizeof(ip), PF_INET );
}
else // 否则则可能是机器名
{
hostdata = gethostbyname( host );
}
if ( !hostdata )
{
printf("获得机器名错误\n");
fflush(0);
return false;
}
sockaddr_in dest; // 发送目标地址
dest.sin_family = PF_INET;
dest.sin_addr = *(in_addr *)(hostdata->h_addr_list[0]);
dest.sin_port = htons( port );
printf("信息已经被发送到主机 %s 端口为 %i\n", inet_ntoa(dest.sin_addr), ntohs(dest.sin_port));
//数据发送
Sent = sendto(ListenSocket, Msg, Len, 0, (sockaddr *)&dest, sizeof(sockaddr_in));
if ( Sent != Len ) //发送长度是否与实际长度相等
{
printf("错误发送UDP信息\n");
fflush(0);
return false;
}
return true;
}
void *CComm::ListenThread( void *data )
{
char bufR[4096];
CComm *Comm = (CComm *)data;
int len = sizeof(Comm->client1);
while(1) // 一直循环,直到有数据来
{
//接收数据
int result = recvfrom( Comm->ListenSocket, bufR, sizeof(bufR), 0, (sockaddr *)&Comm->client1, (socklen_t *)&len);
if ( result > 0 )
{
bufR[result] = 0; //添加结束符
tempport=ntohs(Comm->client1.sin_port); //网络顺序转为主机顺序
if(bufR[0]=='@')
{
strcpy(msg,bufR);
if(int(msg[2])=='0')
{
if(msg[3]==RoutTable[0].DesNode+0x30)
printf("\n收到%c测试信息,经过了%c跳!\n",msg[3],msg[4]);
else
{
printf("\n丢失从%c到%c测试信息!\n",msg[3],msg[1]);
printf("\n>>D%c",msg[1]);
}
}
else
{ msg[2]=char(int(msg[2])-1); //TTL减一
msg[4]=char(int(msg[4])+1); //hop数加一
send_To=msg[1];
Tag=1;
}
}
else
{
//路由表
strcpy(temp_Array,bufR);
Comm->changeRouter(tempport,temp_Array);
}
fflush(0);
}
}
}
void *CComm::SendThread( void *data )
{
signed int SentS;
char bufS[70];
CComm *Comm = (CComm *)data;
int len = sizeof(Comm->client2);
Comm->client2.sin_addr.S_un.S_un_b.s_b1=127;
Comm->client2.sin_addr.S_un.S_un_b.s_b2=0;
Comm->client2.sin_addr.S_un.S_un_b.s_b3=0;
Comm->client2.sin_addr.S_un.S_un_b.s_b4=1;
sockaddr_in dest; // 发送目标地址
dest.sin_family = PF_INET;
dest.sin_addr=Comm->client2.sin_addr;
//数据发送
while(1)
{ bufS[70]=0;
int iT;
for(int i=0;i<10;i++) //do with routtable
{
iT=i*7;
bufS[iT]= RoutTable[i].DesNode+0x30;
bufS[iT+1]= RoutTable[i].NextNode;
bufS[iT+2]= RoutTable[i].node1;
bufS[iT+3]= RoutTable[i].node2;
bufS[iT+4]= RoutTable[i].node3;
bufS[iT+5]= RoutTable[i].node4;
bufS[iT+6]= RoutTable[i].node5;
}
Sleep(2000); //每2秒发送一次
if(Tag==0)
for(int kk=1;kk<4;kk++)
{ if(Port_Number[kk].iport!=0)
{
dest.sin_port = htons(Port_Number[kk].iport);
SentS = sendto(Comm->ListenSocket, bufS, strlen(bufS), 0, (sockaddr *)&dest, sizeof(sockaddr_in));
Sleep(300);
}
}
else
{
bool bmaysend=false;
for(int i=0;i<10;i++)
{
if(RoutTable[i].DesNode+0x30==send_To)
{
if(RoutTable[i].NextNode =='*')
{
printf("\n收到%c发送的信息,经过了%c跳!\n",msg[3],msg[4]);
bmaysend=false;
Tag=0;
}
else
{
dest.sin_port = htons(RoutTable[i].iPort);
bmaysend=true;
printf("\n>>F%c\n",send_To,RoutTable[i].iPort);
}
}
}
if(bmaysend)
{
//调用sendto函数发送数据
SentS = sendto(Comm->ListenSocket, msg, strlen(msg), 0, (sockaddr *)&dest, sizeof(sockaddr_in));
Tag=0;
}
}
}
}
//地址绑定,注意在UDP协议中,不需要listen,这里函数listen只是绑定一个端口
bool CComm::Listen( int PortNum )
{
ListenSocket = socket(PF_INET, SOCK_DGRAM, 0);
if ( ListenSocket == INVALID_SOCKET )
{
printf("Error: socket创建失败\n");
fflush(0);
return false;
}
srv.sin_family = PF_INET;
srv.sin_addr.s_addr = htonl( INADDR_ANY ); // 任何地址
srv.sin_port = htons( PortNum );
if ( bind( ListenSocket, (struct sockaddr *)&srv, sizeof(srv)) != 0 )
{
printf("Error: 绑定失败\n");
fflush(0);
closesocket( ListenSocket );
return false;
}
int ThreadID; // 线程id
评论0