Re: [問題] InputStreamReader

看板java作者 (真呆)時間18年前 (2008/02/04 16:30), 編輯推噓1(103)
留言4則, 1人參與, 最新討論串5/6 (看更多)
印不出來的意思是說空白拉... 有跑到while裡面的迴圈,但System.out不出東西 我把我簡單的client和server貼出來好了 ====================================================== 客戶端的程式碼: import java.io.*; import java.net.*; import java.util.Scanner; public class TestClient { public void runClient(){ Socket client; try{ client=new Socket(InetAddress.getLocalHost(),9000); String msg; OutputStreamWriter output=new OutputStreamWriter(client.getOutputStream()); BufferedWriter bw=new BufferedWriter(output); do{ System.out.print("Please write what u want to send:"); Scanner scanner = new Scanner(System.in); msg=scanner.next(); bw.write(msg); bw.flush(); System.out.println(msg+" sent!"); }while(!msg.equals("quit")); output.close(); client.close(); } catch(Exception e){ System.out.println(e.getMessage()); } } public static void main(String[] args){ TestClient client=new TestClient(); client.runClient(); } } ================================================================== server端的程式碼: import java.io.*; import java.net.*; public class TestServer { public void runSerer(){ ServerSocket server; Socket connection; try{ server=new ServerSocket(9000,100); while(true){ connection=server.accept(); System.out.println(connection.getInetAddress().getHostName()); InputStreamReader in=new InputStreamReader(connection.getInputStream()); int ch=0; StringBuffer msg=new StringBuffer(); while((ch=in.read())!=-1){ msg.append((char)ch); } System.out.print(msg); in.close(); connection.close(); server.close(); System.out.println("Connection closed"); } } catch(Exception e){ } } public static void main(String[] args){ TestServer server=new TestServer(); server.runSerer(); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.87.151.40

02/04 16:30, , 1F
我猜根本就沒跑到最後System.out.println那段 你查查看吧
02/04 16:30, 1F

02/04 16:31, , 2F
基本上你的這些溝通 都是片段的 不會有跑到結束的時候
02/04 16:31, 2F

02/04 16:32, , 3F
除非connection close, 我以前在寫bbs terminal cleint端也
02/04 16:32, 3F

02/04 16:33, , 4F
有碰過一樣的問題,你還是查查看吧 冏
02/04 16:33, 4F
文章代碼(AID): #17fiqOXy (java)
文章代碼(AID): #17fiqOXy (java)