2013-02-28 4 views
0

Я пытаюсь создать программу сокетов для синхронизации файлов с двух ПК вместе. Поэтому, в основном, я сначала пытаюсь синхронизировать один путь. Так что происходит ..Ошибка с несколькими объектами ObjectOutputStream и ObjectInputStream (Socket is closed)

  1. Сервер и клиент соединяются вместе через разъем. (ok)
  2. Сервер отправляет список файлов, которые у него есть через objectoutputstream, и клиент получает его через objectinputstream. (ok)
  3. Клиент сравнивает список файлов и отправляет на сервер обратно (через objectoutputstream) файлы, которых у него нет. (ok)
  4. Сервер отправляет недостающие файлы клиенту. (Проблема здесь)

Моя ошибка на стороне сервера, он показывает:

java.net.SocketException: Socket is closed 
at java.net.Socket.getOutputStream(Unknown Source) 
at ee4210.TcpServer$ConnectionRequestHandler.run(TcpServer.java:116) 
at ee4210.TcpServer.handleClientRequest(TcpServer.java:80) 
at ee4210.TcpServer.startServer(TcpServer.java:72) 
at ee4210.TcpServer.main(TcpServer.java:39) 

Я считаю, что это что-то с этим делать ObjectOutputStream и ObjectInputStream, потому что, если я не закрыть ObjectOutputStream или ObjectInputStream, он может работать.

Может ли клиент иметь объект objectoutputstream и objectinputstream для отправки/получения объектов? Точно так же может быть и сервер?

Цените свою помощь. Извините, за длинную статью. Мне нравится быть как можно более детально.

Мой код клиента:

package ee4210; 

