黑马程序员技术交流社区

标题: TCP问题,帮忙看看这个代码 [打印本页]

作者: 谭荣强    时间: 2014-5-7 16:32
标题: TCP问题,帮忙看看这个代码
服务端上传图片到客户端。图片可以上传,服务端返回的信息,客户端收不到,还报异常。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.ServerSocket;
  class lianxi1 {

        public static void main(String[] args) throws UnknownHostException, IOException {
                 Socket s = new Socket("192.168.0.145",7553);
               
                 FileInputStream fis =new FileInputStream("d:\\1.jpg");
                 OutputStream os = s.getOutputStream();

                 byte[] buf = new byte[1024];
                 int len = -1;
                 while((len=fis.read(buf))!=-1){
                         os.write(buf, 0, len);
                 }
                 s.shutdownOutput();
                 
                 InputStream is = s.getInputStream();
               
                 byte[] buf1=new byte[1024];
                 BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
                 int len1 = bis.read(buf1);
                 System.out.println(new String(buf1,0,len1));                 
                 
                 fis.close();
                 s.close();                 
        }
}

  class lianxi2 {         
        public static void main(String[] args) throws IOException {
         
                ServerSocket ss = new ServerSocket(7553);
                Socket s  = ss.accept();                       
                        BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
                        BufferedOutputStream bos2 = new BufferedOutputStream(new FileOutputStream("d:\\2.jpg"));
                        byte[] buf = new byte[1024];
                        int len = -1;
                        while((len =bis.read(buf))!=-1){
                                bos2.write(buf, 0, len);
                        }
                        BufferedOutputStream bos1 = new BufferedOutputStream(s.getOutputStream());
                        bos1.write("上传成功".getBytes());
                        System.out.println("haha");
                        bos2.close();
                        s.close();         
        }
}




作者: NewDemo    时间: 2014-5-7 17:27
本帖最后由 NewDemo 于 2014-5-7 17:29 编辑

{:2_31:}拿着楼主的代码在eclipse中完美运行。因为楼主是把两个类写在了一起,我不清楚要怎么才能一下把两个主函数都运行了,所以我给剪切到了两个java文件中分开运行,另外ip地址换成了本机ip  127.0.0.1。
先打开服务端的程序,然后再打开客户端的



作者: heima_xyu    时间: 2014-5-7 17:32
楼主太不细心了,看下你的代码
BufferedOutputStream bos1 = new BufferedOutputStream(s.getOutputStream());
                        bos1.write("上传成功".getBytes());
                        System.out.println("haha");
                        bos2.close();
                        s.close();  

可以看出bos1这个流,没有flush()也没有close()


加上bos1.close()
客户端就能收到“上传成功”
作者: 谭荣强    时间: 2014-5-7 20:56
heima_xyu 发表于 2014-5-7 17:32
楼主太不细心了,看下你的代码
BufferedOutputStream bos1 = new BufferedOutputStream(s.getOutputStream ...

感谢!!
作者: 谭荣强    时间: 2014-5-7 20:59
NewDemo 发表于 2014-5-7 17:27
拿着楼主的代码在eclipse中完美运行。因为楼主是把两个类写在了一起,我不清楚要怎么才能一下把两 ...

知道哪错了,顺便问下用myeclipse开客户端和服务端只有一个窗口,老报错,怎么办。用命令行就没事。
作者: NewDemo    时间: 2014-5-7 22:02
谭荣强 发表于 2014-5-7 20:59
知道哪错了,顺便问下用myeclipse开客户端和服务端只有一个窗口,老报错,怎么办。用命令行就没事。 ...

在命令行里客户端和服务端是独立运行的,所以在eclipse中只要让客户端和服务端的程序都有自己的主函数就可以就可以了。
因为tcp协议需要三次握手,所以不论在命令行还是eclipse中都需要先运行服务端,后运行客户端。
对于楼主的问题我有个疑惑,你的程序并没有错误,在我这里可以完美运行,确实是没有错误的
作者: 谭荣强    时间: 2014-5-7 22:20
NewDemo 发表于 2014-5-7 22:02
在命令行里客户端和服务端是独立运行的,所以在eclipse中只要让客户端和服务端的程序都有自己的主函数就 ...

我在看看,eclipse 还不大会使用。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2