Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The port number is passed as an argument */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- //#include <netinet/in.h>
- #include <arpa/inet.h>
- #include <iostream>
- #include <sys/wait.h>
- #include <signal.h>
- #include <sstream>
- #include <fstream>
- #define RCVBUFSIZE 50 /* Size of receive buffer */
- using namespace std;
- void error(const char *msg)
- {
- perror(msg);
- exit(1);
- }
- void get(int clntSocket, string fileName) {
- /*sending protocol of files
- * check if the file is found and send response
- * if found send the file as arrays
- */
- FILE *file = fopen(fileName.c_str(), "rb");
- if (file == NULL) {//file not found
- string s = "HTTP/1.0 404 Not Found\r\n";
- char buffer[s.size() + 1];
- strncpy(buffer, s.c_str(), sizeof(buffer));
- buffer[sizeof(buffer) - 1] = 0;
- send(clntSocket, buffer, sizeof(buffer), 0);
- return;
- }
- //file found
- string s = "HTTP/1.0 200 OK\r\n";
- char buffer[1024];
- strncpy(buffer, s.c_str(), sizeof(buffer));
- buffer[sizeof(buffer)-1] = 0;
- if (send(clntSocket, buffer, sizeof(buffer), 0) != sizeof(buffer))
- error("send() failed");
- int read_size;
- char send_buffer[1024];
- while (!feof(file)) {
- read_size = fread(send_buffer, 1, 1024 , file);
- cout<<read_size<<endl;
- if (send(clntSocket, send_buffer, read_size, 0) != read_size)
- error("send() failed");
- }
- fclose(file);
- }
- void post(int clntSocket, string fileName) {
- //create file with fileName
- FILE* file=fopen(fileName.c_str(), "wb");
- int recv_size;
- char buffer[1024];
- if ((recv_size = recv(clntSocket, buffer, sizeof(buffer), 0)) < 0)
- error("recv() failed");
- while (recv_size > 0) {
- fwrite(buffer, 1, recv_size, file);
- if ((recv_size = recv(clntSocket, buffer, sizeof(buffer), 0)) < 0)
- error("recv() failed");
- }
- fclose(file);
- }
- void HandleTCPClient(int clntSocket) {
- char echoBuffer[RCVBUFSIZE];
- int recvMsgSize;
- /* Buffer for echo string */
- /* Size of received message */
- /* Receive message from client */
- if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
- error("recv() failed");
- cout<<echoBuffer<<endl;
- //split the message and find the method and the fileName
- istringstream iss(echoBuffer);
- string type, fileName;
- iss >> type;
- iss >> fileName;
- cout << "type: " << type << endl;
- cout << "filename: " << fileName << endl;
- if (type == "GET" || type == "get")
- get(clntSocket, fileName);
- else if(type == "POST" || type == "post")
- post(clntSocket, fileName);
- else
- error("incorrect type");
- //close(clntSocket); /* Close client socket */
- }
- void sigchld_handler(int s)
- {
- while(wait(NULL) > 0);
- }
- int main(int argc, char *argv[])
- {
- int sockfd, newsockfd, portno;
- socklen_t clilen;
- char buffer[256];
- struct sockaddr_in serv_addr, cli_addr;
- int n;
- struct sigaction sa;
- /*if (argc < 2) {
- fprintf(stderr,"ERROR, no port provided\n");
- exit(1);
- }*/
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd < 0)
- error("ERROR opening socket");
- bzero((char *) &serv_addr, sizeof(serv_addr));
- //portno = atoi(argv[1]);
- portno = 5000;
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //*/*//
- serv_addr.sin_port = htons(portno);
- if (bind(sockfd, (struct sockaddr *) &serv_addr,
- sizeof(serv_addr)) < 0)
- error("ERROR on binding");
- if (listen(sockfd,5) < 0)
- error("ERROR on listening");
- sa.sa_handler = sigchld_handler; // reap all dead processes
- sigemptyset(&sa.sa_mask);
- sa.sa_flags = SA_RESTART;
- if (sigaction(SIGCHLD, &sa, NULL) == -1)
- {
- perror("sigaction");
- exit(1);
- }
- while(1) {
- // The accept() call actually accepts an incoming connection
- clilen = sizeof(cli_addr);
- cout << "before accept"<<endl;
- if ((newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen)) < 0)
- {
- continue;
- //error("ERROR on accept");
- }
- cout << "connections was found"<< endl;
- if (!fork())
- { // this is the child process
- close(sockfd); // child doesn.t need the listener
- HandleTCPClient(newsockfd);
- close(newsockfd);
- exit(0);
- }
- close(newsockfd); // parent doesn.t need this
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement