Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /* for printf() and fprintf() */
- #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
- #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
- #include <stdlib.h> /* for atoi() */
- #include <string.h> /* for memset() */
- #include <unistd.h> /* for close() */
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <vector>
- using namespace std;
- size_t split(const std::string &txt, std::vector<std::string> &strs, char ch)
- {
- size_t pos = txt.find( ch );
- size_t initialPos = 0;
- strs.clear();
- // Decompose statement
- while( pos != std::string::npos ) {
- strs.push_back( txt.substr( initialPos, pos - initialPos ) );
- initialPos = pos + 1;
- pos = txt.find( ch, initialPos );
- }
- // Add the last one
- strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1 ) );
- return strs.size();
- }
- void error(const char *msg)
- {
- perror(msg);
- exit(1);
- }
- #define RCVBUFSIZE 100
- void get(int sock, string fileName) {
- FILE* file = fopen(fileName.c_str(), "wb");
- int recv_size;
- char res[1024];
- if ((recv_size = recv(sock, res, sizeof(res), 0)) < 0)
- {
- error("recv() failed");
- }
- cout << "response is: " << res << endl;
- cout << recv_size << endl;
- cout << " " << endl;
- char buffer[1024];
- if ((recv_size = recv(sock, buffer, sizeof(buffer), 0)) < 0)
- {
- error("recv() failed");
- }
- while (recv_size > 0) {
- cout << recv_size << endl;
- fwrite(buffer, 1, recv_size, file);
- if ((recv_size = recv(sock, buffer, sizeof(buffer), 0)) < 0)
- {
- error("recv() failed");
- }
- }
- fclose(file);
- }
- void post(int sock, string fileName) {
- FILE *file = fopen(fileName.c_str(), "rb");
- if (file == NULL) {//file not found
- error("file not found");
- }
- int recv_size;
- char res[1024];
- if ((recv_size = recv(sock, res, sizeof(res), 0)) < 0)
- {
- error("recv() failed");
- }
- string str(res);
- cout <<str << endl;
- int read_size;
- char send_buffer[1024];
- while (!feof(file)) {
- read_size = fread(send_buffer, 1, 1024, file);
- if (send(sock, send_buffer, read_size, 0) != read_size)
- {
- error("send() failed");
- }
- }
- fclose(file);
- }
- void input_file_reader(int sock, string fileName) {
- FILE *file = fopen(fileName.c_str(), "rb");
- if (file == NULL) {//file not found
- error("file not found");
- }
- int read_size;
- char send_buffer[1024];
- ifstream in(fileName);
- std::string str;
- while (std::getline(in, str) ) {
- vector<string> v;
- split(str, v, ' ');
- int send_buffer_len = str.length();
- if (send(sock, send_buffer, send_buffer_len, 0) != send_buffer_len)
- {
- error("send() sent a different number of bytes than expected");
- }
- string type = v[0];
- string client_file_name =v[1];
- if (type== "GET" || type == "get") {
- get(sock, client_file_name);
- } else if(type == "POST" || type == "post") {
- post(sock, client_file_name);
- } else {
- error("incorrect type");
- }
- }
- fclose(file);
- }
- /* Size of receive buffer */
- /* Error handling function */
- int main(int argc, char *argv[]) {
- char* input = "post 1.jpg 127.0.0.1 5000";
- //char* ip = "127.0.0.1";
- //int port = 5000;
- if (argc < 3) {
- fprintf(stderr,"ERROR, in ruuning command\n");
- exit(1);
- }
- int sock; /* Socket descriptor */
- struct sockaddr_in echoServAddr; /* Echo server address */
- unsigned short echoServPort;
- char *servlP;
- char *echoString;
- char echoBuffer[RCVBUFSIZE];
- unsigned int echoStringLen;
- int bytesRcvd, totalBytesRcvd;
- /* Echo server port */
- /* Server IP address (dotted quad) */
- /* String to send to echo server */
- /* Buffer for echo string */
- /* Length of string to echo */
- /* Bytes read in single recv()
- and total bytes read */
- servlP = argv[1];
- echoString = input;
- echoServPort = atoi(argv[2]); /* Use given port, if any */
- /* Create a reliable, stream socket using TCP */
- cout <<"before accept"<< endl;
- if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- error(" socket () failed");
- /* Construct the server address structure */
- memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
- echoServAddr.sin_family = AF_INET; /* Internet address family */
- echoServAddr.sin_addr.s_addr = inet_addr(servlP); /* Server IP address */
- echoServAddr.sin_port = htons(echoServPort); /* Server port */
- /* Establish the connection to the echo server */
- if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))
- < 0)
- error(" connect () failed");
- //echoStringLen = strlen(echoString); /* Determine input length */
- /* Send the string to the server */
- /* if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
- {
- error("send() sent a different number of bytes than expected");
- }*/
- input_file_reader(sock, "input_file.txt");
- //parse the input string here
- //post(sock, "1.jpg");
- close(sock);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement