Hi,

I want to transfer a file as it is via a socket...not read the file contents and send it.How can I do this?

I wrote the server and client programs.I'm sending data from server to client.

Server

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 5432        // the port client will be connecting to
int main(int argc, char *argv[])
{
    int sockfd, sockfd1, addrlen;
    struct sockaddr_in srvr_addr,cli_addr; // server address information
    FILE *fp=fopen("serv_1.mfcc","w");	
    char mfcc[16];			
 
   if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
    }
    srvr_addr.sin_family = AF_INET;      // host byte order
    srvr_addr.sin_port = htons(PORT); // short, network byte order
    srvr_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    memset(&(srvr_addr.sin_zero), '\0', 8); // zero the rest of the struct
    addrlen=sizeof(cli_addr);	
    bind(sockfd,(struct sockaddr *)&srvr_addr, sizeof(srvr_addr));	
    listen(sockfd,5);	
    sockfd1=accept(sockfd,(struct sockaddr *)&cli_addr,&addrlen);	
    while(1){
		
		read(sockfd1,mfcc,16);
		if(strcmp(mfcc,"fileend")==0)
		{
			break;	
		}
		fprintf(fp,"%s",mfcc);
		}					
    fclose(fp);	    	
    return 0;
}

Client

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 5432       
int main(int argc, char *argv[])
{
    int sockfd, len;
     struct sockaddr_in srvr_addr; // server address information
     FILE *fp=fopen("1.mfcc","r");	
     char mfcc[16];			

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
    }
    srvr_addr.sin_family = AF_INET;      // host byte order
    srvr_addr.sin_port = htons(PORT); // short, network byte order
    srvr_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    memset(&(srvr_addr.sin_zero),'\0', 8); // zero the rest of the struct
    if (connect(sockfd, (struct sockaddr *)&srvr_addr,
    sizeof(struct sockaddr)) == -1) {
    perror("connect");
    exit(1);
    }
    while(!feof(fp)){
		len=fread(mfcc,sizeof(char),15,fp);
		mfcc[len]='\0';
		write(sockfd,mfcc,len+1);
		}
    write(sockfd,"fileend",16);						
    fclose(fp);	    	
    close(sockfd);
    return 0;
}

my server side file is not getting created...im not sure why

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.