I want to send one text file from client side and want to read the text file on the server side and want to display the texts on the server terminal screen. I have successfully written code for both server and multiple client. I have also sent a text file from client to server side. But now I want to know how to modify the text file and send it and read the text file on server end which is sent from the client side. My server and client code is given below together:
Server and Client Code:
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <pthread.h>
#define filename "//home//myname//Documents//folder1//folder2//input.txt"
#define MAX_CLIENTS 5
//the thread function
void *new_connection_handler(void *);
int main(int argc , char *argv[])
{
//client variables
int sock;
struct sockaddr_in server;
char buffer[256], server_reply[2000];
int len;
//server variables
int socket_desc , client_sock;
struct sockaddr_in client;
socklen_t c = sizeof(client);
//check if the the command contain less than two arguments
if(argc != 2)
{
printf("use either: %s <server/client>\n", argv[0]);
}
// If the command contains minumum 2 arguments
else{
// If argv is client then execute the client code
if(strcmp("client",argv[1]) == 0)
{
/****************/// Client code here **********************************************************************
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}
puts("Connected\n");
//keep communicating with server
/* Time to send the file */
/******************************************************/
FILE *pf;
int fsize;
pf = fopen(filename, "rb");
if (pf == NULL)
{
printf("File not found!\n");
return 1;
}
else
{
printf("Found file %s\n", filename);
fseek(pf, 0, SEEK_END);
fsize = ftell(pf);
rewind(pf);
printf("File contains %ld bytes!\n", fsize);
printf("Sending the file now\n");
}
while (1)
{
// Read data into buffer. We may not have enough to fill up buffer, so we
// store how many bytes were actually read in bytes_read.
int bytes_read = fread(buffer, sizeof(buffer), 1, pf);
if (bytes_read == 0) // We're done reading from the file
break;
if (bytes_read < 0)
{
error("ERROR reading from file\n");
}
while (bytes_read > 0)
{
int bytes_written = write(sock, buffer, bytes_read);
if (bytes_written <= 0)
{
error("ERROR writing to socket\n");
}
}
}
printf("Done Sending the File!\n");
printf("Now Closing Connection.\n");
/*********************************************************************************/
close(sock);
}
/****************/// Server code here **********************************************************************
// If argv is server then execute the server code
if(strcmp("server", argv[1]) == 0 )
{
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
bzero (&server.sin_zero, 8);
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
//Listen
listen(socket_desc , MAX_CLIENTS);
//Accept and incoming connection
printf("Waiting for incoming connections\n");
c = sizeof(client);
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, &c)) )
{
printf("Connection accepted\n");
pthread_t thread_id;
if( pthread_create( &thread_id , NULL , new_connection_handler , (void*) (intptr_t)client_sock) < 0)
{
perror("could not create thread");
return 1;
}
printf("Handler assigned\n");
}
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
}
}
return 0;
}
void *new_connection_handler(void *socket_desc)
{
//Get the socket descriptor
int sock = (intptr_t)socket_desc;
int read_size = 0;
char client_message[2000];
//PROBLEM **read the text file sent from client side and display the text on screen**
while( (read_size = recv(sock , client_message , sizeof(client_message) , 0)) > 0 )
printf("Read Text: %.*s", read_size, client_message);
if(read_size == 0)
{
printf("Client disconnected\n");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}
You had done some mistakes in your code.
int bytes_read = fread(buffer, sizeof(buffer), 1 ,pf);
This statement is wrong, when you print the bytes_read
value it print 1 has a result.If you pass this to next while it will print the h repeatedly.
Use this below statement instead of that fread:
int bytes_read = fread(buffer, 1, sizeof(buffer), pf);
This statement only returns the correct readed value.
while (bytes_read > 0)
{
int bytes_written = write(sock, buffer, bytes_read);
if (bytes_written <= 0)
{
error("ERROR writing to socket\n");
}
}
In this part of the code your program writing the readed content again and again into the socket without break, so the server is print the text without break.
So use this statement after the write() bytes_read=bytes_read-bytes_written;
if the read buffer is fully written in your socket it break the statement.
And one more thing is always try to use long data type, when you are trying to use file, because int has lower range compared to long.
UPDATE:
Put the fread(buffer, 1, sizeof(buffer), pf);
and try it. It will work.
You can use open() or fopen().
fd=open("filename",O_RDWR);
fd - returns descriptor of the opened file.
O_RDWR - which is used to open file for read and write, you must include fcntl.h.
fp=fopen("filename","r+");
fp - File pointer.
rw - Opened file for both read and write.
File content:
Chandru
ttttt.
After replace:
Stackoverflowt.
Here the content is big so the content is overwritten in the another line. To avoid that you need to write the changed content in another file. Writing in another file would be easy to append the text in the line end.