2013-03-25 72 views
0

我收到「套接字寫入錯誤」。在探索StackOverflow時,我發現了一些使用flush()的建議;方法通過流發送字節。我遇到的問題是在使用flush()之後;方法,我仍然得到錯誤。我不確定是什麼導致了這個問題。有任何想法嗎?這是完整的客戶端代碼:Java Socket寫入錯誤

import java.io.*; 
import java.net.*; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class TCPClient { 

    public static void main(String[] args) throws Exception { 

     String origSentence; 
     String modifiedSentence; 
     boolean win = false; 
     boolean lose = false; 

     //BufferedReader inFromUser; 
     //inFromUser = new BufferedReader(new InputStreamReader(System.in)); 
     Scanner inFromUser = new Scanner(System.in); 
     Socket clientSocket = new Socket("10.64.121.145", 6784); 
     boolean first = true; 
     DataOutputStream outToServer = 
       new DataOutputStream(clientSocket.getOutputStream()); 
     origSentence = inFromUser.nextLine(); 
     outToServer.writeBytes(origSentence + '\n'); 
     outToServer.flush(); 

     Scanner inFromServer = new Scanner(clientSocket.getInputStream()); 
     modifiedSentence = inFromServer.nextLine(); 
     GUI gui = new GUI(Integer.parseInt(modifiedSentence)); 
     System.out.println("The word is " + modifiedSentence + " letters long. Pick a letter!"); 
     gui.isNotSolved(); 

     while (win != true && lose != true) { 
     origSentence = inFromUser.nextLine(); 
     System.out.println(origSentence); 
     outToServer.writeBytes(origSentence + '\n'); //The failure is on this line 
     outToServer.flush(); 
     modifiedSentence = inFromServer.nextLine(); 
     if (modifiedSentence.equals(""))//guess not in word so add a miss 
     { 
      if (gui.addMiss(origSentence) == false) { 
       lose = true; 
      } 
     } 
     if (!modifiedSentence.equals(""))//guess is in word so display where letter occurs 
     { 
      ArrayList<Character> list = new ArrayList<Character>(); 
      int length = modifiedSentence.length(); 
      for (int i = 0; i < length; i++) { 
       list.add(modifiedSentence.charAt(i)); 
      } 
      for (char c : list) { 
       if (c == ' ') { 
        list.remove(c); 
       } 
      } 
      for (int i = 0; i < list.size(); i++) { 
       String s = list.get(i).toString(); 
       gui.addLetter(origSentence.charAt(0), Integer.parseInt(s)); 
      } 

     } 

     if (gui.isNotSolved() == false)//game won send server "won" message to terminate connection 
     { 
      String won = "won"; 
      outToServer.writeBytes(won); 
      outToServer.flush(); 
     } 

     if (lose == true)//game lost send server "lost" message to 
     {   //have server send the word to display for player 
      String lost = "lost"; 
      outToServer.writeBytes(lost); 
      gui.setWord(modifiedSentence); 
      outToServer.flush(); 
     } 
     } 
     clientSocket.close(); 

    } 
} 

...代碼繼續,但似乎它並沒有使它的其餘代碼反正。我有點失落,爲什麼它一直失敗。我在第一次使用outToServer之後flush(),但第二次使用失敗。我猜這是客戶端問題。任何幫助?謝謝。這裏是服務器代碼:

import java.io.*; 
import java.net.*; 
import java.util.Scanner; 

public class TCPServer { 

    public static void main(String[] args) throws Exception { 
     String clientSentence; 
     Word word = new Word(); 
     int wordLen = word.getLength(); 
     ServerSocket welcomeSocket = new ServerSocket(6784); 
     System.out.println(InetAddress.getLocalHost()); 
     boolean begin = true; 

     while (true) { 
     Socket connSocket = welcomeSocket.accept(); 
     Scanner inFromClient = new Scanner(connSocket.getInputStream()); 
     clientSentence = inFromClient.nextLine(); //cap would go after this 
     DataOutputStream outToClient = 
       new DataOutputStream(connSocket.getOutputStream()); 

     if (begin == true) { 
      //DataOutputStream outToClient = 
      //new DataOutputStream(connSocket.getOutputStream()); 
      System.out.println("begin " + word.getWord()); 
      outToClient.writeBytes("" + word.getLength()); 
      outToClient.flush(); 
     } 
     if (begin == false) { 
      System.out.println("hello"); 
      //DataOutputStream outToClient = 
      //new DataOutputStream(connSocket.getOutputStream()); 
      String pos = word.getSpots(clientSentence.charAt(0)); 
      outToClient.writeBytes(pos); 
      outToClient.flush(); 
     } 
     if (clientSentence.equals("lost")) { 
      outToClient.writeBytes(word.getWord()); 
      outToClient.flush(); 
     } 
     if (clientSentence.equals("won")) { 
      connSocket.close(); 
     } 
     begin = false; 
     connSocket.close(); 
     } 


    } 
} 

