2013-04-23 120 views
2

我在使用Socket的java中有一個簡單的客戶/服務器聊天。問題是程序彼此連接,但由於某些原因,我無法從兩側接收/接收數據。當我斷開服務器時,客戶端給我一個錯誤「連接重置」,顯示它們已連接,但它們不交換數據。java中的Socket服務器

該代碼是從取自here的Java教程中提取的相同代碼。

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package javaapplication3; 

/** 
* 
* @author Amr 
*/ 
import java.net.*; 
import java.io.*; 

public class KnockKnockServer { 
    public static void main(String[] args) throws IOException { 

     ServerSocket serverSocket = null; 
     try { 
      serverSocket = new ServerSocket(4440); 
     } catch (IOException e) { 
      System.err.println("Could not listen on port: 4444."); 
      System.exit(1); 
     } 

     Socket clientSocket = null; 
     try { 
      clientSocket = serverSocket.accept(); 
      System.out.println(Inet4Address.getLocalHost()); 
     } catch (IOException e) { 
      System.err.println("Accept failed."); 
      System.exit(1); 
     } 

     PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); 
     BufferedReader in = new BufferedReader(
       new InputStreamReader(
       clientSocket.getInputStream())); 
     String inputLine, outputLine; 


     while ((inputLine = in.readLine()) != null) { 
      outputLine = "heelo"; 
      out.println(outputLine); 
      if (outputLine.equals("Bye.")) 
       break; 
     } 
     out.close(); 
     in.close(); 
     clientSocket.close(); 
     serverSocket.close(); 
    } 
} 

import java.io.*; 
import java.net.*; 

public class KnockKnockClient { 
    public static void main(String[] args) throws IOException { 

     Socket kkSocket = null; 
     PrintWriter out = null; 
     BufferedReader in = null; 

     try { 
      kkSocket = new Socket(Inet4Address.getLocalHost(), 4440); 
      out = new PrintWriter(kkSocket.getOutputStream(), true); 
      in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); 
     } catch (UnknownHostException e) { 
      System.err.println("Don't know about host: taranis."); 
      System.exit(1); 
     } catch (IOException e) { 
      System.err.println("Couldn't get I/O for the connection to: taranis."); 
      System.exit(1); 
     } 

     BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); 
     String fromServer; 
     String fromUser; 

     while ((fromServer = in.readLine()) != null) { 
      System.out.println("Server: " + fromServer); 
      if (fromServer.equals("Bye.")) 
       break; 

      fromUser = stdIn.readLine(); 
     if (fromUser != null) { 
       System.out.println("Client: " + fromUser); 
       out.println(fromUser); 
     } 
     } 

     out.close(); 
     in.close(); 
     stdIn.close(); 
     kkSocket.close(); 
    } 
} 

回答

1

您在客戶端和服務器上都有一個單獨的線程,它們使用readLine()方法從各自的BufferedReader中讀取哪些塊。

由於客戶端和服務器都希望在之前互相閱讀,他們中的任何一方都會發送任何內容,所以它們都會坐在那裏並阻塞。您也需要到餐桌,並且具有寫入PrintWriter一個線程,從BufferedReader讀取和一個,或你必須有:

outputLine = "heelo"; 
out.println(outputLine); 

while循環之外。

5

建立連接後,你的服務器做的第一件事是

while ((inputLine = in.readLine()) != null) 

也就是說,等待客戶說些什麼。

你的客戶做的第一件事是

while ((fromServer = in.readLine()) != null) 

也就是說,等待服務器說點什麼。

使其中一個先發送一些東西,它應該工作。

2

你從該教程中缺少的位塊:

// initiate conversation with client 
KnockKnockProtocol kkp = new KnockKnockProtocol(); 
outputLine = kkp.processInput(null); 
out.println(outputLine); 

目前,你是不是初始化會話,因此套接字讀取阻塞由其他海報指出。