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