UNIX Client and Server

Reply

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

UNIX Client and Server

 
0
  #1
Nov 29th, 2008
Hello

How to send a file from server to client.

I am a bigger in C with unix, i am trying to read a file from server and display it in client , i got a separate code to read a file , and i tried to connect the server and client with the following code is working fine only it shows a warning.

I am using the sample code from the follosing site
http://www2.ics.hawaii.edu/~esb/2003...cs651/hw1.html

the connection is working fine , but i dont know how to add the sending and receving code in to it and the text reading code in to that code...

if any one can help me , It would be much appreciated. Thank You for reading.


file reading code i am using is as fallows.

#include <stdio.h>

int main ( void )
{
static const char filename[] = "file.txt";
FILE *file = fopen ( filename, "r" );
if ( file != NULL )
{
char line [ 128 ]; /* or other suitable maximum line size */

while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
fputs ( line, stdout ); /* write the line */
}
fclose ( file );
}
else
{
perror ( filename ); /* why didn't the file open? */
}
return 0;
}


Thank u
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,127
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 528
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: UNIX Client and Server

 
0
  #2
Nov 29th, 2008
This isnt an intro. Moved to the C board.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
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
  #3
Nov 29th, 2008
Hi

I can't get you... What does it mean move to c board.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,127
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 528
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: UNIX Client and Server

 
0
  #4
Nov 29th, 2008
It was originally posted in the board which is for welcoming new members, so I moved it to the C programming language board, as it is indeed a question about C programming.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
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
  #5
Nov 29th, 2008
Thank You.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 14
Reputation: d0pedup is an unknown quantity at this point 
Solved Threads: 0
d0pedup d0pedup is offline Offline
Newbie Poster

Re: UNIX Client and Server

 
0
  #6
Dec 2nd, 2008
I'm no expert in socket programming, but i think these functions might be of some use.
  1. send() to send data
  2. recv() to receive data
Hope i helped.
Reply With Quote Quick reply to this message  
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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC