Advertisement
Guest User

client

a guest
Nov 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <stdio.h> /* for printf() and fprintf() */
  2. #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
  3. #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
  4. #include <stdlib.h> /* for atoi() */
  5. #include <string.h> /* for memset() */
  6. #include <unistd.h> /* for close() */
  7. #include <iostream>
  8. #include <fstream>
  9. using namespace std;
  10.  
  11. // void DieWithError(char *errorMessage);
  12.  
  13. void error(const char *msg)
  14. {
  15.     perror(msg);
  16.     exit(1);
  17. }
  18.  
  19. #define RCVBUFSIZE 100
  20. void get(int sock, string fileName) {
  21.     FILE* file = fopen(fileName.c_str(), "wb");
  22.     int recv_size;
  23.     char res[1024];
  24.     if ((recv_size = recv(sock, res, sizeof(res), 0)) < 0)
  25.         // DieWithError("recv() failed");
  26.     cout << "response is: " << res << endl;
  27.     cout << recv_size << endl;
  28.     cout << " " << endl;
  29.     char buffer[1024];
  30.     if ((recv_size = recv(sock, buffer, sizeof(buffer), 0)) < 0)
  31.         // DieWithError("recv() failed");
  32.     while (recv_size > 0) {
  33.         cout << recv_size << endl;
  34.         fwrite(buffer, 1, recv_size, file);
  35.         if ((recv_size = recv(sock, buffer, sizeof(buffer), 0)) < 0)
  36.         {}
  37.             //DieWithError("recv() failed");
  38.     }
  39.     fclose(file);
  40. }
  41.  
  42.  
  43. void post(int sock, string fileName) {
  44.     FILE *file = fopen(fileName.c_str(), "rb");
  45.     if (file == NULL) {//file not found
  46.         // DieWithError("file not found");
  47.     }
  48.     int read_size;
  49.     char send_buffer[1024];
  50.     while (!feof(file)) {
  51.         read_size = fread(send_buffer, 1, 1024, file);
  52.         if (send(sock, send_buffer, read_size, 0) != read_size)
  53.         {}
  54.             // DieWithError("send() failed");
  55.     }
  56.     fclose(file);
  57. }
  58. /* Size of receive buffer */
  59. /* Error handling function */
  60. int main(int argc, char *argv[]) {
  61.     char* input = "Post 1.jpg 192.168.1.101 5000";
  62.     char* ip = "127.0.0.1";
  63.     int port = 5000;
  64.  
  65.     int sock; /* Socket descriptor */
  66.     struct sockaddr_in echoServAddr; /* Echo server address */
  67.     unsigned short echoServPort;
  68.     char *servlP;
  69.     char *echoString;
  70.     char echoBuffer[RCVBUFSIZE];
  71.     unsigned int echoStringLen;
  72.     int bytesRcvd, totalBytesRcvd;
  73.     /* Echo server port */
  74.     /* Server IP address (dotted quad) */
  75.     /* String to send to echo server */
  76.     /* Buffer for echo string */
  77.     /* Length of string to echo */
  78.     /* Bytes read in single recv()
  79.      and total bytes read */
  80.  
  81.     servlP = ip;
  82.     echoString = input;
  83.     echoServPort = port; /* Use given port, if any */
  84.     /* Create a reliable, stream socket using TCP */
  85.     cout <<"before accept"<< endl;
  86.     if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  87.          error(" socket () failed");
  88.  
  89.     /* Construct the server address structure */
  90.     memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
  91.     echoServAddr.sin_family = AF_INET; /* Internet address family */
  92.     echoServAddr.sin_addr.s_addr = inet_addr(servlP); /* Server IP address */
  93.     echoServAddr.sin_port = htons(echoServPort); /* Server port */
  94.     /* Establish the connection to the echo server */
  95.     if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))
  96.             < 0)
  97.          error(" connect () failed");
  98.     echoStringLen = strlen(echoString); /* Determine input length */
  99. //  /* Send the string to the server */
  100. //  if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
  101. //      // DieWithError("send() sent a different number of bytes than expected");
  102. //  //parse the input string here
  103. //  post(sock, "1.jpg");
  104. //
  105. //  close(sock);
  106. //  exit(0);
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement