/*-------------------------------------------------- * Function: SendZMXEvent * Description: * Send ZMX event msssage via UDP to @port on local * Parameters: * @port: local port to send * @buffer: event message to send * @bufferLength: event message length * Returns: *--------------------------------------------------*/ int SendZMXEvent(int port, char *buffer, int bufferLength) { int udpSocket = -1; int ret = 0; ZInfo4(DBG_MISC, "Will send buffer to port->[%d], buffer: %s, length: %d\n", port, buffer, bufferLength); if ((udpSocket = CreateUDPSocket("127.0.0.1", port, 1)) == -1) { ZError(DBG_INIT, "Unable to create server socket."); return -1; } if(buffer != NULL) { ret = write(udpSocket, buffer, bufferLength); if(ret > 0) { ZInfo4(DBG_MISC, "%d bytes wrote", ret); close(udpSocket); return 0; } else { ZError(DBG_MISC, "Error happened when sending ZMX, %d byte wrote", ret); close(udpSocket); return -1; } } else { ZError(DBG_MISC, "ZMX Message is empty"); close(udpSocket); return -1; } }
void Socket::CreateUDPSocketToSendAndReceive() { // Create Socket CreateUDPSocket(); // Set to not block SetNonBlocking(); }
bool Socket::CreateAndConnectUDPSocket(const AdressImplementation& p_connectAdress) { // Create Socket CreateUDPSocket(); // Connect to adress return ConnectUDPSocket(p_connectAdress); }
void Socket::CreateAndBindUDPSocket(const AdressImplementation& p_myAdress) { // Create socket CreateUDPSocket(); // Bind socket to Receive incomming BindSocket(p_myAdress); // Set socket to non blocking SetNonBlocking(); }
UDPClient::UDPClient(string address, unsigned int port) : Client(address, port) { this->serverAddressInfo = (sockaddr*)CreateAddressInfoForClient(); _udp_socket = CreateUDPSocket(); SetReceiveTimeout(this->_udp_socket, GetTimeout(UDP_RECV_TIMEOUT)); for (auto i = 0; i < PACKAGE_COUNT; i++) { auto _pair = new pair<fpos_t, char*>(); _pair->second = new char[UDP_BUFFER_SIZE]; this->receivedBuffer.push_back(_pair); } }
int SetupAlertSocket() { alert_sess = malloc(sizeof(GENSESS)); alert_sess->name[0] = 0; alert_sess->sessid = -1; alert_sess->state = 0; alert_sess->cpindex = -1; alert_sess->vecindex = 0; alert_sess->pkthandler = NULL; alert_sess->sck = CreateUDPSocket(INADDR_LOOPBACK, ALERT_PORT, &alert_sess->addr); alert_sck = alert_sess->sck; alert_addr = (struct sockaddr *)&alert_sess->addr; return (alert_sck != -1); }
Server::Server(unsigned int port) { this->port = port; _udp_socket = CreateUDPSocket(); CreateTCPSocket(); Bind(_udp_socket); Bind(this->_tcp_socket); SetSendTimeout(this->_tcp_socket); SetReceiveTimeout(_udp_socket, GetTimeout(100)); Listen(); FD_ZERO(&this->clientsSet); FD_ZERO(&this->serverSet); FD_SET(this->_tcp_socket, &this->serverSet); FD_SET(_udp_socket, &this->serverSet); std::cout << "Server started at port: " << this->port << std::endl; }
void *receiveStateFromZQ(void *ctxt) { struct sockaddr_in fromAddr; U32 len; int zqSocket; int bytesRet; char reqBuf[MAX_ZMX_REQ_PKT_LEN]; //Create a socket and bind to ZQService port if ((zqSocket = CreateUDPSocket(ZQ_IP, ZAPP_INPUTCTRD_SOCKET, 0)) == -1) { printf("Unable to create/bind socket to port %d", ZAPP_INPUTCTRD_SOCKET); return (NULL); } len = sizeof(struct sockaddr_in); int state = 0; while(1) { memset(reqBuf, 0, sizeof(reqBuf)); printf("Waiting for events on port->%d\n",ZAPP_INPUTCTRD_SOCKET); printf("Start listener in Inputctrd!!!!!!!!\n"); bytesRet = recvfrom(zqSocket, reqBuf, MAX_ZMX_REQ_PKT_LEN, 0, (struct sockaddr *)&fromAddr, &len); printf("Got an event ->%s", reqBuf); if (bytesRet > 0) { if ((state = getState(reqBuf)) != -1) { sendKeyMode = state; printf("receive state from zqdevice , mode = %d\n", state); } else printf("Get state message from zqdevice failed.\n"); } } }
NetSession* NetSystem::CreateSession(short port) { NetSession *newSession = new NetSession(); NetConnection me; NetAddress* na = new NetAddress(); size_t numAddr = 1; char szHostName[255]; gethostname(szHostName, 255); NetAddressForHost(na, numAddr, AF_INET, szHostName, port, true); me.m_netAddress = *na; NetConnection* newConn = newSession->AddConnection(me.m_netAddress, false); newSession->m_me = newConn; newSession->m_me->m_connectionID = std::string("CHRISTIAN WALKER"); UDPSocket *sock = CreateUDPSocket(&newSession->m_packetQueue, port); newSession->m_sockets.push_back(sock); m_networkSessions.push_back(newSession); return newSession; }
int main(int argc, char *argv[]) { // int sock, rtsock, count, params[2], fd,index_sp,mimo_flag=0,mimo_flag1=0,mimo_flag2=0, average[10]; int sock, rtsock, i,j, counta=0, countb=0, countc=0, count, params[2], fd,index_sp,mimo_flag=0,mimo_flag1=0,mimo_flag2=0, average[10]; int addrLen,k,sum=0,qsum=0,sum1=0,qsum1=0,sum2=0,qsum2=0, number=0, add=0; FILE* f; double alfa; float a,b,c,Xmn,Ymn,Ymn1,Ymn2,Xinput,Yinput,Distance,dp1,dp2,tambah, averageda, averagedb, averagedc, averaged=0.0; struct sockaddr_in addr; char *s, s1[1024]; if((f=fopen("/root/ssf.c","r+"))==NULL) { printf("cannot open this file\n");exit(-1); } if(argc < 2) { fprintf(stderr, "usage: Rtrg_gps [ru|rr|tu|tr] [<address>]\n"); exit(-1); } s = (argc < 3)? "" : argv[2]; if(strcmp(argv[1], "ru") == 0) { if((sock = BindSocket(CreateUDPSocket(), s)) < 0) { exit(-1); } addrLen = sizeof(addr); if(getsockname(sock, &addr, &addrLen) < 0 ) { close(sock); return -1; } printf("Rtrg_gps will receive request at this address %s\n", EncodeIPAddress(s1, &addr)); for(;;){ recv(sock,&index_sp, sizeof(index_sp), 0); //printf("I receive ur index \n"); for(i=0; i< index_sp;i++) { recv(sock, &p[i], sizeof(p), 0); if(strcmp((p[i].essid,"mimos")==0)&&counta<3) // {a=p[i].dis;mimo_flag++;fprintf(f,"%s ESSID: %s Signal level: %d Distance: %f\n",p[i].mac,p[i].essid,p[i].sl,a); {a=p[i].dis;tambah=p[i].sl;mimo_flag++;fprintf(f,"%s ESSID: %s Signal level: %d Distance: %f\n",p[i].mac,p[i].essid,tambah,a); a=a+p[i].dis; counta++; // add += p[i].sl; // averaged = (float)add / 10; // n[i]=0; // a+= n[i]; // printf(" entah macam mana %f\n", averaged); } else if(strcmp((p[i].essid,"mimos1")==0)&&countb<3) {b=p[i].dis;mimo_flag1++;fprintf(f,"%s ESSID: %s Signal level: %d Distance: %f\n",p[i].mac,p[i].essid,p[i].sl,b); b=b+p[i].dis; countb++; } else if(strcmp((p[i].essid,"mimos2")==0)&&countc<3) {c=p[i].dis;mimo_flag2++;fprintf(f,"%s ESSID: %s Signal level: %d Distance: %f\n",p[i].mac,p[i].essid,p[i].sl,c); c=c+p[i].dis; countc++; } printf("%s ESSID: %s Signal level: %d Distance: %f\n",p[i].mac,p[i].essid,p[i].sl,p[i].dis); //add += p[i].sl; //averaged = (float)add / i; // n[i]=0; // a+= n[i]; //printf(" %f\n", averaged); } } add += tambah; averaged = (float)add / 3; printf(" entah macam mana %f\n", averaged); //printf("enter referend point x,y : " ); //scanf ("%f,%f", &Xinput, &Yinput); // key in location of other point //printf ("You enter %f, %f \n", Xinput, Yinput); // calculate mobile node location if(mimo_flag > 0 && mimo_flag1>0 && mimo_flag2 >0 ){ alfa=acos((a*a+100-b*b)/(20*a)); if( isnan(alfa)==0){ Xmn=a*cos(alfa);Ymn1=a*sin(alfa);Ymn2=-Ymn1; Distance=sqrt(((Xmn-Xinput)*(Xmn-Xinput))+((Ymn-Yinput)*(Ymn-Yinput))); dp1=sqrt(Xmn*Xmn + (10-Ymn1)*(10-Ymn1)); dp2=sqrt(Xmn*Xmn + (10-Ymn2)*(10-Ymn2)); if (fabsf(dp1-c) < fabsf(dp2-c)) Ymn=Ymn1; else Ymn=-Ymn2; if ( Xmn < 0 ) Xmn = -Xmn; else Xmn = Xmn; printf("alfa is %f, dp1 is %f,dp2 is %f. The location of mobile node is (%f,%f) \n",alfa,dp1,dp2,Xmn,Ymn); fprintf(f,"The location of mobile node is (%f,%f)\n",Xmn,Ymn); // { // Ymn=Ymn1; // Distance=sqrt((Xmn-Xinput)*(Xmn-Xinput)+(Ymn1-Yinput)*(Ymn1-Yinput)); //measure the distance // } // else // { // Ymn=Ymn2; // Distance=sqrt((Xmn-Xinput)*(Xmn-Xinput)+(Ymn2-Yinput)*(Ymn2-Yinput)); //measure the distance // } //printf("alfa is %f, dp1 is %f,dp2 is %f. The location of mobile node is (%f,%f). The distance between mobile node (%f,%f) and (%f,%f) is %f \n",alfa,dp1,dp2,Xmn,Ymn, Xinput, Yinput, Xmn, Ymn, Distance); //fprintf(f,"The location of mobile node is , and the nearest distance is \n"); //fclose(f); //return (i=0); } else printf("Please wait ...... \n"); } //end if else printf("Please wait ...... \n"); //} //end for loop } //end infinate for loop //} }
int main(int argc, char *argv[]) { //add server int sockfd, new_fd; // listen on sock_fd, new connection on new_fd struct sockaddr_storage their_addr; // connector's address information socklen_t sin_size; struct sigaction sa; int yes=1; char l[INET6_ADDRSTRLEN]; int rv; //until here int sock, rtsock, i,j, sd, count, params[2], fd,index_sp,mimo_flag=0,mimo_flag1=0,mimo_flag2=0, average[10]; int addrLen,k,sum=0,qsum=0,sum1=0,qsum1=0,sum2=0,qsum2=0, number=0, add=0; int a_macadd1, a_macadd2, a_macadd3, a_macadd4, a_macadd5,a_macadd6; FILE *f; FILE *ff; FILE *fff; double alfa; float a,b,c,Xmn,Ymn,Ymn1,Ymn2,X1,X2,X3,X4,X5,Y1,Y2,Y3,Y4,Y5,Distance,dp1,dp2,tambah, averaged=0.0; float Distance1,mobile_A,mobile_B,Distance15,Distance2,Distance23,Distance24,Distance25; float Distance3,Distance31,Distance32,Distance34,Distance35,Distance41,Distance42,Distance43,Distance45; float Distance51,Distance52,Distance53,Distance54,Distance4,Distance5,Distance6,Distance7,Distance8,Distance9; struct sockaddr_in addr; struct hostent *hp; struct in_addr *ptr; char nearestmobile, mobile1, mobile2, mobile3, mobile4, mobile5; char *s, s1[1024]; if((f=fopen("/root/ssf.c","r+"))==NULL) { printf("entah\n");exit(-1); } if(argc < 2) { fprintf(stderr, "usage: Rtrg_gps [ru|rr|tu|tr] [<address>]\n"); exit(-1); } s = (argc < 3)? "" : argv[2]; if(strcmp(argv[1], "ru") == 0) { if((sock = BindSocket(CreateUDPSocket(), s)) < 0) { exit(-1); } addrLen = sizeof(addr); if(getsockname(sock, &addr, &addrLen) < 0 ) { close(sock); return -1; } printf("Rtrg_gps will receive request at this address %s\n", EncodeIPAddress(s1, &addr)); for(;;){ recv(sock,&ind.index_sp, sizeof(ind.index_sp), 0); //printf("I receive ur index \n"); for(i=0; i< ind.index_sp;i++) { recv(sock, &p[i], sizeof(p), 0); if(strcmp(p[i].essid,"mimos")==0) { a=p[i].dis;tambah=p[i].sl;mimo_flag++; fprintf(f,"%s ESSID: %s Signal level: %d Distance: %f Got from : %s\n",p[i].mac,p[i].essid,p[i].sl,a,ind.mac); } else if(strcmp(p[i].essid,"mimos1")==0) {b=p[i].dis;mimo_flag1++;fprintf(f,"%s ESSID: %s \nSignal level: %d \n Distance: %f Got from : %s\n",p[i].mac,p[i].essid,p[i].sl,b,ind.mac);} else if(strcmp(p[i].essid,"mimos2")==0) {c=p[i].dis;mimo_flag2++;fprintf(f,"%s ESSID: %s Signal level: %d Distance: %f Got from : %s\n",p[i].mac,p[i].essid,p[i].sl,c,ind.mac);} printf("%s ESSID: %s Signal level: %d Distance: %f Got from : %s\n",p[i].mac,p[i].essid,p[i].sl,p[i].dis,ind.mac); //add server inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); printf("server: %s\n", s); //until here // calculate mobile node location if(mimo_flag > 0 && mimo_flag1>0 && mimo_flag2 >0 ){ alfa=acos((a*a+25-b*b)/(10*a)); if( isnan(alfa)==0){ Xmn=a*cos(alfa);Ymn1=a*sin(alfa);Ymn2=-Ymn1; dp1=sqrt(Xmn*Xmn + (5-Ymn1)*(5-Ymn1)); dp2=sqrt(Xmn*Xmn + (5-Ymn2)*(5-Ymn2)); if (fabsf(dp1-c) < fabsf(dp2-c)) Ymn=Ymn1; else Ymn=-Ymn2; if ( Xmn < 0 ) Xmn = -Xmn; else Xmn = Xmn; printf("alfa is %f, dp1 is %f,dp2 is %f. The location of mobile node is (%f,%f) \n",alfa,dp1,dp2,Xmn,Ymn); fprintf(f,"The location of mobile node is (%f,%f)\n",Xmn,Ymn); Distance1=sqrt(((Xmn-3.5)*(Xmn-3.5))+((Ymn-1.2)*(Ymn-1.2))); //mobile with kyle Distance2=sqrt(((Xmn-2.8)*(Xmn-2.8))+((Ymn-8.7)*(Ymn-8.7))); //mobile node with sally Distance3=sqrt(((3.5-2.8)*(3.5-2.8))+((1.2-8.7)*(1.2-8.7))); //kyle with sally Distance4=sqrt(((3.5-0)*(3.5-0))+((1.2-0)*(1.2-0))); //kyle with mimos Distance5=sqrt(((2.8-0)*(2.8-0))+((8.7-0)*(8.7-0))); //sally with mimos Distance6=sqrt(((2.8-10)*(2.8-10))+((8.7-0)*(8.7-0))); //sally with mimos2 Distance7=sqrt(((3.5-10)*(3.5-10))+((1.2-0)*(1.2-0))); //kyle with mimos2 Distance8=sqrt(((3.5-0)*(3.5-0))+((1.2-10)*(1.2-10))); //kyle with mimos1 Distance9=sqrt(((2.8-0)*(2.8-0))+((8.7-10)*(8.7-10))); //sally with mimos1 printf("Distance for mobile node with Kyle is %f \n",Distance1); fprintf(f,"Distance for mobile node with Kyle is %f \n",Distance1); printf("Distance for mobile node with Sally is %f \n",Distance2); fprintf(f,"Distance for mobile node with Sally is %f \n",Distance2); printf("Distance for Klye with Sally is %f \n",Distance3); fprintf(f,"Distance for Klye with Sally is %f \n",Distance3); //nearest distance { if (Distance1>Distance2){ ff=fopen("answer.c","w"); fff=fopen("data.c","r+"); nearestmobile = mobile_B; printf ("The nearest mobile is Sally with distance %f\n ", Distance2); fprintf (f,"The nearest mobile is Sally with distance %f\n ", Distance2); fprintf(ff,"*****@*****.**"); fprintf(fff,"-----------------------------------------------------------------------------------\n"); fprintf(fff,"SIP URI LOCATION\n"); fprintf(fff,"%s (%f,%f) \n",ind.mac,Xmn,Ymn); fprintf(fff,"[email protected] (2.8,8.7) \n"); fprintf(fff,"[email protected] (3.5,1.2) \n"); fprintf(fff,"-----------------------------------------------------------------------------------\n"); fprintf(fff,"\n"); fprintf(fff,"\n"); fprintf(fff,"******************************************************************************************************************************\n"); fprintf(fff," %s [email protected] [email protected]\n",ind.mac); fprintf(fff,"%s X %f %f \n",ind.mac,Distance2,Distance1); fprintf(fff,"Sally %f X %f \n",Distance2,Distance3); fprintf(fff,"Kyle %f %f %f \n",Distance1,Distance3); fprintf(fff,"mimos %f %f %f \n",a,Distance4,Distance5); fprintf(fff,"mimos1 %f %f %f \n",b,Distance9,Distance8); fprintf(fff,"mimos2 %f %f %f \n",c,Distance6,Distance7); fprintf(fff,"******************************************************************************************************************************\n"); fclose(ff); fclose(fff); } else { ff=fopen("answer.c","w"); fff=fopen("data.c","r+"); nearestmobile = mobile_A; printf ("The nearest mobile is Kyle with distance %f\n ", Distance1); fprintf (f,"The nearest mobile is Kyle with distance %f\n ", Distance1); fprintf(ff,"*****@*****.**"); fprintf(fff,"-----------------------------------------------------------------------------------\n"); fprintf(fff,"SIP URI LOCATION\n"); fprintf(fff,"%s (%f,%f) \n",ind.mac,Xmn,Ymn); fprintf(fff,"[email protected] (2.8,8.7) \n"); fprintf(fff,"[email protected] (3.5,1.2) \n"); fprintf(fff,"-----------------------------------------------------------------------------------\n"); fprintf(fff,"\n"); fprintf(fff,"\n"); fprintf(fff,"******************************************************************************************************************************\n"); fprintf(fff," %s [email protected] [email protected]\n",ind.mac); fprintf(fff,"%s X %f %f \n",ind.mac,Distance2,Distance1); fprintf(fff,"Sally %f X %f \n",Distance2,Distance3); fprintf(fff,"Kyle %f %f X \n",Distance1,Distance3); fprintf(fff,"mimos %f %f %f \n",a,Distance4,Distance5); fprintf(fff,"mimos1 %f %f %f \n",b,Distance9,Distance8); fprintf(fff,"mimos2 %f %f %f \n",c,Distance6,Distance7); fprintf(fff,"******************************************************************************************************************************\n"); fclose(ff); fclose(fff); } } } else printf("Please wait ...... \n"); } //end if else printf("Please wait ...... \n"); } //end for loop //fclose(f); } //end infinate for loop } }
AnsiString InitUDP2(CConceptClient *owner, char *host) { AnsiString result; if (owner->RTSOCKET) { closesocket(owner->RTSOCKET); owner->RTSOCKET = INVALID_SOCKET; } int s_len = strlen(host); if ((!owner) || (!host) || (!s_len)) return result; char *stun_server = host; char *p_str = strchr(host, ','); if ((!p_str) || (p_str[1] == '-')) return result; p_str[0] = 0; p_str++; char *local_host = strchr(p_str, ','); if ((!local_host) || (local_host[1] == '-')) return result; local_host[0] = 0; local_host++; char *local_port = strchr(local_host, ','); if ((!local_port) || (local_port[1] == '-')) return result; local_port[0] = 0; local_port++; AnsiString lport(local_port); struct addrinfo hints; struct addrinfo *a_res = 0; memset(&serveraddr, 0, sizeof(serveraddr)); memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; int gerr = getaddrinfo(local_host, local_port, &hints, &a_res); if ((gerr != 0) || (!a_res)) { AnsiString err((char *)"Error in getaddrinfo(): "); err += host; err += (char *)":"; err += p_str; err += (char *)" "; err += (char *)gai_strerror(gerr); fprintf(stderr, "Dropping RT socket: %s\n", err.c_str()); result = ""; return result; } if ((a_res->ai_family != AF_INET) && (a_res->ai_family != AF_INET6)) { freeaddrinfo(a_res); fprintf(stderr, "Invalid socket family\n"); return result; } memcpy(&serveraddr, a_res->ai_addr, a_res->ai_addrlen); server_len = a_res->ai_addrlen; unsigned short connect_port = 0; int socket = CreateUDPSocket(lport.ToInt(), a_res->ai_family == AF_INET6, connect_port); freeaddrinfo(a_res); if (socket < 0) return result; char connect_ip[0xFF]; connect_ip[0] = 0; if (stun_server[0] == '*') { strcpy(connect_ip, "self"); } else { int res = STUN(socket, stun_server, p_str, connect_ip, &connect_port); if (!res) { closesocket(socket); return result; } } owner->RTSOCKET = socket; result = connect_ip; result += (char *)","; result += AnsiString((long)connect_port); return result; }