Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //client
- #include<stdio.h>
- #include<string.h>
- #include<sys/socket.h>
- #include<arpa/inet.h>
- #define DEFAULT_BUFLEN 512
- #define DEFAULT_PORT 27015
- #define MAX_FILES_NAME 40
- int sock, read_size;
- struct sockaddr_in server;
- char server_reply[DEFAULT_BUFLEN];
- void login();
- void menu();
- int sendFile();
- int main(int argc , char *argv[])
- {
- //keep communicating with server
- login();
- menu();
- close(sock);
- return 0;
- }
- void login(){
- char user[DEFAULT_BUFLEN];
- char password[DEFAULT_BUFLEN];
- //Create socket
- sock = socket(AF_INET , SOCK_STREAM , 0);
- if (sock == -1)
- {
- printf("Could not create socket");
- }
- puts("Socket created");
- server.sin_addr.s_addr = inet_addr("127.0.0.1");
- server.sin_family = AF_INET;
- server.sin_port = htons( DEFAULT_PORT );
- //Connect to remote server
- if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
- {
- perror("connect failed. Error");
- return;
- }
- puts("Connected\n");
- while(1)
- {
- memset(server_reply,0,strlen(server_reply));
- printf("Enter username : ");
- scanf("%s" , user);
- //Send some data
- if( send(sock , user , strlen(user) , 0) < 0)
- {
- puts("Send failed");
- return;
- }
- printf("Enter password : ");
- scanf("%s" , password);
- //Send some data
- if( send(sock , password , strlen(password) , 0) < 0)
- {
- puts("Send failed");
- return;
- }
- //Receive a reply from the server
- if( recv(sock , server_reply , DEFAULT_BUFLEN , 0) < 0)
- {
- puts("recv failed");
- break;
- }
- puts("Server reply :");
- puts(server_reply);
- if(strcmp(server_reply, "success!") == 0){
- return;
- }
- }
- }
- void menu(){
- char message[DEFAULT_BUFLEN];
- int ind=0;
- while(!ind){
- printf("\n1.upload\n2.download\n3.file list\n4.log off\n");
- printf("enter number: ");
- scanf("%s", message);
- if(message[1]!='\0'){
- printf("Enter one-digit number!\n");
- continue;
- }
- if( send(sock , message , DEFAULT_BUFLEN , 0) < 0)
- {
- puts("Send failed");
- return;
- }
- switch(message[0]){
- case '1':
- if(sendFile()==1) continue;
- break;
- case '2':
- break;
- case '3':
- if((recv(sock , iterations , DEFAULT_BUFLEN , 0)) < 0){
- puts("recv failed");
- return;
- }
- j=atoi(iterations);
- printf("Files on server: ");
- for(i=0;i<j;i++)
- {
- if((recv(sock , server_reply , DEFAULT_BUFLEN , 0)) < 0){
- puts("recv failed");
- return;
- }
- printf("%s", server_reply);
- memset(server_reply,0,strlen(server_reply));
- break;
- }
- printf("\n");
- break;
- case '4':
- ind = 1;
- break;
- default:
- printf("Enter number between 1 and 4!\n");
- }
- }
- }
- int sendFile(){
- char succ[] = "success!", fail[] = "failure!", fileName[MAX_FILES_NAME];
- char fileSize[DEFAULT_BUFLEN], filePath[DEFAULT_BUFLEN], chunk[1000];
- FILE *uploadFile;
- int size, i, n;
- printf("Enter file name: ");
- scanf("%s", fileName);
- if(send(sock,fileName,MAX_FILES_NAME,0) < 0)
- {
- puts("Send failed");
- return 1;
- }
- memset(filePath, 0, DEFAULT_BUFLEN);
- strcpy(filePath, "./clientFiles/");
- strcat(filePath, fileName);
- printf("%s\n", filePath);
- if((uploadFile = fopen(filePath, "rb"))==NULL){
- perror("Opening upload file failed!\n");
- if(send(sock,fail,DEFAULT_BUFLEN,0) < 0)
- {
- puts("Send failed");
- return 1;
- }
- }
- if(send(sock,succ,DEFAULT_BUFLEN,0) < 0)
- {
- puts("Send failed");
- return 1;
- }
- fseek(uploadFile, 0, SEEK_END);
- size = ftell(uploadFile);
- fseek(uploadFile, 0, SEEK_SET);
- n = size/1000;
- sprintf(fileSize, "%d", size);
- if(send(sock,fileSize,DEFAULT_BUFLEN,0) < 0)
- {
- puts("Send failed");
- return 1;
- }
- for(i = 0; i < n; i++){
- fread(chunk, sizeof(char), 1000, uploadFile);
- if(send(sock,chunk,1000,0) < 0)
- {
- puts("Send failed");
- return 1;
- }
- }
- fread(chunk,sizeof(char),size%1000,uploadFile);
- if(send(sock,chunk,1000,0) < 0)
- {
- puts("Send failed");
- return 1;
- }
- fclose(uploadFile);
- printf("Closing file successful!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement