Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <netdb.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #define MAX 80
- #define PORT 7778
- #define SA struct sockaddr
- #define BLOCK_SIZE 1024
- int main(int argc, char * argv[])
- {
- if (argc < 2)
- {
- printf("The second parameter is not specified: \ "name of the program to run on the server \"\n");
- exit(-1);
- }
- FILE * file;
- if ((file = fopen(argv[1], "r")) == NULL)
- {
- printf("Error opening file\n");
- exit(-1);
- }
- int fileno_1 = fileno(file);
- int sockfd, connfd;
- struct sockaddr_in servaddr, cli;
- // socket create and verification
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd == -1) {
- printf("Socket Creation Failed...\n");
- exit(0);
- }
- else
- printf("Socket Successfully Created...\n");
- bzero(&servaddr, sizeof(servaddr));
- // assign IP, PORT
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- servaddr.sin_port = htons(PORT);
- // connect the client socket to server socket
- if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
- printf("Connection With the Server Failed...\n");
- exit(0);
- }
- else
- printf("Connected to the Server...\n");
- // function for chat
- char buffer[BLOCK_SIZE];
- char buffer_2[BLOCK_SIZE];
- int count_chars;
- while ((count_chars = read(fileno_1, buffer, BLOCK_SIZE)) > 0)
- {
- write(sockfd, &buffer, count_chars);
- }
- printf("File sent\n");
- fclose(file);
- close(sockfd);
- if ((file = fopen("result", "w")) == NULL)
- {
- printf("Error creating program result file\n");
- //close(sockfd);
- return -1;
- }
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
- printf("Connection With the Server Failed...\n");
- exit(0);
- }
- else
- printf("Connected to the Server...\n");
- printf("Waiting for result file\n");
- while ((count_chars = read(sockfd, buffer_2, BLOCK_SIZE)) > 0)
- {
- write(fileno(file), buffer_2, count_chars);
- }
- printf("The result is saved in a file result\n");
- fclose(file);
- close(sockfd);
- printf("Success finish: %s\n", argv[0]);
- // close the socket
- close(sockfd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement