【问题标题】:Getting "connection to host lost" in Java Socket Client-Server在 Java Socket Client-Server 中获取“与主机的连接丢失”
【发布时间】:2015-08-03 15:40:45
【问题描述】:

对于服务器,我必须将提供的代码更改为多线程服务器。对于客户端,我对其进行了更改以使其从文本文件中读取数据。

到目前为止,我已经成功编译,但是在客户端运行时,它不仅会给出奇怪的符号,而且最后会显示“连接主机丢失”。我已经尝试更改套接字编号,但同样的问题仍然存在。

这就是它的样子:

¼Ýsr♥Car´3▼3çw3û☻♦DmileageLmodelt↕Ljava/lang/String;Lownerq~☺L

注册q~☺xp@ “本田思域”“John S”q~sq~@@t-sq~@Òêt “大众”t“玛丽亚B”q~

与主机的连接丢失。

这是我的服务器代码:

//a simple client/server application: car registration
//a SERVER program that uses a stream socket connection to exchange objects

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

public class CarsServer {

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

    ServerSocket serverSocket = null;; // TCP socket used for listening

    try {
      /* step 1: create a server socket port number: 8000 */
      serverSocket = new ServerSocket(5200);
      int i = 0;

      for(;;){
        /* setp 2: listen for a connection and create a socket */

        System.out.println("*** this server is going to register the cars ***");
        System.out.println("listening for a connection...");

        Socket clientSocket = serverSocket.accept();
        System.out.println("Spawning " + i++);
        new CarsClient(clientSocket, i).start();
      }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        /* step 5: close the connection to the client */
        System.out.println("*** the server is going to stop running ***");
        serverSocket.close();
    }
  }
}

对于客户

//a simple client/server application that exchanges OBJECTS
//a CLIENT program that uses a stream socket connection to exchange objects

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

class CarsClient extends Thread{

    private Socket incoming;
    private int client;

    public CarsClient(Socket i, int c){
        this.client = c;
        this.incoming = i;
    }

    public void run(){
        try {
          /*
           * step 2: connect input and output streams to the socket
           */
          BufferedReader oisFromServer = new BufferedReader(new FileReader("Cars.txt"));

          ObjectOutputStream oosToServer = new ObjectOutputStream(incoming.getOutputStream());

          System.out.println("I/O streams connected to the socket");

      /*
       * step 3: communicate with the server
       */
        Car[] cars = new Car[3];
        int n = 0;
        String[] l;
        String line;
        while((line = oisFromServer.readLine()) != null){
           l = line.split(", ");
            try {
                // receive an object from the server
                cars[n] = new Car(l[0], l[1], Integer.parseInt(l[2])); // casting!

                // send an object to the server
                oosToServer.writeObject(cars[n]);
                //oosToServer.flush();
                System.out.println("\n### send this car to the server for registration:\n" + cars[n]);

                System.out.println("\t###### the car returned by the server:\n"+ cars[n]);
                n++;
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                }
            } catch (EOFException eof) {
                System.out.println("The server has terminated connection!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
          /*
           * step 4: close the connection to the server
           */
          System.out.println("\nClient: closing the connection...");
          oosToServer.close();
          oisFromServer.close();
          incoming.close();
        } catch (IOException ioe) {
          ioe.printStackTrace();
        }
        System.out.println("the client is going to stop runing...");
    } // end run
}

我是编程新手,所以请帮帮我。

【问题讨论】:

    标签: java sockets client localhost


    【解决方案1】:

    您的问题似乎在这里:

    while((line = oisFromServer.readLine()) != null){
    

    您正在使用所有文本行,一旦完成,oisFromServer 将返回 null。

    要解决这个问题,我建议您使用套接字中的原始 InputStream 和 InputStream.read()。另外,曾几何时,我为这种阻塞阅读编写了一个实用程序类。见DataFetcher,及其依赖在同一个项目包中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-01
      相关资源
      最近更新 更多