0

「ソケット書き込みエラー」が発生します。StackOverflow を調べているうちに、flush(); の使用に関するいくつかの提案が見つかりました。ストリームを介してバイトを送信するメソッド。私が抱えている問題は、flush(); を使用した後です。メソッド、私はまだエラーが発生します。何が問題を引き起こしているのかわかりません。何か案は?これは完全なクライアント コードです。

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() を実行しましたが、2 回目に使用すると失敗します。これはクライアント側の問題だと思います。何か助けはありますか?ありがとう。サーバーコードは次のとおりです。

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)
4

2 に答える 2

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();

したがって、明らかに、クライアントからの最初の行がサーバーによって読み取られ、応答が送信されるとすぐに、接続が閉じられ、クライアントはそれ以上何も送信できなくなります。

于 2013-03-25T18:56:01.810 に答える
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ストリームを1回だけ作成しますが、サーバーは反復ごとに作成するため、接続を確立できません。

于 2013-03-25T21:20:21.467 に答える