看板 java 關於我們 聯絡資訊
請教版上各位高手 我在寫TCPClient的時候發現了一個很奇怪的問題 再送訊息的時候 明明是同樣的程式碼(我將傳送訊息寫成一個method) 可是在不同的區域底下 一個Server端收的到訊息 另一個從外部呼叫的 Server卻收不到 這是什麼問題呢??? 有請版上各位幫我解惑了 我抓了這個蟲抓了快一天..... 程式碼如下 public Boolean connectServer( String ServerIp )throws Exception{ System.out.println("Connecting To Server... "); InetAddress IP = InetAddress.getByName(ServerIp); Client = new Socket ( IP , Port ); if(!Client.isConnected()){ assert Client.isConnected()==false; System.out.println("Connection failed"); } //會成功送到Server端的片段 try{ sendToServer("Message"); } catch(Exception e){ } return Client.isConnected(); } //Send message to TCP Server private void sendToServer(String Message){ //Assertion for TCP connection if(!Client.isConnected()){ assert Client.isConnected(); System.out.println("Connection Failed"); }else{ System.out.println("Connection work"); } try{ DataOutputStream outToServer = new DataOutputStream(Client.getOutputStream()); outToServer.writeBytes(Message+ "Client 1 \n"); System.out.println("Sending '"+Message+"'"); }catch(IOException e){ } } . . . . . . public static void main(String args[]) { try{ TCPClient test = new TCPClient(); test.connectServer("localhost") //這行Server端卻收不到 test.inputMoves("Fail"); }catch(Exception e){ System.out.println(e); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.217.47 ※ 編輯: a39189 來自: 140.115.217.47 (04/22 21:33)
james732:output stream 加個 flush() 之類的試試? 04/22 21:41
james732:在程式結束的時候,該呼叫 close() 的都要呼叫 04/22 21:42
cyclone350:這問題...我也有耶,我只知道問題在哪,怎麼解決,但是 05/06 19:50