Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

When i am running TCP/IP in localhost i am able to read and write the images,but when android is sending any image to C, i am able to receive only the icon of the image file, the img is not getting displayed....what may be the causes for it..
C++
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>          
#include <netdb.h>

#define LENGTH 5000

void error(const char *msg)
{
	perror(msg);
	exit(1);
}

int main ()
{
	/* Defining Variables */
	int sockfd; 
	int nsockfd; 
	int num;
	int sin_size, PORT; 
	struct sockaddr_in addr_local; /* client addr */
	struct sockaddr_in addr_remote; /* server addr */
	char revbuf[LENGTH]; 		// Receiver buffer

	/* Get the Socket file descriptor */

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
	{
		fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor: (errno = %d) \n", errno);
		exit(1);
	}
	else 
		printf("[Server] Obtaining socket descriptor successfully.\n");

	/* Fill the client socket address struct */
	addr_local.sin_family = AF_INET;          // Protocol Family
	addr_local.sin_port = htons(6777);        // Port number
	addr_local.sin_addr.s_addr = INADDR_ANY;  // AutoFill local address
	bzero(&(addr_local.sin_zero), 8);         // Flush the rest of struct

	/* Bind a special Port */
	if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 )
	{
		fprintf(stderr, "ERROR: Failed to bind Port. (errno = %d)\n", errno);
		exit(1);
	}
	else 
		printf("[Server] Binded tcp port %d in addr sucessfully.\n",PORT);

	/* Listen remote connect/calling */
	if(listen(sockfd,8) == -1)
	{
		fprintf(stderr, "ERROR: Failed to listen Port. (errno = %d)\n", errno);
		exit(1);
	}
	else
		printf ("[Server] Listening to the port %d successfully.\n", PORT);

	int success = 0;
	while(success == 0)
	{
		sin_size = sizeof(struct sockaddr_in);

		/* Wait for a connection and obtain a new socket file despriptor for single connection */

		if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == 1)
		{
		    fprintf(stderr, "ERROR: Obtaining new Socket Despcritor. (errno = %d)\n", errno);
		    exit(1);
		}
		 else
    		    printf("[Server] Server has got connected from %s.\n", inet_ntoa(addr_remote.sin_addr));

		/*Received File from Client */
		char* fr_name = "/home/sosdt011/Desktop/imagenew.png";
		FILE *fr = fopen(fr_name, "w");
		if(fr == NULL)
		{
			printf("File %s Cannot be opened file on server.\n", fr_name);
		}
		else
		{
			//bzero(revbuf, LENGTH); 
			int fr_block_sz = 0;
			while((fr_block_sz = recv(nsockfd, revbuf, LENGTH,0)) > 0) 
			{
				int write_sz = fwrite(revbuf, 1 , fr_block_sz , fr); // use actual size of buffer
				if(write_sz < fr_block_sz)
				{
					error("File write failed on server.\n");
				}
		    	        bzero(revbuf,LENGTH);
			    	if (fr_block_sz == 0 ) 
			    	{
           				break;
			    	}
			}
			if(fr_block_sz < 0)  
		        {
		        	if (errno == EAGAIN)
	        		{
		                	printf("recv() timed out.\n");
     	                	}
	            		else
	            		{
	                		fprintf(stderr, "recv() failed due to errno = %d\n", errno);
		 			exit(1);
	            	        }
        		}
			printf("Ok received from client!\n");
			fclose(fr); 
		}
	    	success = 1;
	    	close(nsockfd);
	    	printf("[Server] Connection with Client closed. Server will wait now...\n");
	    	while(waitpid(-1, NULL, WNOHANG) > 0);
	}
}
Posted
Updated 21-Jan-15 0:19am
v10
Comments
dan!sh 12-Jan-15 6:50am    
You have been here for a while. Can you post formatted and only relevant code snippets? Check the guidelines on how to ask questions.
CHill60 12-Jan-15 7:50am    
You've removed the code instead of formatting it
venkat28vk 12-Jan-15 7:52am    
the code is replica of tcp/ip communication, for any reference if you need i shall give u the code...
CHill60 12-Jan-15 8:00am    
Well it is quite difficult to see what is wrong with your code if you don't post it ;-p
CHill60 12-Jan-15 8:10am    
Sorry - this is unreadable and won't even compile. See the comment from d@nish - please post formatted code in your question.

1 solution

Your code is incorrect, you are writing each block to your output file using the size of an integer, rather than the actual length of the data received. Try changing it as below:
C++
while((fr_block_sz = recv(nsockfd, revbuf, LENGTH,0)) > 0) 
 
{
 
int write_sz = fwrite(revbuf, 1 , fr_block_sz , fr); // use actual size of buffer
if(write_sz < fr_block_sz)

{
 
error("File write failed on server.\n");
 
}
 
Share this answer
 
Comments
nv3 20-Jan-15 8:36am    
Good catch!
Richard MacCutchan 20-Jan-15 11:39am    
Makes up for the other blunder.
venkat28vk 20-Jan-15 23:34pm    
after compiling this code i have got an error after i have opened the img

Error interpreting JPEG image file (Improper call to JPEG library in state 200) what does this implicate,
venkat28vk 21-Jan-15 1:15am    
can i post my entire code for better understanding
Richard MacCutchan 21-Jan-15 3:02am    
I have no idea what that error message may mean, you would need to check the documentation of the library in question. As for posting all you code, please don't. Show only the parts where the error occurred, and include all information relevant to that 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