// Author: A.Visser@uva..nl & E.H.Steffens@uva.nl // Copyright: Universiteit van Amsterdam, September 2010. // The Lantronix webserver DSTni is visible as peer-to-peer connection SRV1. // It gives ip-adresses in range 169.254.4.*, and reserves for himself 169.254.0.10. // Typical usage is ConnectLantronix.exe 169.254.0.10 30704 // // Modifications Arnoud: // * added missing include-files to remove warnings // * added some comments // * default options for commandline arguments // * CSAPP style connection #include #include #include #include #include #include #include #include #include "csapp.h" //#define MP_COMMAND_LENGTH 9 //#define MP_REPLY_LENGTH 5 // http://www.lantronix.com/pdf/MatchPort_UG.pdf, p. 72 #define GET_CURRENT_STATE 0x13 void error(char *msg) { perror(msg); exit(0); } int main(int argc, char **argv) { int clientfd, port; char *host, buf[MAXLINE]; //rio_t rio; char MP_command[9]; char MP_reply[5]; bzero(MP_command, 9); MP_command[0] = 0x13; switch(argc) { case 3: port = atoi(argv[2]); host = argv[1]; break; case 1: port = 30704; host = "169.254.0.10"; break; case 2: port = 30704; host = argv[1]; break; default: fprintf(stderr,"usage %s hostname port\n", argv[0]); fprintf(stdout,"default usage %s 169.254.0.10 30704\n", argv[0]); fprintf(stdout,"\nMake sure that your notebook is connected peer-to-peer to SRV1\n"); exit(0); } fprintf(stdout,"open connection to %s:%d ....\n", host, port); clientfd = Open_clientfd(host, port); //fprintf(stdout,"initiating buffer of stream %d ....\n", clientfd); //Rio_readinitb(&rio, clientfd); while (Fgets(buf, MAXLINE, stdin) != NULL) { // buf not used, only used for synchronisation fprintf(stdout,"writing command\n"); Rio_writen(clientfd, MP_command,9); fprintf(stdout,"reading response\n"); Rio_readn(clientfd, MP_reply,5); fprintf(stdout, "%x\n",MP_reply[0]); fprintf(stdout, "%x\n",MP_reply[1]); } Close(clientfd); exit(0); } int old_main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; char MP_command[9]; char MP_reply[5]; bzero(MP_command, 9); MP_command[0] = 0x13; switch(argc) { case 3: portno = atoi(argv[2]); server = gethostbyname(argv[1]); break; case 1: portno = 30704; server = gethostbyname("169.254.0.10"); break; case 2: portno = 30704; server = gethostbyname(argv[1]); if (server!=0) { break; //only break when successful, otherwise print usage } default: fprintf(stderr,"usage %s hostname port\n", argv[0]); fprintf(stdout,"default usage %s 169.254.0.10 30704\n", argv[0]); fprintf(stdout,"\nMake sure that your notebook is connected peer-to-peer to SRV1\n"); exit(0); } if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); // fprintf (stderr, "Debug point a\n"); if (connect(sockfd,(const struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); // fprintf (stderr, "Debug point b\n"); //fprintf(stderr, "Please enter the message: "); bzero(buffer,256); sprintf(buffer, "%c", GET_CURRENT_STATE), fprintf (stderr, "Debug point c\n"); // fgets(buffer,255,stdin); // n = write(sockfd,buffer,strlen(buffer)); n = write(sockfd,MP_command,9); if (n < 0) error("ERROR writing to socket"); bzero(buffer,256); // fprintf (stderr, "Debug point d\n"); // n = read(sockfd,buffer,255); n = read(sockfd,MP_reply,9); if (n < 0) error("ERROR reading from socket"); fprintf (stderr, "Debug point e\n"); fprintf(stderr, "%x\n",MP_reply[0]); fprintf(stderr, "%x\n",MP_reply[1]); return 0; }