1
我試圖使一個簡單的Java TCP客戶機和主機聊天程序。他們都在簡單的GUI中運行。在我啓動服務器之後,它會等待來自客戶端的連接,然後在其JtextArea中輸出一條消息。服務器執行此操作成功地,一切都運行的罰款,直到這裏,現在我發送一些數據,它應該打印在其JTextArea中的客戶端,但客戶端已連接發生的時間卡,連接按鈕保持按下它只是凍結。java的插座問題
這裏是客戶端連接按鈕的代碼,我想我在這裏做得不對
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
serverName = t3.getText();
serverPort = Integer.parseInt(t4.getText());
try {
tb.append("\nconnecting...>\n"+temp);
connect2Server();
temp = in.readLine();
tb.append(temp);
} catch (IOException ex) {
tb.append(ex.toString());
Logger.getLogger(DesktopApplication1View.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void connect2Server() throws IOException{
try{
skt = new Socket(serverName,serverPort);
in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
}
catch(UnknownHostException uh){
tb.append(uh.toString());
}
catch(IOException io){
tb.append(io.toString());
}
}
//vars
BufferedReader in;
Socket skt;