int GSMInterface::sendPacket(String server, int port, String packet) { if(!openTCPSocket(server, port)) return 500; int packetSize = packet.length()-1; serialinterface->printToGSM("AT+QISEND=" + String(packetSize)); serialinterface->printToGSM('\r'); delay(100); serialinterface->printToGSM(packet); if(serialinterface->pollForResponseFromCommand("", "OK", false)) { delay(100); //closeTCPSocket(); return 200; } else { closeTCPSocket(); return 501; } }
int main(int argc, char *argv[]) { if (argc != 4) { fprintf(stderr,"Usage: ./router localhost TCP UDP\n"); exit(1); } int tcpsockfd,udplistener, numbytes; int i; struct LINK newLink; char buf[MAXDATASIZE]; char* pBuf = NULL; char sendbuf[MAXDATASIZE]; char tempbuf[MAXDATASIZE]; struct timeval tval; // time out for select() fd_set readfds; // read fds for select() int ret; FILE *socket_stream_in; struct NetworkTopoStruct* networkTopo = initNetworkTopo(); pGlobalTopo = networkTopo; /* initialize random seed: */ srand ( time(NULL) ); // open tcp tcpsockfd = openTCPSocket(argv[1],argv[2]); socket_stream_in = fdopen(tcpsockfd, "r"); // open the udp listener udplistener = openUDPListenningSocket(argv[3]); // get my address sendmessage(tcpsockfd,"HELO\n"); numbytes = recv(tcpsockfd, buf, MAXDATASIZE-1, 0); buf[numbytes] = '\0'; pBuf = buf+5; printf("manager replied with address %s",pBuf); storesMyAddress(buf,networkTopo); // Stores my address into the database. // register the host with udpport sprintf(sendbuf,"HOST localhost %s\n",argv[3]); sendmessage(tcpsockfd,sendbuf); receive(tcpsockfd,buf); // this is OK // retrive the neighbour links sendmessage(tcpsockfd,"NEIGH?\n"); do{ if ( fgets(buf,MAXDATASIZE-1,socket_stream_in)!=NULL ){ printf("%s",buf); if (isControlInfo(buf,"NEIGH")){ storesNeighFromControlInfo(buf,networkTopo); // stores the neigh information } if (isControlInfo(buf,"DONE")) break; } }while(1); sendmessage(tcpsockfd,"READY\n"); receive(tcpsockfd,buf); // This is OK // Until here, the handshake is done // LOG Information sendmessage(tcpsockfd,"LOG ON\n"); receive(tcpsockfd,buf); // This is LOG ON replied from the server // initialize select() to do multiplexing I/O tval.tv_sec = 0; tval.tv_usec = 80000; FD_ZERO(&readfds); FD_SET(tcpsockfd,&readfds); // input from the tcp FD_SET(udplistener,&readfds); // input from the udp //Broadcast the neighbour links to all nodes to set up the topology of the network for(i=0;i<networkTopo->linkNum;i++) { broadCastLinkInfo(3,networkTopo->Links[i],networkTopo); } // the loop for the select(), and dealing with the input from keyboard and the input from socket. while( (ret=select(max(tcpsockfd,udplistener)+1, &readfds, NULL, NULL, NULL)) >=0 ) { if (FD_ISSET(tcpsockfd,&readfds)) { if (receiveOneLine(socket_stream_in,buf)){ // update and acknowledge link cost if (isControlInfo(buf,"LINKCOST")){ // fetch the cost fetchToken(buf,3,tempbuf); // the cost is in tempbuf sprintf(sendbuf,"COST %s OK\n",tempbuf); sendmessage(tcpsockfd,sendbuf); newLink = storesLinkCostChangeFromControlInfo(buf,networkTopo); // save the link cost change in the data structure and broadcast the change broadCastLinkInfo(4,newLink,networkTopo); } //break if received end if (isControlInfo(buf,"END")){ sleep((rand()%200+100)/300); sendmessage(tcpsockfd,"BYE\n"); break; } } } if (FD_ISSET(udplistener,&readfds)) { if((numbytes = receiveUDPMessage(udplistener,buf)) >0){ dealWithUDPMessage(buf,numbytes, networkTopo,tcpsockfd); // forward message or deal with control information between routers } } FD_ZERO(&readfds); FD_SET(tcpsockfd,&readfds); // got message from the tcp FD_SET(udplistener,&readfds); // got message from the udp server input } if (ret==-1){ perror("select()"); exit(1); } freeTopo(networkTopo); // release the networkTopo close(udplistener); close(tcpsockfd); return 0; }