日期:2014-05-20  浏览次数:20913 次

关于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("文件接收已完成!");
        }
    
}