import java.io.* ; 
import java.net.* ; 
import java.util.* ;public class TcpClient{ 
static ObjectOutputStream out; 
static ObjectInputStream in; 
String message; 
int portNo=2004; 
static Socket _socket = null; 
InetAddress host = null; 
static LinkedList destinationfiles = new <String>LinkedList(); 
static LinkedList sourcefiles = new <String>LinkedList(); 
static LinkedList missingfiles = new <String>LinkedList(); 
String filename=null; 

TcpClient(){} 

public static void listFilesForFolder(final File folder) { //no need to read this 
    for (final File fileEntry : folder.listFiles()) { 
     if (fileEntry.isDirectory()) { 
      listFilesForFolder(fileEntry); 
     } else { 
      //System.out.println(fileEntry.getName()); 
      destinationfiles.add(fileEntry.getName()); 
     } 
    } 
} 

public static void getListfromPeer() { 
    try { 
     in=new ObjectInputStream(_socket.getInputStream()); 
     sourcefiles = (LinkedList) in.readObject(); 
     //in.close(); //if i close, got error 
    } catch (ClassNotFoundException | IOException classNot) { 
     System.err.println("data received in unknown format"); 
    } 

} 

public static void compareList() { 
    // go through each of the peer's filename, check against urs. If dont 
    // have, download 
    ListIterator iter = sourcefiles.listIterator(); 
    String filename; 
    while (iter.hasNext()) { 
     // System.out.println(iter.next()); 
     filename=(String) iter.next(); 
     if (!destinationfiles.contains(filename)) //file cannot be found 
     { 
      missingfiles.add(filename); 
     } 
    } 

} 

public static void main(String[] args) { 
    /*Check if directory exist, create one if doesnt exit*/ 
    File theDir = new File("src/ee4210/files"); 

    // if the directory does not exist, create it 
    if (!theDir.exists()) { 
     System.out.println("creating directory.."); 
     boolean result = theDir.mkdir(); 
     if (result) { 
      System.out.println("Successfully created directory"); 
     } 

    } 
    /*Get the list of files in that directory and store the names in array*/ 
    listFilesForFolder(theDir); 

    /*Connect to peer*/ 
    try { 
     new TcpClient().connect(); 
     //new TcpClient().checkForInput(); 

    } catch (Exception e) { 
     System.out.println("Something falied: " + e.getMessage()); 
     e.printStackTrace(); 
    } 

} 

public void connect() throws IOException 
{ 

    try { 
     host = InetAddress.getLocalHost(); 
     //_socket = new Socket(host.getHostName(), portNo); 
     _socket = new Socket("172.28.177.125", portNo); 
     //System.out.println(host.getHostAddress()); //get the IP address of client 

     /*Get list from peer and compare list*/ 
     getListfromPeer(); //receive from Server 
     compareList(); 
     System.out.println(missingfiles); 

     /*Send to server the list of missing files*/ 

     do 
     { 
      //busy wait till socket is open 
     }while (_socket.isClosed()); 
     out = new ObjectOutputStream(_socket.getOutputStream()); 
     out.flush(); 
     out.writeObject(missingfiles); 
     out.flush(); 
     System.out.println("Client have finished sending missing linkedList over"); 
     out.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

}

сервера Код:

package ee4210; 

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

public class TcpServer extends Thread 
{ 
ServerSocket providerSocket; 
Socket connection = null; 
static ObjectOutputStream out; 
ObjectInputStream in; 
String message; 
int portNo=2004; //portNo is 2004 
TcpClient tcpclientobject = new TcpClient(); 
static LinkedList destinationfiles = new <String>LinkedList(); 
static LinkedList missingfiles = new <String>LinkedList(); 

public static void main(String[] args) 
{ 
    /*Check if directory exist, create one if doesnt exit*/ 
    File theDir = new File("src/ee4210/files"); 

    // if the directory does not exist, create it 
    if (!theDir.exists()) { 
     System.out.println("creating directory.."); 
     boolean result = theDir.mkdir(); 
     if (result) { 
      System.out.println("Successfully created directory"); 
     } 

    } 
    /*Get the list of files in that directory and store the names in array*/ 
    listFilesForFolder(theDir); 

    /*Connect*/ 
    try { 
     new TcpServer().startServer(); 
    } catch (Exception e) { 
     System.out.println("I/O failure: " + e.getMessage()); 
     e.printStackTrace(); 
    } 

    /*Rest of the code in run()*/ 


} 

public static void listFilesForFolder(final File folder) { 
    for (final File fileEntry : folder.listFiles()) { 
     if (fileEntry.isDirectory()) { 
      listFilesForFolder(fileEntry); 
     } else { 
      //System.out.println(fileEntry.getName()); 
      destinationfiles.add(fileEntry.getName()); 
     } 
    } 
} 
public void startServer() throws Exception { 
    ServerSocket serverSocket = null; 
    boolean listening = true; 

    try { 
     serverSocket = new ServerSocket(portNo); 
    } catch (IOException e) { 
     System.err.println("Could not listen on port: " + portNo); 
     System.exit(-1); 
    } 

    while (listening) { 
     handleClientRequest(serverSocket); 
    } 

    serverSocket.close(); 
} 

private void handleClientRequest(ServerSocket serverSocket) { 
    try { 
     new ConnectionRequestHandler(serverSocket.accept()).run(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 


public class ConnectionRequestHandler implements Runnable{ 
    private Socket _socket = null; 

    public ConnectionRequestHandler(Socket socket) { 
     _socket = socket; 
    } 
    public void run() { 
     System.out.println("Client connected to socket: " + _socket.toString()); 

     /*Send the linkedlist over*/ 
     try { 
      out = new ObjectOutputStream(_socket.getOutputStream()); 
      //out.flush(); 
      out.writeObject(destinationfiles); 
      out.flush(); 
      //out.close(); //if I close, will have errors 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     receiveMissingFileNames(); //this one is still ok 

     try { //if i put the close here its ok 
      out.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      out = new ObjectOutputStream(_socket.getOutputStream()); //when I add this line it shows the error 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    public void receiveMissingFileNames() 
    { 
     try { 

      in=new ObjectInputStream(_socket.getInputStream()); 
      System.out.println("Is the socket connected="+_socket.isConnected()); 
      missingfiles=(LinkedList) in.readObject(); 
      System.out.println(missingfiles); 
      in.close(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

}

ответ

2

java.net.SocketException: Гнездо закрыто

Это исключение означает, что вы закрыли розетку и затем пытались ее использовать.

Закрытие входного потока или выходного потока сокета закрывает другой поток и сокет.

+0

Таким образом, это означает, что когда я закрываю поток ввода/вывода, как мне его открыть? – Ferrino

+1

OMG работает! Спасибо за советы. После проверки с помощью другого аналогичного потока (http://stackoverflow.com/questions/13727959/keeping-a-java-socket-open), я просто создаю поток ввода-вывода и НИКОГДА НИКОГДА НЕ ЗАКРЫВАЙТЕ ЭТО НАЗАД. – Ferrino

Смежные вопросы