/* What follows is code that ran before I added somecomments and should run * after....This is student code and I am posting it because my code is * somewhere in the disk cloud of my stuff. I will more fully comment later. */ /*important note: run client on wilkes with ./client 39394 134.173.42.100 stringtopass, and run server on knuth sa ./server 39394*/ /*Thus one more parameter, and not exactly the CS125 Class Project */ /*Majority of socket code was adapted from code a group member wrote in summer research with Prof. Breeden, which also involved some socket tests*/ /* These are the libraries that numerous 'man' commands will show as necessary * for Sockets */ /*this is client*/ #include /*#include This is the stupid library from cs105 and shown in class example */ #include #include #include #include #include #include /* main function */ int main (int argc, char *argv[]) { /* to me, add dnsaccess as a function, and pass in the input parameter */ system("gcc -o dnsaccess dnsaccess.c"); //compile dnsaccess char namebuffer[1000]; //create buffer for running dnsaccess snprintf(namebuffer, sizeof(namebuffer), "./dnsaccess %s > file.txt", argv[2]); //print to namebuffer dnsaccess run command system(namebuffer); //run line in namebuffer char filebuffer[100]; char* filename = "file.txt"; FILE *file; //create file pointer file = fopen(filename, "r"); //open file.txt /*read file to find IPv4 address */ while(fgets(filebuffer, 100, file)){ int dots = 0; int j = 0; for(j=0;j<100; j++){ if(filebuffer[j] == 46){ dots++; } } if(dots == 3){ //printf("Found IPv4!\n"); //printf("%s\n", filebuffer); break; } } fclose(file); //close file /* need a place for the port id from the input string */ int port; //port number to send to port = atoi(argv[1]); //get port from input /* The following code reflects the first slides of the Socket lecture * You need to get a socket and set it up with appropriate parameters * A Socket is a file descriptor describing how the interface is to work */ int socketLoc = socket(AF_INET, SOCK_STREAM, 0); //initialize socket struct sockaddr_in address; //Socket address address.sin_family = AF_INET; //Using IPv4 address.sin_port = htons(port); //link port to socket address address.sin_addr.s_addr = inet_addr(filebuffer);//argv[2]); //getting IP address from input /* got the Socket set up. Now try to connect. This code reflects code in * the Client Example Code of the Socket Lecture */ int err = connect(socketLoc, (struct sockaddr*)&address, sizeof(address)); //Establish connection if(err < 0){ printf("Error with connect\n"); exit(EXIT_FAILURE); } /* The conection is up, So now send a message */ err = send(socketLoc, argv[3], strlen(argv[3]), 0); //send message if(err < 0){ printf("Error with send\n"); exit(EXIT_FAILURE); } printf("client: sent %lu bytes, \"%s\"\n", strlen(argv[3]), argv[3]); //print bytes sent /* The print about just says how many bytes were sent */ /* Now get ready to receive the return message from the server */ char buffer[10000000] = {0}; /*initialize buffer - Mike cannot hold key this long */ err = recv(socketLoc, buffer, 10000000, 0); //receive message from socket if(err < 0){ printf("Error with recv\n"); exit(EXIT_FAILURE); } /*find actual number of characters read */ long unsigned int i; for(i=0;i<10000000;i++){ if(buffer[i] == 0){ break; } } /* print bytes received*/ printf("client: received %lu bytes, \"%s\"\n", i, buffer); exit(0); } /*important note: run client on wilkes with ./client 39394 134.173.42.100 stringtopass, and run server on knuth sa ./server 39394*/ /*Majority of socket code was adapted from code a group member wrote in summer research with Prof. Breeden, which also involved some socket tests*/