//初始化获取服务器 void connectToBalanceServer() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(1, 1); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { //printf("WSAStartup failed with error: %d\n", err); return ; } if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1) { //printf("Could not find a usable version of Winsock.dll\n"); WSACleanup(); return ; } /* else printf("The Winsock 1.1 dll was found okay\n"); */ SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0); //send SOCKADDR_IN balanceServerAddr; balanceServerAddr.sin_addr.S_un.S_addr=inet_addr(BalanceServIP); balanceServerAddr.sin_family = AF_INET; balanceServerAddr.sin_port = htons(BalanceServPort); //addrSrv.sin_port = htons(balanceListenPort); /* clientConnectRequst cliConReq; cliConReq.bitRate=bitRate; cliConReq.flag=1; cliConReq.frame_rate=frame_rate; cliConReq.resolution=resolution; */ //发送请求设置 clientConnectRequst cliConReq; cliConReq.bitRate=5000; cliConReq.flag=FLAG_CLIENT_CONNECT_REQUEST; cliConReq.frame_rate=5000; cliConReq.resolution=5000; cliConReq.reConPort=10001; cliConReq.channel = channel; cliConReq.cameraID = cameraID; //初始频道不正确 if (cliConReq.channel < 1) { cout << "频道为0,设置错误" << endl; isStop = 1; cin.get(); exit(0); } char sendBuf[SEND_TO_BALANCE_SERVER_BUFFER_SIZE]; for(int i=0;i<sizeof(clientConnectRequst);i++) { sendBuf[i]=((char*)(&cliConReq))[i]; } sendto(sockClient,sendBuf,SEND_TO_BALANCE_SERVER_BUFFER_SIZE,0,(SOCKADDR *)&balanceServerAddr,sizeof(balanceServerAddr)); //reconnect thread SOCKET * reConnectSock = &sockClient; HANDLE hThread5; DWORD ThreadID5; hThread5=CreateThread(NULL,0,reConnect,reConnectSock,0,&ThreadID5); isConnect=0; //listen int len = sizeof(SOCKADDR); char recBuff[RECEIVE_FROM_BALANCE_SERVER_BUFFER_SIZE]; while(isConnect!=1) { memset(recBuff,0,RECEIVE_FROM_BALANCE_SERVER_BUFFER_SIZE); recvfrom(sockClient,recBuff,sizeof(recBuff),0,(SOCKADDR *)&addrSrv,&len); //receive ok serge //cout<<" receive data\n"; ServerInfoBlock * pServerBlock; pServerBlock =((ServerInfoBlock *)recBuff); if((*pServerBlock).flag==FLAG_SERVER_INFO) { isConnect=1; if (pServerBlock->server_id<0) { //std::cout << "no server avalible now\n"; // cin.get(); //调整为默认频道 channel = 1; cameraID = 1; connectToBalanceServer(); return; //exit(0); } serverFromBalance=ServerInfo(*pServerBlock); //tempServer.showInfo(); //std::cout<<"the server ID is : "<<serverFromBalance.getServerID()<<std::endl; } else { //std::cout<<"recieve error server info\n"; } } //printf("the %s says: %s \n",inet_ntoa(addrClient.sin_addr),recBuff); std::string temp =serverFromBalance.getServerPublicIP(); unsigned int i; for(i=0;i<temp.length();i++) { ServIP[i]=temp[i]; } ServIP[i]='\0'; /* char aa[20]="192.168.1.109\0"; int i; for(i=0;i<temp.length();i++) { ServIP[i]=aa[i]; } ServIP[i]='\0'; */ ServPort=serverFromBalance.getServerDataPort(); retranport=serverFromBalance.getServerRetransmitPort(); commandport=serverFromBalance.getServerCommandPort(); //cout<<"serverIP: "<<ServIP<<endl; //cout<<"tranport: "<<ServPort<<endl; //cout<<"retranport: "<<retranport<<endl; //cout<<"commandport: "<<commandport<<endl; }