保持Java套接字打开?

时间:2012-12-05 16:35:59

标签: java sockets input io output

我正在制作一个会自动更新的节目/游戏。我有更新部分,但没有检查版本。我原本以为这很容易。继承人我所做的一切。我为游戏写了一个更新程序,我写了一个服务器。每次客户端/更新程序连接时,服务器都会启动一个线程。线程处理一切。游戏更新程序读取名为version.txt的文件,并提供版本号(默认为0.0.1)并将其发送到服务器。服务器确实收到版本,如果版本匹配,将System.out.println();,如果我更改版本,它将更改输出。这部分是有效的。但就目前而言。该过程的第二部分是服务器然后只发送一个名为NPS Game.txt的文本文件(它发送任何内容,但txt很容易测试),客户端用新的版本替换该文件的旧版本刚发送。问题是我一直收到一个错误,说Socket已关闭。我尝试过使用socket.setKeepAlive(true);,但没有改变任何东西(我把它放在客户端和服务器上)。这是代码:

服务器:

package main;

import java.io.*;
import java.net.*;

import javax.swing.JOptionPane;

public class Server {
static ServerSocket serverSocket = null;
static Socket clientSocket = null;
static boolean listening = true;

public static void main(String[] args) throws IOException {
    try {
        serverSocket = new ServerSocket(6987);
    } catch (IOException e) {
        ServerThread.showmsg("Could not use port: 6987");
        System.exit(-1);
    }

    ServerThread.showmsg("server- initialized");
    ServerThread.showmsg("server- waiting...");

    while (listening)
        new ServerThread(serverSocket.accept()).start();
}
}

服务器线程:

package main;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;

import javax.swing.JOptionPane;

public class ServerThread extends Thread {
Socket socket;
ObjectInputStream in;
ObjectOutputStream out;
String version = "0.0.1";

public ServerThread(Socket socket) {
    super("Server Thread");
    this.socket = socket;
}

public void run() {
    showmsg("server- Accepted connection : " + socket);
    getVersion();
    sendFile();
}

public void getVersion() {
    try {
        ObjectInputStream ois = new ObjectInputStream(
                socket.getInputStream());
        try {
            String s = (String) ois.readObject();
            if (s.equals(version)) {
                System.out.println("server- matched version :)");
            } else {
                System.out.println("server- didnt match version :(");
                System.exit(0);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        ois.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void sendFile() {
    // sendfile
    File myFile = new File("C:\\Programming\\NPS\\Files\\bin\\NPS Game.txt");
    byte[] mybytearray = new byte[(int) myFile.length()];
    FileInputStream fis;
    try {
        fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(mybytearray, 0, mybytearray.length);
        OutputStream os = socket.getOutputStream();
        showmsg("server- Sending...");
        os.write(mybytearray, 0, mybytearray.length);
        os.flush();
        socket.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void showmsg(String s) {
    JOptionPane.showMessageDialog(null, s);
}
}

和客户端/更新程序:

package main;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;

import org.omg.CORBA.portable.InputStream;

public class Connections {
String IP, port;
String message = "";
Socket socket;

public Connections(boolean server, boolean updating, String IP, String port) {
    this.IP = IP;
    this.port = port;
    try {
        socket = new Socket(IP, Integer.parseInt(port));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (!server) {
        if (updating) {
            try {
                sendVersion();
                updating();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            client();
        }
    }
    if (server) {

    }
}

public void sendVersion() throws IOException {

    FileReader fileReader = new FileReader(
            "C:\\Program Files\\AVTECH\\NPS\\Files\\bin\\version.txt");
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    String stringRead = bufferedReader.readLine();

    bufferedReader.close();

    ObjectOutputStream oos = new ObjectOutputStream(
            socket.getOutputStream());
    oos.writeObject(stringRead);
    oos.flush();
    oos.close();
}

public void updating() throws IOException {
    int filesize = 6022386; // filesize temporary hardcoded

    int bytesRead;
    int current = 0;

    showmsg("client- connected");

    // receive file
    byte[] byteArray = new byte[filesize];
    java.io.InputStream inStream = socket.getInputStream();
    FileOutputStream fileOutStream = new FileOutputStream(
            "C:\\Program Files\\AVTECH\\NPS\\Files\\bin\\NPS Game.txt");
    BufferedOutputStream buffOutStream = new BufferedOutputStream(
            fileOutStream);
    bytesRead = inStream.read(byteArray, 0, byteArray.length);
    current = bytesRead;

    do {
        bytesRead = inStream.read(byteArray, current,
                (byteArray.length - current));
        if (bytesRead >= 0)
            current += bytesRead;
    } while (bytesRead > -1);

    buffOutStream.write(byteArray, 0, current);
    buffOutStream.flush();
    buffOutStream.close();
    inStream.close();
    socket.close();
}

public static void showmsg(String s) {
    JOptionPane.showMessageDialog(null, s);
}
}

我不知道它有什么问题,但它真的很令人沮丧。如果有人可以提供帮助,我们将不胜感激。我做过的一些事情:谷歌各种问题,尝试实施socket.setKeepAlive(true);。另外,我认为可能值得注意的是,在服务器线程中,在BufferedInputStream bis = new BufferedInputStream(fis);行的正上方,我放了System.out.println(socket.isClosed);并返回true。这就是我的全部。提前谢谢!

1 个答案:

答案 0 :(得分:6)

我认为关闭其中一个流,关闭套接字。因此,请尝试在服务器端删除ois.close()方法中的getVersion()调用。也可以在客户端oos.close()方法中删除sendVersion()调用。

当您构造一个ObjectOutputStream或ObjectInputStream并完成它时,您不应该关闭该流,因为它将关闭基础流,在您的情况下是套接字。