gets()有什么不同的用法 [英] What are the different use of gets()

查看:85
本文介绍了gets()有什么不同的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图使用TCP / IP协议将视频文件从Android发送到C.我的数据丢失(传输中丢失了一些字节)。



使用gets()之后,无论文件大小如何,我都能收到所有数据。



从以下评论中添加代码:[/ edit]

Hi,

I was trying to send a video file from Android to C using TCP/IP protocol. I was having data loss(some bytes were lost in the transmission).

After i used gets() i am able to receive all the data, irrespective of the file size.

[edit] Added code from comment below:[/edit]

int receive_text(int new_socket)
{
	long int buffersize = 0, recv_size = 0, size = 0, read_size, write_size;
	int errno;
	FILE *text;
	char *pBuf,a[50],filename[50],*filename2;
	printf("Enter Filename:\n");
	gets(filename);
	printf("%s",filename);
	strcat(str,filename);
	filename2 = fopen(str,"w");
	printf("Filename2:\n");
	(filename2);	
	text = fopen(str,"w");
 	if (text == NULL)
	{
		puts("Error has occurred. Text file could not be opened \n");
		gets(a);
		return -1;
	}

 	//Loop while we have not received the entire file yet
	
	while (1)
	{ 
		
		ioctl(new_socket, FIONREAD, &buffersize);//We check to see if there is data to be read from the socket
		printf("Buffersize ioctl value is %ld \n",buffersize);
		gets(a); 		
		if (buffersize > 0)
		{
			printf("Buffersize value is: %ld \n", buffersize);
			gets(a);
			pBuf = malloc(buffersize);
			if (!pBuf)
			{
				fprintf(stderr, "Memory Error. Cannot allocate!\n");
				gets(a);
				exit(-1);
			}				
			read_size = recv(new_socket, pBuf, buffersize, 0); 
			printf("Read size value is: %li \n", read_size);
			gets(a);				
			if (read_size < 1)
			{
				printf("%s", strerror(errno));
				gets(a);
				goto free;
			}
 				
			//Write the currently read data into our text file
					
			write_size = fwrite(pBuf, 1, read_size, text);		
			free(pBuf);
			printf("Writesize value is: %li \n", write_size);
			gets(a);	

			//Increment the total number of bytes read
				
			recv_size += read_size;		
			printf("Receivedsize value is: %li \n", recv_size);
			gets(a);
		}
		if((buffersize == 0) && (recv_size != 0))
		{
			goto free;
		}
	}
	free:

	//close(new_socket);
	fclose(text);
	send_text(new_socket);
	printf("Text successfully received: \n");
	gets(a);
	return 0;

}





代码已被提供以供进一步参考



The code has been put for further reference

推荐答案

永远不要使用 gets()来读取二进制数据:它会吞下'\ n'字节,如果数据包含 0 -bytes,则无法解释数据。更糟糕的是,如果你不能保证你的缓冲区足够大,可以容纳所有数据,包括下一次出现的'\ n'



gets()用于获取可打印文本,而非二进制数据。请参见 http://www.cplusplus.com/reference/cstdio/gets/ [ ^ ]





发布代码后,问题变得更加清晰:除了原始版本之外,您还插入了对 gets()的调用套接字读取,而不是像我想象的那样用 gets()替换那些调用!



显然,调用第二个无关的阅读功能并没有任何实际意义。但是,由于这种改变似乎使你的程序工作,也许对gets()的调用有隐藏的副作用,e。 G。强制你的套接字同步。



我没有套接字编程的经验,但我相信它在不同的系统上有所不同,所以你应该提一下你的OS是什么继续工作!



无论如何,你真的应该检查错误,尤其是在调用recv之后。检查操作系统上插座编程的相应文档。

[/ edit]



PS:

有看看这个: http://stackoverflow.com/questions/6979769/linux-ioctl -with-fionread-always-0 [ ^ ]您的问题可能与第二个响应中解释的内容有关 - 您应该使用 select()而不是 ioctl()
Never use gets() for reading binary data: it will swallow '\n' bytes, and you will have trouble interpreting the data if it contains 0-bytes. Even worse, you are risking a buffer overflow if you can't guarantee your buffer is big enough to hold all data up to and including the next occurence of '\n'!

gets() is meant for getting printable text, not binary data. See http://www.cplusplus.com/reference/cstdio/gets/[^]

[edit]
After you posted your code, the question becomes much clearer: You have inserted calls to gets() in addition to your original socket reads, not replaced those calls by gets() as I thought!

Obviously, calling a second - unrelated - reading function does not serve any real purpose. But since that change appeared to make your program work, maybe the calls to gets() have a hidden side effect, e. g. forcing a synchronization of your socket.

I have no experience with socket programming, but I believe it works different on different systems, so you should mention what OS you're working on!

In any case, you really should check for errors, especially after the call to recv. Check the appropriate documentation for socket programming on your OS.
[/edit]

P.S.:
Have a look at this: http://stackoverflow.com/questions/6979769/linux-ioctl-with-fionread-always-0[^] Your problem may be related to what is explained in the second response - that you should use select() rather than ioctl()


gets()用于在字符串中获取值,并像scanf()一样工作
gets() is used to in a string for taking value and as work as scanf() works


这篇关于gets()有什么不同的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