Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi,

I want to send a file from client to server using tcp/ip, along with its file name. How to achieve the above task. I have developed the code and i have used string token. I am giving the code for reference.
C#
#include<stdio.h>
 #include<unistd.h>
 #include<string.h>    
 #include<stdlib.h>
 #include<sys/ioctl.h>
 #include<sys/types.h>
 #include<arpa/inet.h>
 #include<sys/socket.h>
int receive_text(long long int socket)
{ 
	long int buffersize = 0, recv_size = 0, size = 0, read_size, write_size; 
	char verify = '1',c; 
	int errno;
	FILE *text;
	char *pch;
	char *str="/home/sosdt009/Documents";
	char *fname[2];
	char *filename[10];
	char *filebody[1024];
  	int i=0;
 
	//Find the size of the text
	recv(socket, (char *)&size, sizeof(int), 0);
 	printf("Size value is:%ld\n",size);
	//Send our verification signal
	send(socket, &verify, sizeof(char), 0);
 	printf("Size value is:%ld\n",size);
	//Make sure that the size is bigger than 0
	if (size <= 0)
	{
		printf("Error has occurred. Size less than or equal to 0 \n");
		return -1;
	} 
	
	/*if (text == NULL)
	{
		printf("Error has occurred, file could not be opened \n");
		return -1;
	}*/
			 
	//Loop while we have not received the entire file yet

	while (recv_size < size)
	{
		ioctl(socket, FIONREAD, &buffersize);
 
		//We check to see if there is data to be read from the socket 
		if (buffersize > 0)
		{
			char *pBuf = malloc(buffersize);
			printf("Buffer value is:%s\n",pBuf);
			if (!pBuf)
			{
				fprintf(stderr, "Memory Error. Cannot allocate!\n");
				exit(-1);
			} 
			read_size = recv(socket, pBuf, buffersize, 0);
			printf("read size is:%ld\n",read_size);
			if (read_size  < 0)
			{
				printf("%s", strerror(errno));
			}
			//printf ("Splitting string \"%s\" into tokens:\n");
  			pch = strtok (pBuf,"@");
			printf(" value is:%s\n",pch);
  			while (pch != NULL)
  			{
				filename[i]=pch;
				strcpy(str,filename[i]);
				printf("the string copy is: %s\n",str);
				text = fopen(str, "w"); 
    				/*printf ("filename=%s\n",filename[i]);
 				pch = strtok (NULL, "@");*/
				i++;
				filebody[i]=pch;
    				printf ("filebody=%s\n",filebody[i]);
 				pch = strtok (NULL, "@");
				while ((filebody[i] = strtok(NULL, "@")) != NULL)
				printf("Next: %s\n",filebody[1024]);
				//strcpy(filename[i],"filename");
				//strcpy(filebody[i],pBuf);
				//strcat(filename[i],filebody[i]);
				pBuf=filename[i];
  			}		
 
			//Write the currently read data into our text file
			write_size = fwrite(pBuf, 1, buffersize, text); 
			free(pBuf);
 
			//Increment the total number of bytes read
			recv_size += read_size;
		}
	} 
	fclose(text);
	printf("File successfully Received! \n");
	return 1;
}
int main(int argc , char *argv[])
{
      long long int socket_desc , new_socket, c, read_size, buffer = 0;
      struct sockaddr_in server , client;
      char *readin;

      //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( 6777 );

      //Bind

     if( bind(socket_desc,(struct sockaddr *)&server ,sizeof(server)) < 0)
     {
       puts("bind failed");
       return 1;
     }
     puts("Bind completed");

     //Listen

     listen(socket_desc,3);

      //Accept and incoming connection

      puts("Waiting for incoming connections...");
      c = sizeof(struct sockaddr_in);
      if((new_socket = accept(socket_desc,(struct sockaddr *)&client,(socklen_t *)&c)))
      {
		puts("Connection accepted");
      }
      fflush(stdout);
      if (new_socket<0)    
      {
      	perror("Accept Failed");
      	return 1;
      }
    	receive_text(new_socket);
    	close(socket_desc);
    	fflush(stdout);
    	return 0;
}
Posted

ok, obviously you love re-inventing the wheel, you could use FTP for this .... what I would do is send a fixed size packet to start with, containing information for the server side - you may have things like

Filename - 512 bytes
File Size - 4 bytes long int ?
CRC - x bytes

the server receives this 'packet' from the client, then receives a data stream for File Size bytes storing it in a temporary file, and checks the CRC - if all is ok, it copies the temp file to the name indicated by the Filename - obviously the 512 bytes fr Filename, 4 bytes long int, x bytes for the CRC must be values appropriate for your system

there are other protocols you could look at btw, for inspiration/help , 'kermit' springs to mind
 
Share this answer
 
v2
Comments
venkat28vk 28-Dec-15 0:26am    
@ Garth. I am told to use only tcp/ip. It is because of that, i am using this protocol. Is there any ways to achieve the solution for the above with the code i have given. If so let me know.
Sergey Alexandrovich Kryukov 28-Dec-15 0:34am    
What you are missing is the whole concept of protocol. TCP is only the transport-layer protocol. On top of it, you need to have application-layer protocol. The right look at this is: people using "just" TCP still use such application-layer protocol, without realizing it, which is of course a bad thing. Think in terms of protocol stacks.
—SA
Garth J Lancaster 28-Dec-15 0:50am    
+5 Sergey
Sergey Alexandrovich Kryukov 28-Dec-15 1:58am    
You too. :-)
By the way, the inquirer gave me a reason to put a formal answer, too, Solution 2.
—SA
venkat28vk 28-Dec-15 2:03am    
@sergey. Will the code above code for work for reading a filename and its contents from client and send it to server, in the desired location in the server. If u find any errors, kindly guide me.
venkat28 wrote:

@sergey. Will the code above code for work for reading a filename and its contents from client and send it to server, in the desired location in the server. If u find any errors, kindly guide me.
Let's start with the first one:
C
char *str="/home/sosdt009/Documents";

You are hard-coding some path inside the function called "receive_text". If this path is used anywhere, it means the function does not receive file; it puts some file to some hard-coded location.

The whole idea is wrong. You need to define some application-layer protocol based on TCP. Such protocol, by definitions, should define behavior of two parts, not just one: server-side and client-side.

Please see:
Application layer - Wikipedia, the free encyclopedia[^],
Internet protocol suite - Wikipedia, the free encyclopedia[^],
Communications protocol - Wikipedia, the free encyclopedia[^],
Transmission Control Protocol - Wikipedia, the free encyclopedia[^].

—SA
 
Share this answer
 

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