- public class Client {
- public static void main(String[] args)
- {
- Socket socket = null;
- try {
- socket = new Socket("localhost",8888);
- DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
- DataInputStream dis = new DataInputStream(socket.getInputStream());
- dos.writeUTF("我是客户端,请求连接!");
-
- System.out.println(dis.readUTF());
- socket.close();
- } catch (UnknownHostException e)
- {
- e.printStackTrace();
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
复制代码- public class Server {
- public static void main(String[] args)
- {
- ServerSocket ss = null;
- try {
- ss = new ServerSocket(8888);
- Socket socket = ss.accept();
- DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
- DataInputStream dis = new DataInputStream(socket.getInputStream());
- System.out.println("服务器接收到客户端的连接请求:" + dis.readUTF());
- dos.writeUTF("接受连接请求,连接成功!");
- socket.close();
- ss.close();
- } catch (IOException e){
- e.printStackTrace();
- }
- }
- }
复制代码 上面是我写的客户端和服务端通信的程序,我现在想做到就是客户端不停地输入,服务端能够不停地接受,知道服务端的服务厅了,在客户端提示断开连接,或者客户端结束以后,服务端提示客户端断开连接。有哪个大神帮忙讲解一下。
|