RSS Forums RSS

UNIX Client and Server

Please support our C advertiser: Programming Forums
Reply
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

  #1  
Nov 28th, 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
AddThis Social Bookmark Button
Reply With Quote  
Posts: 15,222
Reputation: jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light 
Solved Threads: 455
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: UNIX Client and Server

  #2  
Nov 29th, 2008
This isnt an intro. Moved to the C board.
Master of puppets Im pulling your strings - blinded by me, you cant see a thing. Master! Master!

If i am helpful, please give me reputation points.
Reply With Quote  
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

  #3  
Nov 29th, 2008
Hi

I can't get you... What does it mean move to c board.
Reply With Quote  
Posts: 15,222
Reputation: jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light 
Solved Threads: 455
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: UNIX Client and Server

  #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.
Master of puppets Im pulling your strings - blinded by me, you cant see a thing. Master! Master!

If i am helpful, please give me reputation points.
Reply With Quote  
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

  #5  
Nov 29th, 2008
Thank You.
Reply With Quote  
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

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

  #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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORTNUM 1995

int main(int argc, char *argv[])
{

    int mysocket, consocket, socksize;
    struct sockaddr_in dest;
    struct sockaddr_in serv;
    socksize = sizeof(struct sockaddr_in);
    memset(&dest, 0, sizeof(dest));
    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = INADDR_ANY;
    serv.sin_port = htons(PORTNUM);
    mysocket = socket(AF_INET, SOCK_STREAM, 0);
    bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
    listen(mysocket, 1);
    int i= 0;

    char buf[50];
    consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
while(1)
{
    /*consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);*/
    printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
    printf("\n\nClient Connected,\n\nFile Send Successfully\n\n");
    FILE* fp = fopen("server.txt","r");
    while(!feof(fp))
    {

        bzero(buf,sizeof(buf));
        fread(buf,sizeof(char),50,fp);
        write(consocket,buf,50);
    }
    write(consocket ,"quit1234",50);
    fclose(fp);
    return 0;
}
}

Client Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define MAXRCVLEN 500
#define PORTNUM 1995

int main(int argc, char *argv[])
{

    int mysocket,n;
    struct sockaddr_in dest;
    mysocket = socket(AF_INET, SOCK_STREAM, 0);
    memset(&dest, 0, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_addr.s_addr = inet_addr("127.0.0.1");
    dest.sin_port = htons(PORTNUM);
    connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
    printf("\n\n CLIENT CONNECTED\n\nFile Received Successfully...\n\n");
    char buf[50];
    FILE* fp = fopen("client.txt","w");
    while(1)
         {
             bzero(buf,sizeof(buf));
             read(mysocket,buf,50);
             if(strcmp(buf,"quit1234")==0)
                {
                break;
                }
             fprintf(fp,"%s",buf);
        }

    fclose(fp);
}


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.

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 You for readying.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 727 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:24 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC