成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術文章
文章詳情頁

Java套接字。服務器-客戶端通信

瀏覽:106日期:2024-05-04 10:45:11
如何解決Java套接字。服務器-客戶端通信?

您正在使用readLine()哪個期望換行符。n在您發送的郵件中添加或不使用readLine()。

解決方法

我試圖用沒有gui的服務器連接帶有gui的客戶端。連接已完成,但我看不到這兩個應用程序之間的任何消息。(我應該在客戶端中找到SERVERHERE,在服務器中找到CLIENT HERE)

客戶端連接代碼:

@Overridepublic void ClientRunning(){ try { connectToServer();setStreams();ClientRun(); }catch(EOFException oefException){showMessage('n Client terminated the connectionn'); }catch(IOException ioException){ioException.printStackTrace(); }finally{close(); }}public void connectToServer() throws IOException{ showMessage('Attempting Connection... n'); connection = new Socket(InetAddress.getByName(serverIP),6789); showMessage('Connected to: '+ connection.getInetAddress().getHostName());}public void setStreams() throws IOException{ output = new PrintWriter(connection.getOutputStream(),true); output.flush(); input= new BufferedReader(new InputStreamReader(connection.getInputStream())); showMessage('n Streams are now set. n');}public void close(){ showMessage('n closing...'); try{ output.close(); input.close(); connection.close(); }catch(IOException ioException){ioException.printStackTrace(); }}public void showMessage(final String text){ SwingUtilities.invokeLater(new Runnable(){public void run(){ cwindow.append(text);} });}public void sendMessage(String message){ output.write('CLIENT - '+message); output.flush(); showMessage('nCLIENT - '+message);}private void ClientRun() throws IOException{ String message='CLIENT HERE!'; sendMessage(message); do{try{ message=input.readLine(); showMessage('n'+message);}catch(EOFException eofException){showMessage('n Server ended the connection!'); } }while(message!='EXIT'); }

(輸入和輸出在此客戶端類擴展到的GUI類中定義。定義為“受保護的BufferedReader輸入;受保護的PrintWriter輸出;”)

另外,服務器代碼:

public class ServerClass {private ServerSocket server;private Socket connection;private BufferedReader input;private BufferedWriter output;public void startServer(){ try{server=new ServerSocket(6789,100);while(true){ try{waitForConnection();setStreams();ServerRunning(); }catch(EOFException eofException){showMessage('n Server ended the connection!'); }finally{ close();}} }catch(IOException ioException){ioException.printStackTrace(); }}private void waitForConnection() throws IOException{ showMessage('Waiting for someone to connect... n'); connection=server.accept(); showMessage('Now connected to '+ connection.getInetAddress().getHostName());}private void setStreams() throws IOException{ output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); output.flush(); input= new BufferedReader(new InputStreamReader(connection.getInputStream())); showMessage('n Streams are now set. n');}public void ServerRunning() throws IOException{ String message='SERVER HERE!'; sendMessage(message); do{try{ message=input.readLine(); showMessage('n'+message);}catch(EOFException eofException){showMessage('n Server ended the connection!'); } }while(message!='EXIT'); }private void close(){ showMessage('n Closing connections... n'); try{output.close();input.close();connection.close();}catch(IOException ioException){ ioException.printStackTrace();}}private void showMessage(String text){ System.out.println(text);}private void sendMessage(String message){ try{ output.write('SERVER - '+message);output.flush();showMessage(message); }catch(IOException ioException){System.out.println('n ERROR!'); }}

連接似乎還可以,所以我不會弄錯。任何幫助,將不勝感激。

PS:我也嘗試在服務器中使用PrintWriter,并且還嘗試在流語句中嘗試try catch,問題仍然存在。

標簽: java
相關文章:
主站蜘蛛池模板: 成年日韩片av在线网站 | 97国产大学生情侣11在线视频 | 在线观看亚洲精品国产 | 国产成人亚洲精品久久 | 综合久久一区二区三区 | 亚洲视频观看 | 国产亚洲人成网站在线观看 | 成人看片黄a免费 | 人妖欧美一区二区三区四区 | 国产亚洲精品久久综合影院 | 美国一级做a一级视频 | 九草在线 | 久久精品国产线看观看亚洲 | 看5xxaaa免费毛片 | 精品在线观看免费 | 免费看片aⅴ免费大片 | 国产成人综合网亚洲欧美在线 | 国产特黄特色的大片观看免费视频 | 国产高清精品自在线看 | 精品免费久久久久久成人影院 | 男人天堂中文字幕 | 免费91最新地址永久入口 | 精品久久在线观看 | 美国一级毛片oo | 国产99久久久久久免费看 | 亚洲视频一 | 亚洲国产成人91精品 | 成人三级视频在线观看 | 在线播放人成午夜免费视频 | 欧美一级视频 | 日韩a一级欧美一级在线播放 | 国产福利社区 | 国产一二三区在线 | 88av视频在线 | 国产91精品一区二区视色 | 精品中文字幕一区在线 | 欧美无玛| 成人免费在线视频网 | 国产aⅴ一区二区 | 成人丝袜激情一区二区 | 五月桃花网婷婷亚洲综合 |