以下是錯誤:

Exception in thread "main" h //Ignore the 'h', it's just a print statement checking input 
java.net.SocketException: Software caused connection abort: socket write error 
at java.net.SocketOutputStream.socketWrite0(Native Method) 
at java.net.SocketOutputStream.socketWrite(Unknown Source) 
at java.net.SocketOutputStream.write(Unknown Source) 
at java.io.DataOutputStream.writeBytes(Unknown Source) 
at TCPClient.main(TCPClient.java:35) 

The error now is: 
Exception in thread "main" java.net.ConnectException: Connection timed out: connect 
at java.net.PlainSocketImpl.socketConnect(Native Method) 
at java.net.PlainSocketImpl.doConnect(Unknown Source) 
at java.net.PlainSocketImpl.connectToAddress(Unknown Source) 
at java.net.PlainSocketImpl.connect(Unknown Source) 
at java.net.SocksSocketImpl.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at java.net.Socket.<init>(Unknown Source) 
at java.net.Socket.<init>(Unknown Source) 
at TCPClient.main(TCPClient.java:17) 
+0

完整的堆棧跟蹤,請。 – 2013-03-25 18:05:36

+0

你的意思是發佈其餘的代碼?什麼是完整的堆棧跟蹤? – user1767061 2013-03-25 18:06:22

+0

引發異常的整個錯誤消息。並把它放在你的問題。 – 2013-03-25 18:07:47

回答

2

您要關閉的立即與服務器的連接:一旦

Socket connSocket = welcomeSocket.accept(); 
Scanner inFromClient = new Scanner(connSocket.getInputStream()); 
clientSentence = inFromClient.nextLine(); //cap would go after this 
DataOutputStream outToClient = 
    new DataOutputStream(connSocket.getOutputStream()); 

[...] 
connSocket.close(); 

所以,很顯然,作爲客戶端的第一行已被服務器讀取,並且響應已發送,連接已關閉,客戶端無法發送更多內容。

+0

我拿出connSocket.close(),但現在出現以下錯誤: – user1767061 2013-03-25 19:26:47

+0

線程「main」異常java.net.ConnectException:連接超時:連接 \t at java.net.PlainSocketImpl.socketConnect(Native Method ) \t在java.net.PlainSocketImpl.doConnect(未知來源) \t在java.net.PlainSocketImpl.connectToAddress(未知來源) \t在java.net.PlainSocketImpl.connect(未知來源) \t在java.net。 SocksSocketImpl.connect(Unknown Source) \t at java.net.Socket.connect(Unknown Source) \t at java.net.Socket.connect(Unknown Sour ce) \t at java.net.Socket。 (Unknown Source) \t at java.net.Socket。 (來源不明) \t在TCPClient.main(TCPClient.java:17) – user1767061 2013-03-25 19:27:03

+0

你一定已經改變了別的東西。你的程序以前沒有得到連接失敗,否則你不會得到寫入錯誤。 – EJP 2013-03-25 21:52:16

0

應與服務器代碼如下修改工作:

Socket connSocket = welcomeSocket.accept(); 
Scanner inFromClient = new Scanner(connSocket.getInputStream()); 
DataOutputStream outToClient = new DataOutputStream(connSocket.getOutputStream()); 
while (inFromClient.hasNext()) { 
    clientSentence = inFromClient.nextLine(); //cap would go after this 
    System.out.println("received from client: "+clientSentence); 
} 
. 
. 
. 
. 
connSocket.close(); 

你的客戶端創建IO流只有一次,而服務器沒有爲它每次迭代所以沒有連接可以建立。