sheebs
sheebs

Reputation: 129

How to send text file from server to client using C programming

I am trying to send a text file from a server to a client, but am not sure on how to proceed with coding this.

I have been reading some tutorials and managed to receive data from a client, then send a confirmation back to the client saying that the data had been received. But I would like to modify this so that I am sending an entire text file over to a client.

I am a total newbie to C and TCP socket programming, so any insight would be greatly appreciated.

My code for the server so far:

/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg) {
    perror(msg);
    exit(1);
}

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

    int sockfd, newsockfd, portno;
    socklen_t clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    if (argc < 2) {
        fprintf(stderr, "ERROR, no port provided\n");
        exit(1);
    }
    // Create a TCP socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");
    bzero((char *) &serv_addr, sizeof (serv_addr));
    portno = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    // Assign a name/port number to socket
    if (bind(sockfd, (struct sockaddr *) &serv_addr,
            sizeof (serv_addr)) < 0)
        error("ERROR on binding");
    // Establish a queue for connections
    listen(sockfd, 5);
    clilen = sizeof (cli_addr);
    // Extract a connection from the queue
    newsockfd = accept(sockfd,
            (struct sockaddr *) &cli_addr,
            &clilen);
    if (newsockfd < 0)
        error("ERROR on accept");
    bzero(buffer, 256);
    // Read/write
    n = read(newsockfd, buffer, 255);
    if (n < 0) error("ERROR reading from socket");
    printf("Here is the message: %s\n", buffer);
    n = write(newsockfd, "I got your message", 18);
    if (n < 0) error("ERROR writing to socket");
    close(newsockfd);
    close(sockfd);
    return 0;
}

Upvotes: 4

Views: 8003

Answers (3)

moshbear
moshbear

Reputation: 3322

Use a

while ((nread = read(filefd, filebuf, sizeof(filebuf))) > 0) {
     size_t toread = nread, off = 0, wr;
     while (toread > 0) {
         wr = write(connfd, filebuf + off, toread);
         if (wr >= 0) {
             toread -= wr;
             off += wr;
         } else {
             /* handle a write error */
         }
    }
}

loop.

Upvotes: 1

qwerty123
qwerty123

Reputation: 479

On the server side, from where the file is to be send use

k=sendfile(sock_desc2,filehandle,NULL,obj.st_size);

On the client side receive it with read and write commands as in the below code

I had a similar assignment recently.Check out the code. Hope it is working! http://pastebin.com/Nep9ws9T

Upvotes: 0

znlyj
znlyj

Reputation: 1149

Maybe UDP will be concise.

ssize_t sendto(int sockfd, const void *buf, size_t nbytes, int flags, const struct sockaddr * destaddr, socklen_t desteln);
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen);

doesn't this work? send(new_fd, buf, bufsize, 0)

Upvotes: 0

Related Questions