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>
- using namespace std;
- // void DieWithError(char *errorMessage);
- 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)
- // DieWithError("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)
- // DieWithError("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)
- {}
- //DieWithError("recv() failed");
- }
- fclose(file);
- }
- void post(int sock, string fileName) {
- FILE *file = fopen(fileName.c_str(), "rb");
- if (file == NULL) {//file not found
- // DieWithError("file not found");
- }
- 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)
- {}
- // DieWithError("send() failed");
- }
- fclose(file);
- }
- /* Size of receive buffer */
- /* Error handling function */
- int main(int argc, char *argv[]) {
- char* input = "Post 1.jpg 192.168.1.101 5000";
- char* ip = "127.0.0.1";
- int port = 5000;
- 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 = ip;
- echoString = input;
- echoServPort = port; /* 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)
- // // DieWithError("send() sent a different number of bytes than expected");
- // //parse the input string here
- // post(sock, "1.jpg");
- //
- // close(sock);
- // exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement