首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

关于tcp套接字得到输入输出流的有关问题

2012-02-10 
关于tcp套接字得到输入输出流的问题刚学到java网络编程,想用tcp做一个类似ftp文件传输的东西。结果发现建立

关于tcp套接字得到输入输出流的问题
刚学到java网络编程,想用tcp做一个类似ftp文件传输的东西。结果发现建立了tcp连接却只能传输一次。再次传输的时候提示 Socket is closed。让我很纠结~~查了半天百度,貌似是socket的getInputStream()得到的流被关闭了导致的~~各位大大有什么解决办法~~
代码如下:
server端

Java code
import java.io.*;import java.net.*;public class Server implements Runnable{    ServerSocket ser = null;    int port = 6666;    BufferedReader br = null;    BufferedWriter bw = null;    Socket soc = null;    File file_send = null;    File file_receive = null;    InputStream ips = null;    OutputStream ops = null;    public void StartServer() {        try {            ser = new ServerSocket(port);            while (true) {                soc = ser.accept();                System.out.println("链接已建立!");                //Thread thread = new Thread(this);                //thread.start();            }        } catch (IOException e) {            System.out.println("ServerSocket创建失败!检查端口是否被占用");            e.printStackTrace();        }    }        ////////////////接收////////////////    public void receive() {        try {            ips = soc.getInputStream();            file_receive = new File("D:/hello.java");            br = new BufferedReader(new InputStreamReader(ips));            bw = new BufferedWriter(new FileWriter(file_receive));            String s = null;            while ((s = br.readLine()) != null) {                bw.write(s);                bw.newLine();            }            bw.flush();            br.close();            bw.close();        } catch (IOException e) {            e.printStackTrace();        }        System.out.println("文件接收已完成!");    }/////////////////////发送/////////////////////////    public void send(){                try {            ops = soc.getOutputStream();            file_send = new File("D:/dos命令参数.txt");            bw = new BufferedWriter(new OutputStreamWriter(ops));            br = new BufferedReader(new FileReader(file_send));            String s = null;            while((s = br.readLine()) != null){                bw.write(s,0,s.length());                bw.newLine();            }            System.out.println("文件发送已完成!");            bw.flush();            br.close();            bw.close();        } catch (IOException e) {            e.printStackTrace();        }    }    @Override    public void run() {        send();        receive();            }        }

client端
Java code
package com.yao;import java.io.*;import java.net.*;public class Client {    public int port = 6666;    public String host = "127.0.0.1";    Socket soc = null;    File file_receive = null;    File file_send = null;    BufferedReader br = null;    BufferedWriter bw = null;    OutputStream ops = null;    InputStream ips = null;    /////////////建立连接/////////////    public void Connect(){        try {            soc = new Socket(host,port);        } catch (UnknownHostException e) {            System.out.println("无法在主机号:"+host+"创建监听!");            e.printStackTrace();        } catch (IOException e) {            System.out.println("无法在端口"+port+"添加监听");            e.printStackTrace();        }    }        ////////////////发送/////////////////////////        public void send(){        try {            ops = soc.getOutputStream();            file_send = new File("D:/Hello.txt");            br = new BufferedReader(new FileReader(file_send));            bw = new BufferedWriter(new OutputStreamWriter(ops));            String s = null;            while((s = br.readLine()) != null){                bw.write(s,0,s.length());                bw.newLine();            }            System.out.println("文件发送已完成!");            bw.flush();            br.close();            bw.close();        } catch (IOException e) {            e.printStackTrace();        }    }        /////////////////////接收/////////////////////        public void receive() {            try {                ips = soc.getInputStream();                file_receive = new File("D:/dos命令参数.java");                br = new BufferedReader(new InputStreamReader(ips));                bw = new BufferedWriter(new FileWriter(file_receive));                String s = null;                while ((s = br.readLine()) != null) {                    bw.write(s);                    bw.newLine();                }                bw.flush();                br.close();                bw.close();            } catch (IOException e) {                e.printStackTrace();            }            System.out.println("文件接收已完成!");        }    } 



[解决办法]
因为你在发送文件的时候,直接把bw给关闭了,这样导致socket关闭,然后在接收的时候就会出现问题,而且你的server和client都不严谨,server程序很明显在并发的时候会出错,线程不能这样些,顺便附上我简单修改过的,表达能力不行,希望你能理解

Server
Java code
package server;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class Server {        private int port;    Server(int port) {        this.port = port;    }        public void StartServer() {        try {            ServerSocket server = new ServerSocket(port);            while (true) {                try{                    new Thread(new ServerProcess(server.accept())).start();                }catch(Exception ex){                    ex.printStackTrace();                }            }        } catch (IOException e) {            System.out.println("ServerSocket创建失败!检查端口"+port+"是否被占用");            e.printStackTrace();        }    }}class ServerProcess implements Runnable{        private static final String SEND_PATH = "D:/dos命令参数.txt";       //发送文件路径    private static final String RECV_PATH = "D:/hello.java";        //接收文件路径        private InputStream in;    private OutputStream out;        ServerProcess(Socket socket) {        try {            in = socket.getInputStream();            out = socket.getOutputStream();        } catch (IOException e) {            e.printStackTrace();            throw new RuntimeException("socket 错误!");        }    }    ////////////////接收////////////////    private void receive() {        FileOutputStream fileOut = null;        try {            File file_receive = new File(RECV_PATH);            fileOut = new FileOutputStream(file_receive);            pipe(fileOut,in);        } catch (IOException e) {            e.printStackTrace();        }finally{            if(null != fileOut){                try {                    fileOut.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        System.out.println("文件接收已完成!");    }        private void pipe(OutputStream out,InputStream in) throws IOException {        byte[] buf = new byte[512];        int len = 0;        while((len = in.read(buf)) != -1) {            out.write(buf,0,len);        }    }/////////////////////发送/////////////////////////    private void send(){        FileInputStream fileIn = null;        try {            File file_send = new File(SEND_PATH);            fileIn = new FileInputStream(file_send);            pipe(out,fileIn);            System.out.println("文件发送已完成!");        } catch (IOException e) {            e.printStackTrace();        }finally{            if(null != fileIn) {                try {                    fileIn.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    @Override    public void run() {        try{            send();            receive();        }finally{            try {                in.close();            } catch (IOException e) {                e.printStackTrace();            }finally{                try {                    out.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}
[解决办法]
探讨
额,上一个问题二了~~~~二楼的代码没有关闭过 in和 out,只在最后文件传输完毕后关闭了这两个流。如果想在in和out上再包上其它的高层流该,比如说BufferedReader,BufferedWriter啥的,怎么处理?也不关闭么?会不会存在什么问题?

热点排行