2013-02-25 60 views
-3

我做了一個簡單的應用程序連接在Java 客戶端的服務器實在不行插座寫不響應

我使用新的Socket(「localhost」的,4444);爲客戶端 和新的ServerSocket(4444);服務器

客戶: http://www.hastebin.com/siqamonebu.coffee

服務器: http://www.hastebin.com/rivebetani.avrasm

我貼在hastebin的代碼,因爲其大,張貼在這裏 我不知道壞了哪一部分,但服務器永遠不會收到你說的消息

+3

'我貼在hastebin的代碼,因爲它能夠big'。請閱讀http://sscce.org – SJuan76 2013-02-25 14:12:08

+0

你真的認爲我會讀嗎? :P – user2107534 2013-02-25 14:17:05

+1

@ user2107534你會更好。因爲這裏的大多數人都會拒絕回答那些不能承受「你曾經試過」和「sscce」測試的問題。 – Cubic 2013-02-25 14:19:09

回答

2

的ChatServer - 廣播到所有連接的客戶端

在一個命令提示符:java ChartServer
在另一:java ChatClient localhost(或服務器正在運行,其中的IP地址)
而另:java ChatClient localhost(或服務器運行的IP地址)

開始在客戶端聊天OWS。

服務器這樣的...

// xagyg wrote this, but you can copy it 
import java.io.*; 
import java.net.*; 
import java.util.*; 

public class ChatServer { 

    public static List list = new ArrayList(); 

    public static void main(String[] args) throws Exception { 

     ServerSocket svr = new ServerSocket(4444); 

     System.out.println("Chat Server started!"); 

     while (true) { 
      try { 
       Socket s = svr.accept(); 
       synchronized(list) { 
        list.add(s);    
       }         
       new Handler(s, list).start(); 
      } 
      catch (IOException e) { 
       // print out the error, but continue! 
       System.err.println(e); 
      } 
     } 
    } 
} 

class Handler extends Thread { 

    private Socket s; 
    private String ipaddress; 
    private List list; 

    Handler (Socket s, List list) throws Exception { 
     this.s = s; 
     ipaddress = s.getInetAddress().toString(); 
     this.list = list; 
    } 

    public void run() { 

     try { 

     BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream())); 
     String message; 
     //MyDialog x = (MyDialog)map.get(ipaddress.substring(1)); 
     while ((message = reader.readLine()) != null) { 
      if (message.equals("quit")) { 
       synchronized(list) { 
        list.remove(s); 
       } 
       break; 
      } 
      synchronized(list) { 
       for (Object object: list) { 
        Socket socket = (Socket)object; 
        if (socket==s) continue; 
        PrintWriter writer = new PrintWriter(socket.getOutputStream()); 
        writer.println(ipaddress + ": " + message); 
        writer.flush(); 
       } 
      } 
     } 
     try { reader.close(); } catch (Exception e) {} 
     } 
     catch (Exception e) { 
     System.err.println(e); 
     } 
    } 
} 

客戶端這樣的...

// xagyg wrote this, but you can copy it 
import java.util.*; 
import java.io.*; 
import java.net.*; 

public class ChatClient { 


    public static void main(String[] args) throws Exception { 

     Socket s = new Socket(args[0], 4444); 
     BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
     PrintWriter out = new PrintWriter(s.getOutputStream()); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
     String message; 
     new SocketReader(in).start(); 
     while ((message = reader.readLine())!=null) { 
      out.println(message); 
      out.flush(); 
      if (message.equals("quit")) break; 
     } 
     in.close(); 
     out.close(); 
    }   
} 

class SocketReader extends Thread { 

    BufferedReader in; 

    public SocketReader(BufferedReader in) { 
     this.in = in; 
    } 

    public void run() { 
     String message; 
     try { 
      while ((message = in.readLine())!=null) { 
       System.out.println(message); 
      } 
     } 
     catch (Exception e) { 
      throw new RuntimeException(e); 
     }   
    } 
} 
0

我認爲這是因爲你如何啓動你的服務器,嘗試使用這個結構,因爲你正在創建運行方法內的ServerSocket,這可能會導致衝突:

public class ThreadSocketServer implements Runnable { 

    private int port; 
    private boolean isRunning; 
    private ServerSocket ss; 

    public ThreadSocketServer(int port, boolean initialStatus) { 
     this.port = port; 
     isRunning = initialStatus; 
     System.out.println("Start the server!"); 
     try { 
      ss = new ServerSocket(port); 
      Thread threadServer = new Thread(this); 
      threadServer.start(); 
     } catch (IOException e) { 
      System.err.println(e.getClass() + " - " + e.getMessage()); 
     } 
    } 

    @Override 
    public void run() { 

     while (isRunning) { 
      try { 
       Socket client = ss.accept(); 

       ObjectInputStream ois = new ObjectInputStream(
         client.getInputStream()); 
       //Process here 
       ois.close(); 
      } catch (IOException e) { 
       System.err.println(e.getClass() + " - " + e.getMessage()); 
       isRunning = false; 
      } catch (ClassNotFoundException e) { 
       System.err.println(e.getClass() + " - " + e.getMessage()); 
       isRunning = false; 
      } 
     } 

    } 

    public static void main(String[] args) { 
     new ThreadSocketServer(1085, true); 
    } 
+0

沒有修復它:/ – user2107534 2013-02-25 14:35:18

+0

mmm ...你試過複製和粘貼我的代碼來測試它嗎?然後,也許你可以調整你的代碼,並替換你的邏輯,而不是我的try塊中的代碼:Socket client = ...直到ois.close()。 – 2013-02-26 13:43:50