0

我試圖通過使用套接字發送一個對象,將通過ObjectOutputStream持有一些信息。當我打電話給方法Client_Handler(String auth, String user)併發送它的工作正常的對象,但是當我再次調用它不起作用。我希望能夠使用連接多次發送包含不同數據的對象。ObjectOutput/InputStream只發送和接收對象一次

客戶端:

public void Client_Handler(String auth, String user){ 
     try{ 
     socket2 = new Socket(serverAddress, 8080); 
     User_Authorization us3 = new User_Authorization(); 
     ObjectOutputStream out2 = new ObjectOutputStream(socket2.getOutputStream()); 
     us3.set_name(user); 
     us3.set_authorization(auth); 
     out2.writeUnshared(us3); 
     }catch(Exception e){ 
      JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE); 
     } 
} 

服務器:

public class Conn implements Runnable{ 
private Socket socket; 
public static ServerSocket server ; 

public void go(){ 
    Thread r = new Thread(this); 
     try{ 
       r.start(); 
     }catch(Exception e){ 
      JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE); 
     } 
} 

@Override 
public void run() { 
    try{ 
    server = new ServerSocket(8080); 
    socket = server.accept(); 
    while(true){ 
     Login.User_Authorization us = null;    
     ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); 
     us = (Login.User_Authorization) in.readObject(); 
    System.out.println(us.get_name()+ "he's " +us.get_authorization()); 
    } 
    }catch(Exception e){JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);} 
} 
+1

定義「不工作」。 –

+0

當再次執行時,沒有輸出顯示,如果它沒有得到任何輸入。有時它會給我一個EOFException,但不是每次都有,這有點奇怪。 –

+0

http://stackoverflow.com/a/7022301/1490962 –

回答

1

當我調用該方法的Client_Handler(字符串權威性,字符串用戶)和 發送正常工作的對象,但是當我打電話它再次沒有 工作。

因爲,每次你調用Client_Handler(String auth, String user)方法,你正在試圖建立與服務器通過socket2 = new Socket(serverAddress, 8080);被終止以前的連接的新的連接時間。這就是爲什麼你會得到EOFException例外。在調用Client_Handler方法之前,您應該創建一個socket2對象。在調用Client_Handler方法時,只需使用初始化的套接字socket2即可。

你的代碼可能的東西是這樣的,在客戶端:

Socket socket2; 
ObjectOutputStream out2 = null; 
public void mainMethod() 
{ 
    try 
    { 
     socket2 = new Socket(serverAddress, 8080); 
     out2 = new ObjectOutputStream(socket2.getOutputStream()); 
     boolean canSend = true; 
     while (canSend) 
     { 
      canSend = Client_Handler("authentication","user"); 
     } 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 
    finally 
    { 
     if (out2!=null) 
     { 
      try 
      { 
       out2.close(); 
      } 
      catch (Exception ex) 
      { 
       ex.printStackTrace(); 
      } 
     } 
    } 
} 
public boolean Client_Handler(String auth, String user) 
{ 
    try 
    { 
     User_Authorization us3 = new User_Authorization(); 
     us3.set_name(user); 
     us3.set_authorization(auth); 
     out2.writeUnshared(us3); 
    }catch(Exception e) 
    { 
     JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE); 
     return false; 
    } 
    return true; 
} 
+0

我在方法外創建'socket2'對象,但這次我當我再次調用它時,得到了'java.io.StreamCorruptedException:無效的類型代碼:AC'。 –

+0

順便說一句,我可以在同一個程序中使用兩個具有不同端口號的套接字嗎? –

+1

看我的更新..是的,你可以在同一個程序中使用兩個不同端口號的套接字。但你應該在兩個獨立的線程中調用對兩個套接字輸入/輸出流的讀/寫操作..因爲它們阻塞了操作並會阻塞線程... –