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,問題仍然存在。
相關文章:
1. node.js - mysql如何通過knex查詢今天和七天內的匯總數據2. shell - Update query wrong in MySQL3. javascript - 用jsonp抓取qq音樂總是說回調函數沒有定義4. mysql 插入數值到特定的列一直失敗5. mysql 怎么做到update只更新一行數據?6. javascript - 新浪微博網頁版的字數限制是怎么做的7. 怎么在網頁中設置圖片進行左右滑動8. 360瀏覽器與IE瀏覽器有何區別???9. sublime可以用其他編譯器替換嗎?10. python - 在使用Pycharm時經常看到如下的樣式,小括號里紅色的部分是什么意思呢?
