• java Soket编程(三)


    简单实现聊天功能:

    必须客户端先发消息。

    服务器端:

    package com.robert.view;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    import com.robert.view.CustomerClient.ReadThreadHandler;
    
    public class ServerClient {
    	ServerSocket serverSocket = null;
    	Socket clientSocket = null;
    	Socket socket = null;
    	InputStream inputStream = null;
    	BufferedInputStream bis = null;
    	DataInputStream dis = null;
    	OutputStream outputStream = null;
    	BufferedOutputStream bos = null;
    	DataOutputStream dos = null;
    
    	public static void main(String[] args) {
    
    		ServerClient serverClient = new ServerClient();
    		serverClient.communicate();
    	}
    
    	public void communicate() {
    		try {
    			serverSocket = new ServerSocket(9999);
    			while (true) {
    				socket = serverSocket.accept();
    				Runnable threadHandler = new ThreadHandler(socket);
    				Thread thread = new Thread(threadHandler);
    				thread.start();
    
    				Runnable readThreadHandler = new ReadThreadHandler(socket);
    				Thread readThread = new Thread(readThreadHandler);
    				readThread.start();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	class ThreadHandler implements Runnable {
    		private Socket socket;
    
    		public ThreadHandler(Socket socket) {
    			this.socket = socket;
    		}
    
    		@Override
    		public void run() {
    			try {
    				String input = "";
    				while (!"exit".equals(input)) {
    					outputStream = socket.getOutputStream();
    					bos = new BufferedOutputStream(outputStream);
    					dos = new DataOutputStream(bos);
    					Scanner scanner = new Scanner(System.in);
    					input = scanner.nextLine();
    					dos.writeUTF(input);
    					dos.flush();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	class ReadThreadHandler implements Runnable {
    		private Socket socket = null;
    
    		public ReadThreadHandler(Socket socket) {
    			this.socket = socket;
    		}
    
    		@Override
    		public void run() {
    			try {
    				String output = "";
    				while(!"exit".equals(output))
    				{
    					inputStream = socket.getInputStream();
    					bis = new BufferedInputStream(inputStream);
    					dis = new DataInputStream(bis);
    					output = dis.readUTF();
    					System.out.println("client:"+output);	
    				}
    				
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}	}
    }
    

    客户端:

    package com.robert.view;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    
    public class CustomerClient {
    
    	OutputStream outputStream = null;
    	BufferedOutputStream bos = null;
    	DataOutputStream dos = null;
    
    	InputStream inputStream = null;
    	BufferedInputStream bis = null;
    	DataInputStream dis = null;
    
    	public static void main(String[] args) {
    		CustomerClient customerClient = new CustomerClient();
    		customerClient.communication();
    	}
    
    	public void communication() {
    		try {
    			Socket socket = new Socket("localhost", 9999);
    
    			Runnable writeThreadHandler = new WriteThreadHandler(socket);
    			Thread writeThread = new Thread(writeThreadHandler);
    			writeThread.start();
    
    			Runnable readThreadHandler = new ReadThreadHandler(socket);
    			Thread readThread = new Thread(readThreadHandler);
    			readThread.start();
    
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	class WriteThreadHandler implements Runnable {
    		Socket socket = null;
    
    		public WriteThreadHandler(Socket socket) {
    			this.socket = socket;
    		}
    
    		@Override
    		public void run() {
    			try {
    				String input = "";
    				while (!"exit".equals(input)) {
    					outputStream = socket.getOutputStream();
    					bos = new BufferedOutputStream(outputStream);
    					dos = new DataOutputStream(bos);
    					Scanner scanner = new Scanner(System.in);
    					input = scanner.nextLine();
    					dos.writeUTF(input);
    					dos.flush();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	class ReadThreadHandler implements Runnable {
    		private Socket socket = null;
    
    		public ReadThreadHandler(Socket socket) {
    			this.socket = socket;
    		}
    
    		@Override
    		public void run() {
    			try {
    				String output = "";
    				while (!"exit".equals(output)) {
    					inputStream = socket.getInputStream();
    					bis = new BufferedInputStream(inputStream);
    					dis = new DataInputStream(bis);
    					output = dis.readUTF();
    					System.out.println("server:" + output);
    				}
    
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    


    实现原理:

    1.先建立一个Server端,然后客户端去连接该server。

    2.取得对应端(server端,client端)的输入流和输出流。

    3.发送消息,互相通信。

  • 相关阅读:
    Binder机制,从Java到C (大纲)
    大陆台湾计算机术语比对
    关于nginx架构探究(2)
    关于nginx架构探究(1)
    关于开源中文搜索引擎架构coreseek中算法详解
    关于linux定时操作cron的理解
    sqlachemy 使用实例
    nginx+uwsgi+flask搭建python-web应用程序
    VC维
    关于zMPLS的设计解析
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986883.html
Copyright © 2020-2023  润新知