1

我有以下课程

ClientDemo.java

ClientTread.java

服务器演示.java

服务器线程.java

 public class ClientDemo {
   public static void main(String[] args) throws InterruptedException {
     try {
        Socket client=new Socket("localhost", 6666);
        while(true)
        {
            System.out.println("Hello");
            Thread th=new Thread(new ClientThread(client));
            th.start();
            System.out.println("Thread started........");
            th.sleep(1000*30);

        }

     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }
    }

   }

ClientThread.java

 public class ClientThread implements Runnable {
   Socket c;
    public ClientThread(Socket client) {
    this.c=client;
}

@Override
public void run() {
    DataOutputStream dos=null;
    try {
        System.out.println("Client thread is going to write.......");
        dos = new DataOutputStream(c.getOutputStream());
        dos.writeUTF("Hello From Client");
        System.out.println("Data written by client............");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
        System.out.println(e+"");
    }


      }

    }

SeverDemo.java

      public class ServerDemo {
        public static void main(String[] args) {
      try {

    ServerSocket serversocket=new ServerSocket(6666);
    System.out.println("server listening..........");
    Thread ts=new Thread( new ServerThread(serversocket.accept()));
    ts.start();
    System.out.println("server thread started.........");
            } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
        }
     }
    }

服务器线程.java

 public class ServerThread implements Runnable {
Socket s;

public ServerThread(Socket server) {
    this.s=server;
}

@Override
public void run() {
    DataInputStream dis;
    try {
        dis = new DataInputStream(s.getInputStream());
        String message=dis.readUTF();
        System.out.println(message);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

}

代码完美执行一次,之后我收到以下错误

在客户端控制台中

Hello

Thread started........

Client thread is going to write.......

Data written by client............

Hello

Thread started........

Client thread is going to write.......

java.net.SocketException: Connection reset by peer: socket write error
4

2 回答 2

0

在您的 ServerThread 中,

public void run() {
    DataInputStream dis;
    try {
        dis = new DataInputStream(s.getInputStream());
        String message=dis.readUTF();
        System.out.println(message);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

从客户端读取一行后,执行停止。因此服务器代码退出。因此你得到了异常。所以你可以做的是:

public void run() {
        DataInputStream dis;
        try {
             while(true)
             {
                dis = new DataInputStream(s.getInputStream());
                String message=dis.readUTF();
                System.out.println(message);
             }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

在这里,我们将代码包含在一个 while 循环中的 run 方法中,以使服务器永远运行。你可以把你自己的逻辑。

希望有帮助!!

于 2013-07-20T10:28:48.230 回答
0

服务器在套接字上启动一个线程,该线程从套接字输入流中读取消息,然后将其写入控制台,然后停止运行。因此,套接字及其流超出范围并可能被垃圾收集,导致套接字关闭。而且由于套接字已关闭,客户端无法再写入任何内容。

如果您希望客户端能够发送无限的消息,那么服务器应该循环并读取无限的消息。

于 2013-07-20T10:19:06.920 回答