Exp 11
Exp 11
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main()
{
int serversocket, clientsocket;
struct sockaddr_in serveraddr, clientaddr;
socklen_t len;
FILE *ptr;
char filename[20];
char file[2048], temp[500];
bzero(filename, sizeof(filename));
bzero(file, sizeof(file));
serversocket = socket(AF_INET, SOCK_STREAM, 0);
if (serversocket < 0) {
perror("Error opening socket");
exit(1);
}
bzero((char*)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(5015);
serveraddr.sin_addr.s_addr = INADDR_ANY;
if (bind(serversocket, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0) {
perror("Error on binding");
exit(1);
}
listen(serversocket, 5);
printf("\nWaiting for the connection\n");
bzero((char*)&clientaddr, sizeof(clientaddr));
len = sizeof(clientaddr);
clientsocket = accept(serversocket, (struct sockaddr*)&clientaddr, &len);
if (clientsocket < 0) {
perror("Error on accept");
exit(1);
}
printf("\nConnection obtained.\n");
if (recv(clientsocket, filename, sizeof(filename), 0) < 0) {
perror("Error reading from socket");
exit(1);
}
printf("\nThe read filename is:\t%s\n", filename);
printf("\nReading the file.\n");
ptr = fopen(filename, "r");
if (ptr == NULL) {
perror("Error opening file");
strcpy(file, "File not found");
} else {
while (fgets(temp, sizeof(temp), ptr) != NULL) {
strcat(file, temp);
}
fclose(ptr);
}
printf("\nSending the file.\n");
if (send(clientsocket, file, sizeof(file), 0) < 0) {
perror("Error writing to socket");
exit(1);
}
close(clientsocket);
close(serversocket);
return 0;
}
Client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define FILENAME "sample.c"
int main()
{
int clientsocket;
struct sockaddr_in serveraddr;
struct hostent *server;
FILE *ptr;
char file[2048];
bzero(file, sizeof(file));
clientsocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientsocket < 0) {
perror("Error opening socket");
exit(1);
}
bzero((char*)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(5015);
server = gethostbyname("127.0.0.1");
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
exit(1);
}
bcopy((char*)server->h_addr, (char*)&serveraddr.sin_addr.s_addr, server->h_length);
printf("\nTrying to connect to the server.\n");
if (connect(clientsocket, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0) {
perror("Error connecting");
exit(1);
}
printf("\nConnected to the server.\n");
if (send(clientsocket, FILENAME, sizeof(FILENAME), 0) < 0) {
perror("Error writing to socket");
exit(1);
}
printf("\nSending filename.\n");
if (recv(clientsocket, file, sizeof(file), 0) < 0) {
perror("Error reading from socket");
exit(1);
}
printf("\nThe file is:\n%s", file);
ptr = fopen(FILENAME, "w");
if (ptr == NULL) {
perror("Error opening file");
exit(1);
}
fprintf(ptr, "%s", file);
fclose(ptr);
close(clientsocket);
return 0;
}
Output:
Client:
Trying to connect to the server.
Connected to the server.
Sending filename.
The file is:
Hi Hello
HH
Server:
Waiting for the connection
Connection obtained.
The read filename is: sample.c
Reading the file.
Sending the file.