943,785 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1181
  • C RSS
Nov 29th, 2008
0

UNIX Client and Server

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manojn1979 is offline Offline
4 posts
since Nov 2008
Nov 29th, 2008
0

Re: UNIX Client and Server

This isnt an intro. Moved to the C board.
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,505 posts
since Apr 2005
Nov 29th, 2008
0

Re: UNIX Client and Server

Hi

I can't get you... What does it mean move to c board.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manojn1979 is offline Offline
4 posts
since Nov 2008
Nov 29th, 2008
0

Re: UNIX Client and Server

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.
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,505 posts
since Apr 2005
Nov 29th, 2008
0

Re: UNIX Client and Server

Thank You.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manojn1979 is offline Offline
4 posts
since Nov 2008
Dec 2nd, 2008
0

Re: UNIX Client and Server

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
d0pedup is offline Offline
14 posts
since Dec 2008
Dec 2nd, 2008
0

Re: UNIX Client and Server

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manojn1979 is offline Offline
4 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Calculate text bounding box size in pixels
Next Thread in C Forum Timeline: need urgent responce please!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC