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 512
- int main(void) {
- char message[1024];
- struct sockaddr_in adr;
- int socket;
- char separator[] = " ";
- char * token;
- socket = socket(PF_INET, SOCK_STREAM, 0);
- adr.sin_family = AF_INET;
- adr.sin_port = htons(3047);
- adr.sin_addr.s_addr = inet_addr("150.254.79.243");
- if (connect(socket, (struct sockaddr*) &adr,
- sizeof(adr)) < 0)
- {
- printf("Nawiazanie polaczenia nie powiodlo sie.\n");
- return 1;
- }
- while(1) {
- memset(message, 0, 1024);
- printf("Polaczenie nawiazane.\nPodaj message: ");
- fflush(stdout);
- if (fgets(message,1024,stdin) != NULL)
- {
- printf("-------------------- ");
- }
- token = strtok( message, separator );
- printf("token: %s\n", token);
- if(strncmp(token, "send", 4) == 0) {
- printf( "wywolaj funkcje: send\n" );
- send(socket, token, strlen(token), 0);
- printf("message wyslana (message sent).\n");
- } else if(strncmp(token, "list", 4) == 0) {
- printf( "wywolaj funkcje: list\n");
- } else if(strncmp(token, "quit", 4) == 0) {
- send(socket, token, strlen(token), 0);
- close(socket);
- exit(0);
- } else {
- printf("Nieznana komenda!\n");
- }
- }
- close(socket);
- /* Send File to Server */
- int sendFile() {
- char* fs_name = "file.txt"; //to variable
- char sdbuf[LENGTH];
- printf("[Client] Sending %s to the Server...", fs_name);
- FILE *fs = fopen(fs_name, "r");
- if(fs == NULL)
- {
- printf("ERROR: File %s not found.\n", fs_name);
- exit(1);
- }
- bzero(sdbuf, LENGTH);
- int fs_block_sz;
- //int success = 0;
- while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs))>0)
- {
- if(send(socket, sdbuf, fs_block_sz, 0) < 0)
- {
- printf("ERROR: Failed to send file %s.\n", fs_name);
- break;
- }
- bzero(sdbuf, LENGTH);
- }
- printf("Ok File %s from Client was Sent!\n", fs_name);
- //success = 1;
- }
- /* 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