/* What follows is code that ran before I added some comments 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 the server*/ #include #include #include #include #include #include int main(int argc, char *argv[]){ /* need a port variable, socket address, various Socket parms */ int port; //port to listen to struct sockaddr_in address; //socket address port = atoi(argv[1]); //get port from input address.sin_family = AF_INET; //use IPv4 address.sin_port = htons(port); //link port to socket address /* go get and open a socket, set it up appropriately */ int socketLoc = socket(AF_INET, SOCK_STREAM, 0); //initialize socket int opt = 1; /* this was done in lecture code */ int err = setsockopt(socketLoc, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)); //eliminates address already in use if(err < 0){ printf("Error with sockopt\n"); exit(EXIT_FAILURE); } /* bind the socket, look at class FAQs on Sockets */ err = bind(socketLoc, (struct sockaddr*)&address, sizeof(address)); //bind if(err < 0){ printf("Error with bind\n"); exit(EXIT_FAILURE); } /* Acquired, initialized and bound a Socket, how make it listen for a * servide request * Then Accept that request */ listen(socketLoc, 3); //listen int addrlen = sizeof(address); int new_socket = accept(socketLoc, (struct sockaddr*)&address, (socklen_t*)&addrlen); //accept incoming connection char buffer[10000000] = {0}; err = read(new_socket, buffer, 10000000); //read from buffer if(err < 0){ printf("Error with read\n"); exit(EXIT_FAILURE); } /*find actual numbe of characters read*/ long unsigned int i; for(i=0;i<10000000;i++){ if(buffer[i] == 0){ break; } } /*send message*/ err = send(new_socket, buffer, sizeof(buffer),0); if(err < 0){ printf("Error with send\n"); exit(EXIT_FAILURE); } /*print bytes received*/ printf("Server: received and echoed %lu bytes from 134.173.42.167\n", i); return 0; }