// socket program for file transfer
//Code: client.c
#include <stdio.h>
#include <arpa/inet.h>// inet_addr() header file for networking
#include <fcntl.h>// provides control over open files
#include <unistd.h>//read(), write(), close()
int main()
{
int soc, n;
char buffer[1024], fname[50];
struct sockaddr_in addr;
/* socket creates an endpoint for communication and returns a file
descriptor */
soc = socket(PF_INET, SOCK_STREAM, 0);
/*
* sockaddr_in is used for ip manipulation
* we define the port and IP for the connection.
*/
addr.sin_family = AF_INET;
addr.sin_port = htons(7891);//htons translates short integer from
host byte order to network byte order
addr.sin_addr.s_addr = inet_addr("[Link]");//local host IP address
(refer to itself)
/* keep trying to esatablish connection with server */
while(connect(soc, (struct sockaddr *) &addr, sizeof(addr))) ;
printf("\nClient is connected to Server");
printf("\nEnter file name: ");
scanf("%s", fname);
/* send the filename to the server */
send(soc, fname, sizeof(fname), 0);
printf("\nRecieved response\n");
/* keep printing any data received from the server */
while ((n = recv(soc, buffer, sizeof(buffer), 0)) > 0)
printf("%s", buffer);
return 0;
}
//Code: server.c
#include <stdio.h>
#include <arpa/inet.h>// inet_addr() header file for networking
#include <fcntl.h>// provides control over open files
#include <unistd.h>//read(), write(), close()
int main()
{
int welcome, new_soc, fd, n;
char buffer[1024], fname[50];
struct sockaddr_in addr;
welcome = socket(PF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(7891);//htons translates short integer from
host byte order to network byte order
addr.sin_addr.s_addr = inet_addr("[Link]");//local host IP address
(refer to itself)
bind(welcome, (struct sockaddr *) &addr, sizeof(addr));
printf("\nServer is Online");
/* listen for connections from the socket */
listen(welcome, 5);
/* accept a connection, we get a file descriptor */
new_soc = accept(welcome, NULL, NULL);
/* receive the filename */
recv(new_soc, fname, 50, 0);
printf("\nRequesting for file: %s\n", fname);
/* open the file and send its contents */
fd = open(fname, O_RDONLY);
if (fd < 0)
send(new_soc, "\nFile not found\n", 15, 0);
else
while ((n = read(fd, buffer, sizeof(buffer))) > 0)
send(new_soc, buffer, n, 0);
printf("\nRequest sent\n");
close(fd);
return 0;
}