int main(void) { char *hostname = "www.mtu.edu"; char *port = "80"; // can also be a name in /etc/services // getaddrinfo(): network address and service translation: struct addrinfo *socketaddrinfo = example_getaddrinfo(hostname, port); // socket(): create an endpoint for communication int sock = example_socket(socketaddrinfo); // connect(): initiate a connection on a socket example_connect(sock, socketaddrinfo); freeaddrinfo(socketaddrinfo); // done with socketaddrinfo // Send an HTTP request: char httpget[1024]; snprintf(httpget, 1024, "GET / HTTP/1.1\r\n" "Host: %s\r\n" "Connection: close\r\n\r\n", hostname); ssize_t s = send(sock, httpget, strlen(httpget), 0); if(s == -1) { perror("send"); exit(EXIT_FAILURE); } else if (s < (ssize_t)strlen(httpget)) { // If you want to ensure that the entire message gets sent, // you must loop and send the part of the message that didn't // get sent the first time. printf("Entire message did not get sent."); } while(1) { char recvbuf[1024]; ssize_t recvdBytes = 0; /* Read up to 1024 bytes from the socket. Even if we know that there are far more than 1024 bytes sent to us on this socket, recv() will only give us the bytes that the OS can provide us. If there are no bytes for us to read (and the connection is open and without error), recv() if there are no bytes to read. */ recvdBytes = recv(sock, recvbuf, 1024, 0); if(recvdBytes > 0) // print bytes we received to console fwrite(recvbuf, 1, recvdBytes, stdout); else if(recvdBytes == 0) // server closed connection { fflush(stdout); close(sock); exit(EXIT_SUCCESS); } else { // error receiving bytes perror("recv:"); close(sock); exit(EXIT_FAILURE); } } }
int main(void) { // We can't listen on a port lower than 1024 unless we are root. char *port = "8080"; // can also be a name in /etc/services. // getaddrinfo(): network address and service translation: struct addrinfo *socketaddrinfo = example_getaddrinfo(NULL, port); // socket(): create an endpoint for communication int sock = example_socket(socketaddrinfo); example_setsockopt(sock); example_bind(sock, socketaddrinfo); example_listen(sock, 128); freeaddrinfo(socketaddrinfo); // done with socketaddrinfo printf("Press Ctrl+C to exit server.\n"); printf("Point your web browser to: http://127.0.0.1:%s\n", port); while(1) { // Get a socket for a particular incoming connection. int newsock = accept(sock, NULL, NULL); if(newsock == -1) { perror("accept(): "); continue; // try again if accept failed. } char http_body[] = "hello world"; char http_headers[1024]; snprintf(http_headers, 1024, "HTTP/1.0 200 OK\r\n" "Content-Length: %zu\r\n" "Content-Type: text/html\r\n\r\n", strlen(http_body)); // should check return value /* Note: send() does not guarantee that it will send all of * the data that we ask it too. To reliably send data, we * should inspect the return value from send() and then call * send() again on any bytes did not get sent. */ // Send the HTTP headers if(send(newsock, http_headers, strlen(http_headers), 0) == -1) { perror("send"); close(newsock); exit(EXIT_FAILURE); } // Send the HTTP body if(send(newsock, http_body, strlen(http_body), 0) == -1) { perror("send"); close(newsock); exit(EXIT_FAILURE); } /* shutdown() (see "man 2 shutdown") can be used before close(). It allows you to partially close a socket (i.e., indicate that we are done sending data but still could receive). This could allow us to signal to a peer that we are done sending data while still allowing us to receive data. SHUT_RD will disallow reads from the socket, SHUT_WR will disallow writes, and SHUT_RDWR will disallow read and write. Also, unlike close(), shutdown() will affect all processes that are sharing the socket---while close() only affects the process that calls it. */ #if 0 if(shutdown(newsock, SHUT_RDWR) == -1) { perror("shutdown"); close(newsock); exit(EXIT_FAILURE); } #endif if(close(newsock) == -1) { perror("close"); exit(EXIT_FAILURE); } } }
int main(int argc, char* argv[]) { // args should only be the port number and the name // of the program running if (argc != 2) { printf("Usage Error - provide port number\n"); exit(1); } char *port = argv[1]; // create the ./files dir createSubdir(); // getaddrinfo(): network address and service translation: struct addrinfo *socketaddrinfo = example_getaddrinfo(NULL, port); // socket(): create an endpoint for communication int sock = example_socket(socketaddrinfo); example_setsockopt(sock); example_bind(sock, socketaddrinfo); example_listen(sock, 128); freeaddrinfo(socketaddrinfo); // done with socketaddrinfo //printf("Press Ctrl+C to exit server.\n"); //printf("Run a web browser on the same computer as you run the server and point it to:\n"); //printf("http://127.0.0.1:%s\n", port); while(1) { printf("Waiting for connections...\n"); // Get a socket for a particular incoming connection. int newsock = accept(sock, NULL, NULL); if(newsock == -1) { perror("accept(): "); continue; // try again if accept failed. } printf("Client connected\n"); // Check the password of the client if (checkPass(newsock) == -1) { printf("Incorrect password - Connection aborted\n"); closeSock(newsock); continue; } else { printf("Password accepted - Continuing connection\n"); } sendBytes(newsock, "You connected!\n", strlen("You connected!\n")); char cmd[1024]; memset(cmd, 0, 1024); recvBytes(newsock, cmd, 1024); printf("Received command: %s\n", cmd); if (strcmp(cmd, "list") == 0) { sendBytes(newsock, "File listing:\n", strlen("File listing:\n")); // Scan the dirextory and send the file list sendList(newsock); continue; } if (strcmp(cmd, "get") == 0) { char retBuf[1024]; memset(retBuf, 0, 1024); // Read the name of the file recvBytes(newsock, retBuf, 1024); // Check if the file exists and send it getFile(newsock, retBuf); continue; } if (strcmp(cmd, "put") == 0) { char retBuf[1024]; memset(retBuf, 0, 1024); // Read the name of the file getLine(newsock, retBuf); // Check if the file exists and send it putFile(newsock, retBuf); continue; } // char http_body[] = "hello world"; // char http_headers[1024]; // snprintf(http_headers, 1024, // "HTTP/1.0 200 OK\r\n" // "Content-Length: %zu\r\n" // "Content-Type: text/html\r\n\r\n", // strlen(http_body)); // should check return value /* Note: send() does not guarantee that it will send all of * the data that we ask it too. To reliably send data, we * should inspect the return value from send() and then call * send() again on any bytes did not get sent. */ // Send the HTTP headers //if(send(newsock, http_headers, strlen(http_headers), 0) == -1) //{ // perror("send"); // close(newsock); // exit(EXIT_FAILURE); //} //// Send the HTTP body //if(send(newsock, http_body, strlen(http_body), 0) == -1) //{ // perror("send"); // close(newsock); // exit(EXIT_FAILURE); //} /* shutdown() (see "man 2 shutdown") can be used before close(). It allows you to partially close a socket (i.e., indicate that we are done sending data but still could receive). This could allow us to signal to a peer that we are done sending data while still allowing us to receive data. SHUT_RD will disallow reads from the socket, SHUT_WR will disallow writes, and SHUT_RDWR will disallow read and write. Also, unlike close(), shutdown() will affect all processes that are sharing the socket---while close() only affects the process that calls it. */ #if 0 if(shutdown(newsock, SHUT_RDWR) == -1) { perror("shutdown"); close(newsock); exit(EXIT_FAILURE); } #endif closeSock(newsock); } }