Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to read a file and send it to client, the client has to receive the file and send the same file back to server. I have developed the code, but i am not able to send and receive data. Can someone guide me in sorting this issue out? I am giving the code for further reference.
/****************** SERVER CODE ****************/

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main()
{
  int socket_desc, newSocket;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */

  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  
  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */

  serverAddr.sin_family = AF_INET;

  /* Set port number, using htons function to use proper byte order */

  serverAddr.sin_port = htons(6777);

  /* Set IP address to localhost */

  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

  /* Set all bits of the padding field to 0 */

  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Bind the address struct to the socket ----*/

  bind(socket_desc, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
  puts("Bind Completed:");

  /*---- Listen on the socket, with 5 max connection requests queued ----*/

  if(listen(socket_desc,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  /*---- Accept call creates a new socket for the incoming connection ----*/

  addr_size = sizeof serverStorage;
  newSocket = accept(socket_desc, (struct sockaddr *) &serverStorage, &addr_size);
  puts("Connection is Accepted:");

  /*---- Send message to the socket of the incoming connection ----*/

   FILE *fp;
   FILE *recvfp;   
   int size=0,read_size=0,tot_size=0; 
   int i=0,stat=0,write_size=0,file_size=1;
   char file_buffer[1024],received_int[10],clientSocket;
   fp = fopen("/home/sosdt009/Desktop/character2.txt","r");
   recvfp = fopen("/home/sosdt009/Desktop/Rrec.txt","w");
   fseek(fp, 0, SEEK_END); // seek to end of file
   size = ftell(fp); // get current file pointer   
   while (size>tot_size)
   {
 		//Read from the file into our send buffer
		printf("Ready for sending \n");
		read_size = fread(buffer,sizeof(buffer),1,fp);	
		printf("Read size value is %d: \n",read_size);	
		//printf(" Multiple loop \n");		
		stat = send(clientSocket,buffer,read_size, 0);
		while (stat < 0);
                tot_size=tot_size+read_size;   		  
   }
    while(file_size>0)
   {
     printf("Receiving: \n");
     file_size=recv(clientSocket,file_buffer,sizeof(file_buffer), 0);
     printf("%d",file_size);
     write_size = fwrite(file_buffer,1,file_size,recvfp); 
     printf("bytes=%d",write_size);
   }
  shutdown(clientSocket,1);
  fclose(fp);
  fclose(recvfp);
  close(clientSocket);
  //printf("read size value is %d: \n",read_size);	
  //printf("size=%d",size);
  return 0;
}


and the client code is as follows


/****************** CLIENT CODE ****************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>

void main()
{
  int ClientSocket;
  struct sockaddr_in serverAddr;
  socklen_t addr_size;

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */

  ClientSocket = socket(AF_INET, SOCK_STREAM, 0);
  
  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */

  serverAddr.sin_family = AF_INET;

  /* Set port number, using htons function to use proper byte order */

  serverAddr.sin_port = htons(6777);

  /* Set IP address to localhost */

  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

  /* Set all bits of the padding field to 0 */

  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Connect the socket to the server using the address struct ----*/

  addr_size = sizeof serverAddr;
  if(connect(ClientSocket, (struct sockaddr *) &serverAddr, addr_size)<0)
  {
	puts("Connection failed:");
	exit(0);
  }
  else
  {
	puts("Connection open for data:");
  }  
   FILE *fp;
   FILE *recvfp;   

   int size=0,read_size=0,tot_size=0; 
   int i=0,stat=0,write_size=0,file_size=1;
   char buffer[1024],file_buffer[1024],received_int[10];

   fp = fopen("/home/sosdt009/Desktop/character.txt", "r");
   recvfp = fopen("/home/sosdt009/Desktop/character.txt", "w");

   //fseek(fp, 0, SEEK_END); // seek to end of file
   //size = ftell(fp); // get current file pointer    
    
  /* sprintf(received_int, "%ld", size);
    printf("Sent int=%s",received_int);*/
  /*  send(clientSocket,received_int,sizeof(received_int),0);*/
    
  /* strcpy(received_int,"empty\n");
    recv(clientSocket, received_int,sizeof(received_int), 0);*/

  //write(clientSocket,&size, sizeof(size));
  // read(clientSocket, &received_int, sizeof(received_int));
  
  //recv(clientSocket, received_int,sizeof(received_int), 0);
  //printf("Received int=%s",received_int); 
  // fseek(fp, 0, SEEK_SET);
  // fread(buffer,size, 1, fp);   

  while(file_size>0)
   {
     printf("Receiving: \n");
     file_size=recv(ClientSocket,file_buffer,sizeof(file_buffer), 0);
     printf("%d",file_size);
     write_size = fwrite(file_buffer,1,file_size,recvfp); 
     printf("bytes=%d",write_size);
   }
  while (size>tot_size)
  { 
		//Read from the file into our send buffer
		//printf("Ready for sending \n");
		read_size = fread(file_buffer,1,sizeof(file_buffer),fp);	
		printf("read size value is %d: \n",read_size);	
		do
		{
			printf(" Multiple loop \n");		
			stat = send(ClientSocket,buffer,read_size, 0);
		} while (stat < 0);
                tot_size=tot_size+read_size;    		  
  }
  shutdown(ClientSocket,1);
  fclose(fp);
  printf("read size value is %d: \n",read_size);	
  printf("size=%d",size);
 /* while(file_size>0)
  {
     printf("Receiving: \n");
     file_size=recv(clientSocket,file_buffer,sizeof(file_buffer), 0);
     printf("%d",file_size);
     write_size = fwrite(file_buffer,1,file_size,recvfp); 
     printf("bytes=%d",write_size);
  }*/

  fclose(recvfp);
  close(ClientSocket);
/*Complete file block*/

//printf("%ld,%s\n", size,buffer);
//send(clientSocket,buffer,size,0);
//strcpy(buffer,"empty\n");
//recv(clientSocket, buffer, 1024, 0);

  /*---- Print the received message ----*/
//printf("Data received: %s",buffer);   
}
Posted
Comments
venkat28vk 16-Oct-15 2:34am    
The connections are established, but the problem here is i am not able to send and receive the file.
CPallini 16-Oct-15 3:24am    
What do you mean with 'i am not able to send and receive data'? What is the problem, exactly?
venkat28vk 16-Oct-15 3:29am    
@pallini. I need to read a file and send it to the client and the client has to send the same file back to server, this is the actual requirement. As far as i have done is just the connection is established, but not able to read and write data.

1 solution

I don't know if there are more errors but I found at least three:

In your server code you are using clientSocket for reading and writing but it has never been initialised. Use the socket descriptor returned by the accept call instead (newSocket).

In your server code you are setting the file pointer of the input file to the end to get the file size but forgot to set it back to the begin before reading from the file.

This server code part is wrong:
C++
stat = send(clientSocket,buffer,read_size, 0);
while (stat < 0);
        tot_size=tot_size+read_size;

When the return value is -1, the call failed (and should be here according to the first error described above).

While your program does not work as intended you should check all function return values and print them upon failure. So you know which call fails at which program step. You did this already for some calls. So add the output to your question. That makes it much easier for us to help.
 
Share this answer
 
Comments
venkat28vk 16-Oct-15 4:59am    
I have changed with what u have said, but still i am not able to send and receive data.
Jochen Arndt 16-Oct-15 5:15am    
I anticipated that there are more errors.

The process of finding errors is called debugging. You should do that know. Use the debugger or the poor mans debugging by printing out program states. So you know where errors occur first which makes it much easier to locate the source. Concentrate on the first error that occurs because that usually leads to additional ones.

Imagine that I found three errors just by reading the code. But that is a very hard task. It is much easier to find errors during debugging. But only you have the final abilities to do that (no one here will create a project to run your code).

If you got stuck locating an error, post the relevant code part until the error occurs and post the error (usually the return value of a function). Then we may help to fix this error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900