Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/socket.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <stdlib.h>
- #define LENGTH 8092
- int main(void) {
- char message[1024];
- struct sockaddr_in adr;
- int gniazdo;
- char separator[] = " ";
- char * token;
- char abcd[512];
- int port;
- printf("Podaj adres IP odbiorcy: ");
- scanf("%s", abcd);
- printf("Podaj numer portu odbiorcy: ");
- scanf("%u", & port);
- gniazdo = socket(PF_INET, SOCK_STREAM, 0);
- adr.sin_family = AF_INET;
- adr.sin_port = htons(port);
- adr.sin_addr.s_addr = inet_addr(abcd);
- if (connect(gniazdo, (struct sockaddr * ) & adr,
- sizeof(adr)) < 0) {
- printf("Nawiazanie polaczenia nie powiodlo sie.\n");
- return 1;
- }
- printf("Polaczenie nawiazane.\n");
- while (1) {
- memset(message, 0, 1024);
- printf("Podaj message: \n");
- fflush(stdout);
- if (fgets(message, 1024, stdin) != NULL) {
- printf("-------------------- ");
- }
- token = strtok(message, separator);
- printf("token: %s\n", token);
- if (strncmp(token, "send", 4) == 0) {
- send(gniazdo, "send", 4, 0);
- //memset(token, '\0', sizeof(token));
- token = strtok(NULL, separator);
- printf("TOken22 %s\n", token);
- send(gniazdo, token, strlen(token), 0);
- char * fs_name;
- strcpy(fs_name, token);
- printf("fsname %s\n", fs_name);
- char sdbuf[LENGTH];
- printf("wywalka");
- printf("[Client] Sending %s to the Server...", fs_name);
- //get file length
- printf("wywalka");
- FILE * fileptr;
- char * buffer;
- long filelen;
- printf("wywalka");
- fileptr = fopen(fs_name, "rb"); // Open the file in binary mode
- printf("wywalka");
- fseek(fileptr, 0, SEEK_END); // Jump to the end of the file
- filelen = ftell(fileptr); // Get the current byte offset in the file
- printf("file length: %ld", filelen);
- char str[12];
- memset(str, '\0', 12);
- sprintf(str, "%d\n", filelen);
- buffer = (char * ) malloc((filelen + 1) * sizeof(char));
- send(gniazdo, str, strlen(str), 0);
- fclose(fileptr); // Close the file
- FILE * fs = fopen(fs_name, "r");
- if (fs == NULL) {
- printf("ERROR: File %s not found.\n", token);
- exit(1);
- }
- bzero(sdbuf, LENGTH);
- int fs_block_sz;
- while ((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0) {
- if (send(gniazdo, sdbuf, fs_block_sz, 0) < 0) {
- printf("ERROR: Failed to send file %s.\n", token);
- break;
- }
- bzero(sdbuf, LENGTH);
- }
- printf("Ok File %s from Client was Sent!\n", token);
- //send(gniazdo, token, strlen(token), 0);
- printf("message wyslana (message sent).\n");
- } else if (strncmp(token, "list", 4) == 0) {
- int numOfPaths;
- printf("wywolaj funkcje: list\n");
- send(gniazdo, token, strlen(token), 0);
- memset(message, '\0', 1024);
- recv(gniazdo, message, 1024, 0);
- sscanf(message, "%d", & numOfPaths);
- printf("%d paths(s): \n", numOfPaths);
- for (int i = 0; i < numOfPaths; i++) {
- memset(message, '\0', 1024);
- recv(gniazdo, message, 1024, 0);
- printf("%s\n", message);
- }
- } else if (strncmp(token, "quit", 4) == 0) {
- send(gniazdo, token, strlen(token), 0);
- close(gniazdo);
- exit(0);
- } else {
- printf("Nieznana komenda!\n");
- }
- }
- close(gniazdo);
- /* Receive File from Server */
- /*printf("[Client] Receiveing file from Server and saving it as final.txt...");
- char* fr_name = "/home/aryan/Desktop/progetto/final.txt"; //to variable
- FILE *fr = fopen(fr_name, "a");
- if(fr == NULL)
- printf("File %s Cannot be opened.\n", fr_name);
- else
- {
- bzero(revbuf, LENGTH);
- int fr_block_sz = 0;
- int success = 0;
- //while(success == 0)
- //{
- while(fr_block_sz = recv(sockfd, revbuf, LENGTH, 0))
- {
- if(fr_block_sz < 0)
- {
- error("Receive file error.\n");
- }
- int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
- if(write_sz < fr_block_sz)
- {
- error("File write failed.\n");
- }
- else if(fr_block_sz)
- {
- break;
- }
- bzero(revbuf, LENGTH);
- }
- printf("Ok received from server!\n");
- success = 1;
- fclose(fr);
- //}
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement