View Single Post
Join Date: Nov 2008
Posts: 4
Reputation: manojn1979 is an unknown quantity at this point 
Solved Threads: 0
manojn1979 manojn1979 is offline Offline
Newbie Poster

Re: UNIX Client and Server

 
0
  #7
Dec 2nd, 2008
Hi thank u for the reply.

I got changed the code to following , now i am able to send a file from server to client . That means send A from server and Save as B in client.


Server code

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <sys/types.h>
  7. #include <netinet/in.h>
  8. #include <sys/socket.h>
  9.  
  10. #define PORTNUM 1995
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.  
  15. int mysocket, consocket, socksize;
  16. struct sockaddr_in dest;
  17. struct sockaddr_in serv;
  18. socksize = sizeof(struct sockaddr_in);
  19. memset(&dest, 0, sizeof(dest));
  20. serv.sin_family = AF_INET;
  21. serv.sin_addr.s_addr = INADDR_ANY;
  22. serv.sin_port = htons(PORTNUM);
  23. mysocket = socket(AF_INET, SOCK_STREAM, 0);
  24. bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
  25. listen(mysocket, 1);
  26. int i= 0;
  27.  
  28. char buf[50];
  29. consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
  30. while(1)
  31. {
  32. /*consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);*/
  33. printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
  34. printf("\n\nClient Connected,\n\nFile Send Successfully\n\n");
  35. FILE* fp = fopen("server.txt","r");
  36. while(!feof(fp))
  37. {
  38.  
  39. bzero(buf,sizeof(buf));
  40. fread(buf,sizeof(char),50,fp);
  41. write(consocket,buf,50);
  42. }
  43. write(consocket ,"quit1234",50);
  44. fclose(fp);
  45. return 0;
  46. }
  47. }

Client Code

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <arpa/inet.h>
  7. #include <sys/types.h>
  8. #include <netinet/in.h>
  9. #include <sys/socket.h>
  10.  
  11. #define MAXRCVLEN 500
  12. #define PORTNUM 1995
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16.  
  17. int mysocket,n;
  18. struct sockaddr_in dest;
  19. mysocket = socket(AF_INET, SOCK_STREAM, 0);
  20. memset(&dest, 0, sizeof(dest));
  21. dest.sin_family = AF_INET;
  22. dest.sin_addr.s_addr = inet_addr("127.0.0.1");
  23. dest.sin_port = htons(PORTNUM);
  24. connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
  25. printf("\n\n CLIENT CONNECTED\n\nFile Received Successfully...\n\n");
  26. char buf[50];
  27. FILE* fp = fopen("client.txt","w");
  28. while(1)
  29. {
  30. bzero(buf,sizeof(buf));
  31. read(mysocket,buf,50);
  32. if(strcmp(buf,"quit1234")==0)
  33. {
  34. break;
  35. }
  36. fprintf(fp,"%s",buf);
  37. }
  38.  
  39. fclose(fp);
  40. }


Now i am trying to read B and display in client , for that i changed the following code and inserted it in to the client code but it say file is alread open..... so i am not able to display the B in client.

What is the point in code i am missing out , how to cahge the following code to display the file B in client.

  1. static const char filename[] = "file.txt";
  2. FILE *file = fopen ( filename, "r" );
  3. if ( file != NULL )
  4. {
  5. char line [ 128 ]; /* or other suitable maximum line size */
  6.  
  7. while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
  8. {
  9. fputs ( line, stdout ); /* write the line */
  10. }
  11. fclose ( file );
  12. }
  13. else
  14. {
  15. perror ( filename ); /* why didn't the file open? */
  16. }
  17. return 0;
  18. }

Thank You for readying.
Reply With Quote