简体   繁体   English

Java套接字写错误

[英]Java Socket Write Error

I'm getting a "Socket Write Error". 我收到了“套接字写入错误”。 While scouting around on StackOverflow I found some suggestions for using flush(); 在StackOverflow上进行侦察时,我发现了一些使用flush()的建议; method to send bytes through the stream. 通过流发送字节的方法。 The problem I'm having is that after using the flush(); 我遇到的问题是在使用flush()之后; method, I still get the error. 方法,我仍然得到错误。 I'm not sure what is causing the problem. 我不确定是什么导致了这个问题。 Any ideas? 有任何想法吗? This is the complete client code: 这是完整的客户端代码:

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

   }
}

... the code continues but it doesn't seem to make it to the rest of the code anyway. ...代码继续但无论如何它似乎都没有进入代码的其余部分。 I'm a bit lost as to why it keeps failing. 我有点迷失为什么它一直失败。 I flush() after the first use of outToServer but on the second use it fails. 我在第一次使用outToServer后刷新(),但在第二次使用时它失败了。 I'm guessing this is a Client-side problem. 我猜这是客户端问题。 Any help? 有帮助吗? Thanks. 谢谢。 Here is the Server Code: 这是服务器代码:

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


   }
}

Here is the error: 这是错误:

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)

You're closing the connection in the server immediately: 您正在立即关闭服务器中的连接:

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

So obviously, as soon as the first line from the client has been read by the server and the response has been sent, the connection is closed, and the client can't send anything more. 很明显,只要服务器读取了客户端的第一行并且响应已经发送,连接就会关闭,客户端无法再发送任何内容。

Should work with below changes in the server code: 应该使用服务器代码中的以下更改:

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

Your client creates IO stream just once while server did it for every iteration so no connection could be established. 您的客户端只创建一次IO流,而服务器在每次迭代时都执行此操作,因此无法建立连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM