2016-04-22 160 views
1

我尝试创建使用套接字的程序。 我想输入单词,然后发送给我返回单词输入。 第一次,它似乎工作正常,但第二次失败。Java套接字错误

这是我的源代码。

---------------------服务器--------------------

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class ServerTestClass {      
    public static void main(String[] args) { 
     ServerSocket welcome = null; 
     Socket Sock = null; 
     try{ 
      welcome = new ServerSocket(45678); 
     }catch(IOException e){ 
      System.err.println("error"); 
     } 

     while(true){ 
      try{ 
       Sock = welcome.accept(); 
       EchoThread echoThread = new EchoThread(Sock,45678); 
       echoThread.start(); 
      }catch(IOException e){ 
       System.err.println("error 1"); 
      } 
     } 
    } 
} 
class EchoThread extends Thread{ 
    Socket Sock ; 
    int port; 
    EchoThread(Socket Sock,int port){ 
     this.port = port; 
     this.Sock = Sock; 
    } 
    public void run(){ 
     OutputStreamWriter output = null; 
     BufferedReader input = null; 
     try{ 
      output = new OutputStreamWriter(Sock.getOutputStream()); 
      input = new BufferedReader(new InputStreamReader(Sock.getInputStream())); 
      while(true){ 
       String word = input.readLine(); 
       output.write(": "+word); 
       output.flush(); 
       output.close(); 
       input.close(); 
       Sock.close(); 
       break; 
      } 
     }catch(IOException e){ 
      System.err.println("error 2"); 
     } 
     finally{ 
      try{ 
       if(output != null){ 
        output.close(); 
       } 
       if(input != null){ 
        input.close(); 
       } 
       if(Sock != null){ 
        Sock.close(); 
       } 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

------------------客户---------------

package client; 
import java.net.*; 
import java.io.*; 
import java.util.*; 
public class Client 
{ 
    public static final int PORT = 45678; // default port 
    public static void main(String[] args) 
    { 
     Socket clientSocket = null; 
     int port = PORT; 
     String hostName = null; 

     try 
     { // create socket 
      clientSocket = new Socket("localhost", port); 
     } 
     catch (IOException e) 
     { 
      System.err.println("Error Creating Socket"); 
      System.exit(2); 
     } 
     catch (NumberFormatException e) { 
      System.out.println("Port number must be integer between 1024 and 65535"); 
      System.exit(3); 
     } 
     OutputStreamWriter output = null; 
     Scanner input = null; 
     try 
     { 
      output = new OutputStreamWriter(clientSocket.getOutputStream()); 
      input = new Scanner(clientSocket.getInputStream()); 
      Scanner inputData = new Scanner(System.in);  
      while(true) { 
       System.out.println("Enter your word: "); 
       String inputWord = inputData.nextLine(); 
       output.write(inputWord + "\r\n"); 
       output.flush(); 
       if (inputWord.equals("")) 
       { 
        break; 
       } 
       String outputWord = input.nextLine(); 
       System.out.println(outputWord); 
      } 
      input.close(); 
      output.close(); 
      clientSocket.close(); 
     } catch (IOException e) 
     { 
      System.err.println("Closing Socket connection"); 
     } 
     finally { 
      try { 
       if (input != null) 
        input.close(); 
       if (output != null) 
        output.close(); 
       if (clientSocket != null) 
        clientSocket.close(); 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

------ -----------------结果---------------------------

run: 
Enter your word: 
word 
: word 
Enter your word: 
word 
Exception in thread "main" java.util.NoSuchElementException: No line found 
    at java.util.Scanner.nextLine(Scanner.java:1540) 
    at client.Client.main(Client.java:49) 
Java Result: 1 
BUILD SUCCESSFUL (total time: 18 seconds) 
+0

你需要检查是否有下一行使用'hasNextLine()'。 –

+1

我没有读取所有的代码,但有一件事看起来不太好:你为什么要在你的'EchoThread'类的'run'方法的无限循环内关闭套接字(和其他流)?关闭它会阻止你与通过此套接字连接的客户端进一步通信。 – Pshemo

+0

他不能使用hasNextLine();他从控制台获取价值,因为控制台没有任何东西。用户甚至不会有机会为程序输入值 – Priyamal

回答

0
while(true) { 
    System.out.println("Enter your word: "); 
    String inputWord = ""; 

    inputword = inputData.nextLine(); 
    output.write(inputWord + "\r\n"); 
    output.flush(); 


    if (inputWord.equals("")) 
    { 
           break; 
    } 
    //String outputWord = input.nextLine(); 
    //System.out.println(outputWord); no need of the commented lines 
    } 

} 

这是你从中获得例外的地方,即使在你解决这个例外之后,即使你解决了当前的问题,也可能得到一些例外。不要强迫readline如果没有行读取多数民众赞成在定义一个例外。也许你应该试试这个

+0

其工作我试过了我的自我 – Priyamal