⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 00000004.htm

📁 水木清华BBS
💻 HTM
📖 第 1 页 / 共 2 页
字号:
发信人:&nbsp;stevenc&nbsp;(stevenc),&nbsp;信区:&nbsp;Java&nbsp;<BR>&nbsp;<BR>我在使用socket建立了网络通信后,使用DataInputStream
&nbsp;<BR>和DataOutputStream进行数据流的传送,client端能送出文件,
&nbsp;<BR>但server却不能正常收取全部文件,而只能接收到一部分文件,
&nbsp;<BR>大约只有9KB左右,其余的不知所踪,而且这9KB也只是文件的
&nbsp;<BR>前一部分。
&nbsp;<BR>这到底是怎么回事,还请大虾们多多指教。
&nbsp;<BR>小弟是位新手,如今是火烧眉毛,哪位大虾如果不嫌弃的话,
&nbsp;<BR>还请您直接把解决方法邮至<A HREF="mailto:chenqch@163.net。小弟这里上
">chenqch@163.net。小弟这里上
</A>&nbsp;<BR>清华比较困难,这里就先多谢了。
&nbsp;<BR>程序如下:
&nbsp;<BR>public&nbsp;class&nbsp;FileClientThread&nbsp;extends&nbsp;Thread
&nbsp;<BR>	{
&nbsp;<BR>		Socket&nbsp;miniClient;
&nbsp;<BR>		DataOutputStream&nbsp;out1;
&nbsp;<BR>		String&nbsp;ip;
&nbsp;<BR>		FileInputStream&nbsp;infile;
&nbsp;<BR>		byte&nbsp;read[];
&nbsp;<BR>		int&nbsp;r;
&nbsp;<BR>		boolean&nbsp;flag=true;
&nbsp;<BR>		File&nbsp;file2;
&nbsp;<BR>		//File&nbsp;file3;
&nbsp;<BR>		//FileOutputStream&nbsp;outfile;
&nbsp;<BR>		
&nbsp;<BR>		FileClientThread()
&nbsp;<BR>		{
&nbsp;<BR>			super(&quot;FileClient&nbsp;Thread&quot;);
&nbsp;<BR>		}
&nbsp;<BR>		
&nbsp;<BR>		public&nbsp;void&nbsp;run()
&nbsp;<BR>		{
&nbsp;<BR>		&nbsp;&nbsp;&nbsp;&nbsp;while(flag)
&nbsp;<BR>		&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;<BR>				
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;ip=edit1.getText();
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;miniClient=new&nbsp;Socket(ip,2002);
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception&nbsp;r)
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;<BR>				richEdit3.setText&quot;outputsocketerror&quot;+r);
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;<BR>			
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;filename=edit4.getText();
&nbsp;<BR>					file2=new&nbsp;File(&quot;C:\\My&nbsp;Documents\\Visual&nbsp;Studio&nbsp;Projects\\xianliao&quot;,filen&nbsp;<BR>ame);
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;infile=new&nbsp;FileInputStream(file2);
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;read=new&nbsp;byte[(int)file2.length()];
&nbsp;<BR>				
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;r=infile.read(read);
&nbsp;<BR>				
&nbsp;<BR>				/*file3=new&nbsp;File(&quot;C:\\My&nbsp;Documents\\Visual&nbsp;Studio&nbsp;Projects\\xianliao&quot;,&quot;win&nbsp;<BR>.gif&quot;);
&nbsp;<BR>				outfile=new&nbsp;FileOutputStream(file3);
&nbsp;<BR>				outfile.write(read);*/
&nbsp;<BR>				
&nbsp;<BR>				out1=new&nbsp;DataOutputStream(new&nbsp;BufferedOutputStream(miniClient.getOutputStr&nbsp;<BR>eam()));
&nbsp;<BR>								
&nbsp;<BR>				out1.writeInt(read.length);
&nbsp;<BR>				
&nbsp;<BR>				out1.write(read,0,read.length);
&nbsp;<BR>				
&nbsp;<BR>				out1.writeBytes(file2.getName());&nbsp;
&nbsp;<BR>				
&nbsp;<BR>				infile.close();
&nbsp;<BR>				//outfile.close();
&nbsp;<BR>				out1.close();
&nbsp;<BR>				miniClient.close();
&nbsp;<BR>				
&nbsp;<BR>				flag=false;
&nbsp;<BR>				
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception&nbsp;r)
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;richEdit3.setText(&quot;output&nbsp;error&quot;+r);
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;<BR>			}
&nbsp;<BR>		}
&nbsp;<BR>	}
&nbsp;<BR>	
&nbsp;<BR>	public&nbsp;class&nbsp;FileServerThread&nbsp;extends&nbsp;Thread
&nbsp;<BR>	{
&nbsp;<BR>		DataInputStream&nbsp;in1;
&nbsp;<BR>		byte[]&nbsp;b;
&nbsp;<BR>		ServerSocket&nbsp;server1;
&nbsp;<BR>		Socket&nbsp;client;
&nbsp;<BR>		File&nbsp;file;
&nbsp;<BR>		FileOutputStream&nbsp;outfile;
&nbsp;<BR>		
&nbsp;<BR>		FileServerThread()
&nbsp;<BR>		{
&nbsp;<BR>			super(&quot;FileSocketServer&nbsp;Thread&quot;);
&nbsp;<BR>		}
&nbsp;<BR>		
&nbsp;<BR>		public&nbsp;void&nbsp;run()
&nbsp;<BR>		{
&nbsp;<BR>			button6.setEnabled(false);
&nbsp;<BR>			try
&nbsp;<BR>			{
&nbsp;<BR>				server1=new&nbsp;ServerSocket(2002);
&nbsp;<BR>			}
&nbsp;<BR>			catch(Exception&nbsp;r)
&nbsp;<BR>			{
&nbsp;<BR>				richEdit3.setText(&quot;input&nbsp;error&quot;+r);
&nbsp;<BR>			}
&nbsp;<BR>			
&nbsp;<BR>				
&nbsp;<BR>				try
&nbsp;<BR>				{
&nbsp;<BR>					client=server1.accept();
&nbsp;<BR>				}
&nbsp;<BR>				catch(Exception&nbsp;r)
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;richEdit3.setText(&quot;inputserver&nbsp;error&quot;+r);
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;<BR>			
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;in1=new&nbsp;DataInputStream(client.getInputStream());
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;									
&nbsp;<BR>					int&nbsp;len=in1.readInt();
&nbsp;<BR>					
&nbsp;<BR>					b=new&nbsp;byte[len];
&nbsp;<BR>					in1.read(b);
&nbsp;<BR>					
&nbsp;<BR>					String&nbsp;filename=in1.readLine();
&nbsp;<BR>					edit2.setText(filename);
&nbsp;<BR>					
&nbsp;<BR>					file=new&nbsp;File(&quot;C:\\My&nbsp;Documents\\Visual&nbsp;Studio&nbsp;Projects\\xianliao&quot;,filena&nbsp;<BR>me);
&nbsp;<BR>					outfile=new&nbsp;FileOutputStream(file);
&nbsp;<BR>					outfile.write(b);
&nbsp;<BR>					
&nbsp;<BR>					/*String&nbsp;str=new&nbsp;String(b);
&nbsp;<BR>					richEdit1.setText(str);*/
&nbsp;<BR>					
&nbsp;<BR>					
&nbsp;<BR>					outfile.close();
&nbsp;<BR>					in1.close();
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;client.close();
&nbsp;<BR>					
&nbsp;<BR>					button6.setEnabled(true);
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;catch(Exception&nbsp;r)
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;<BR>				&nbsp;&nbsp;&nbsp;&nbsp;richEdit3.setText(&quot;input&nbsp;error&quot;+r);
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;<BR>			
&nbsp;<BR>			try
&nbsp;<BR>			{
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;server1.close();
&nbsp;<BR>			}
&nbsp;<BR>			catch(Exception&nbsp;r)
&nbsp;<BR>			{
&nbsp;<BR>			&nbsp;&nbsp;&nbsp;&nbsp;richEdit3.setText(&quot;server&nbsp;close&nbsp;error&quot;+r);
&nbsp;<BR>			}
&nbsp;<BR>		}
&nbsp;<BR>	}&nbsp;<BR>&nbsp;<BR>&nbsp;<BR>&nbsp;<BR>发信人:&nbsp;SerialNo&nbsp;(小抠**乖乖肥猪),&nbsp;信区:&nbsp;Java&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;<BR>我也遇到socks丢数据的现象&nbsp;<BR>也是后面的数据有时候会丢失&nbsp;<BR>但有时候会好&nbsp;<BR>最后也没解决:(&nbsp;<BR>&nbsp;<BR>&nbsp;<BR>&nbsp;<BR>发信人:&nbsp;Lover9&nbsp;(无情),&nbsp;信区:&nbsp;Java&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;我听人家说,socket的字节流模式只能保证字节的准确传输,&nbsp;<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;但不能保证写入socket的字节流能在一次读之后就能读出来,&nbsp;<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;因此如果你能知道写入socket的字节数之后,在读出的一方&nbsp;<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用简单的while控制直到读出的字节数与写入的数相同时才终止&nbsp;<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;读入字节流。&nbsp;<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果&nbsp;有兴趣,可以试一试。&nbsp;<BR>&nbsp;<BR>&nbsp;<BR>&nbsp;<BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -