Professional Documents
Culture Documents
OS Lab Assignment
OS Lab Assignment
OS Lab Assignment
LAB ASSIGNMENT
Name EnrollNo
K. Sai Niroop Reddy 19STUCHH010134
SERVER CODE:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
listen(sockfd,5);
clilen = sizeof(cli_addr);
while(1)
{
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
length = strlen(buffer);
mid = length/2;
for(i = 0; i < mid; i++) {
leftHalf[i]= buffer[i];
}
leftHalf[i] = '\0';
for(i = mid, k = 0; i <= length; i++, k++) {
rightHalf[k]= buffer[i];
}
printf("Name of file that content need to be sent: %s\n",leftHalf);
FILE *fp = fopen(leftHalf,"r");
if(fp==NULL)
{
printf("File open error");
return 1;
}
while(1)
{
/* First read file in chunks of 256 bytes */
unsigned char buff[256]={0};
int nread = fread(buff,1,256,fp);
printf("Bytes read %d \n", nread);
}
if (nread < 256)
{
if (feof(fp))
printf("End of file\n");
if (ferror(fp))
printf("\n");
break;
}
}
close(newsockfd);
}
return 0;
}
CLIENT CODE:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero(buffer,256);
n = write(sockfd,argv[3],strlen(argv[3]));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
while(1)
{
n = write(sockfd,argv[3],strlen(argv[3]));
if (n < 0)
error("ERROR writing to socket");
bzero(argv[3],256);
while((bytesReceived = read(sockfd, buffer, 256)) > 0)
{
printf("%s ", buffer);
}
}
if(bytesReceived < 0)
{
printf("\n Read Error \n");
}
close(sockfd);
return 0;
}