Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #define MSG_LEN 120
- #define DFL_PORT 8006
- int get_cmd(char* line, int sock){
- FILE *fp;
- char* filename2 = malloc(strlen(line));
- strcpy(filename2, line);
- int isfileopen = 0;
- int readcount;
- char* filename = malloc(strlen(line));
- filename = strtok(filename2, " ");
- filename = strtok(NULL, " ");
- printf("filename %s, line %s_\n", filename, line);
- if( (fp = fopen(filename, "wb")) == NULL ){
- perror("get_cmd open file");
- return 1;
- }else{
- isfileopen = 1;
- write(sock, line, MSG_LEN);
- }
- while(1){
- readcount = read(sock, line, MSG_LEN);
- if(readcount<MSG_LEN) line[readcount] = '\0';
- printf("get_cmd %s_%d\n", line, readcount);
- fwrite(line, 1, readcount, fp);
- if(readcount<MSG_LEN-1){
- break;
- }
- }
- if(isfileopen){
- fclose(fp);
- }
- printf("cl get_cmd: file recieved\n");
- return 0;
- }
- int main(int argc, char *argv[]) {
- struct sockaddr_in server; //Server's address assembled here
- struct hostent *host_info;
- int sock, i;
- char *server_name;
- char* line = malloc(MSG_LEN);
- // Get server name from commandline. If none, use localhost
- if ( argc > 1 )
- server_name = argv[1];
- else
- server_name = "localhost";
- // Create the socket
- sock = socket(AF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- perror("creating stream socket");
- exit(EXIT_FAILURE);
- }
- host_info = gethostbyname(server_name);
- if (host_info == NULL) {
- fprintf(stderr, "%s: unknown host: %s\n", argv[0], server_name);
- exit(EXIT_FAILURE);
- }
- server.sin_family = host_info->h_addrtype;
- memcpy((char *)&server.sin_addr, host_info->h_addr,host_info->h_length);
- server.sin_port = htons(DFL_PORT);
- if (connect(sock, (struct sockaddr *)&server, sizeof server) < 0) {
- perror("connecting to server");
- exit(EXIT_FAILURE);
- }
- printf("connected to server %s\n", server_name);
- printf("Commands l - alias 'ls'\n g [file name] - get file from server\n\n");
- int command = 0;
- FILE *fp;
- while(1){
- int isfileopen = 0;
- fgets(line, MSG_LEN-1, stdin);
- command = line[0];
- if(command == 'g'){
- line[strlen(line)-1] = '\0'; // for deleting the '\n'
- // char that fgets put at the end
- }
- printf("line %s_%d, x_%c_x\n", line, strlen(line), line[0]);
- // for(i=0;i<strlen(line); i++){
- //
- // printf("line[%d]: %c\n",i, line[i]);
- // } // print out garbage
- if(line[0] == '\0'){
- continue;
- }
- printf("cmd %c\n", command);
- if(command == 'g'){
- get_cmd(line, sock);
- continue;
- }
- write(sock, line, MSG_LEN-1);
- printf("Get answer...\n");
- while( (i=read(sock, line, MSG_LEN)) >0 ){
- line[i] = '\0';
- printf("%d_%s_\n",i, line);
- if(i<MSG_LEN-1){
- break;
- }
- }
- bzero(line, MSG_LEN);
- if(isfileopen){
- fclose(fp);
- }
- }
- close(sock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement