socket 实现点对点发送消息

流过昼夜 提交于 2019-11-27 19:10:36
  1. socket:套接字,描述 ip 和 port;socket 在建立网络连接时使用;通过socket可以向网络发送请求以及应答网络请求
  2. serverSocket:主要应用于服务器端,监听 socket 连接
  3. 具体示例代码如下:
package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端
 * @author hualei
 * @date Sep 3, 2017 4:26:34 PM
 * 
 */
public class Client {

    private Socket socket;
    private String alias;

    public Client(){
        try {
            socket = new Socket("127.0.0.1", 8088);
            // socket = new Socket("ip", 8088);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        // System.out.println("enter client ...");
        Scanner scanner = null;
        try {

            // 接收服务端消息
            ServerHandler serverHandler = new ServerHandler(socket);
            Thread thread = new Thread(serverHandler);
            thread.setDaemon(true);
            thread.start();

            OutputStream outputStream = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(outputStream, "utf-8"), true);
            // 发送单行数据 pw.println("Hello, server!");
            // 发送多行数据
            scanner = new Scanner(System.in);
            System.out.println("请输入昵称:");
            alias = scanner.nextLine();
            System.out.println("你好!" + alias + ", 开始聊天吧!");
            pw.println(alias + " 上线了");

            while(true){
                pw.println(alias + "对你说:" + scanner.nextLine());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            scanner.close();
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }

    private class ServerHandler implements Runnable{
        private Socket socket;
        public ServerHandler(Socket socket){
            this.socket = socket;
        }
        @Override
        public void run() {
            InputStream inputStream;
            try {
                inputStream = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                while(true){
                    System.out.println(br.readLine());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * 服务器端
 * @author hualei
 * @date Sep 3, 2017 4:26:51 PM
 * 
 */
public class Server {

    private ServerSocket serverSocket;
    // private volatile int count;
    private List<PrintWriter> allOut = new ArrayList<PrintWriter>();
    private ExecutorService threadPool = Executors.newFixedThreadPool(10);
    private Map<String, PrintWriter> aliasOuts = new HashMap<String, PrintWriter>();

    public Server(){
        try {
            serverSocket = new ServerSocket(8088);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        // System.out.println("enter server ...");
        try {
            // 循环监听 socket
            while(true){
                System.out.println("等待客户端连接...");
                Socket socket = serverSocket.accept();
                System.out.println("客户端连接成功");
                //count ++;

                // 未抽取处理类之前代码
                /*System.out.println("服务端与客户端 "+count+" 建立连接");
                InputStream inputStream = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));

                // 接收多行数据
                while(true){
                    System.out.println("客户端" + count + "说:" + br.readLine());
                }*/
                ClientHandler clientHandler = new ClientHandler(socket);
                threadPool.execute(clientHandler);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }

    private class ClientHandler implements Runnable{
        // private int ccount;
        private Socket socket;

        public ClientHandler(Socket socket /*, int count*/){
            this.socket = socket;
            //this.ccount = count;
        }

        @Override
        public void run() {
            // System.out.println("服务端与客户端 "+ccount+" 建立连接");
            PrintWriter pw = null;
            try {
                // 接收客户端输出流
                OutputStream outputStream = socket.getOutputStream();
                pw = new PrintWriter(new OutputStreamWriter(outputStream, "utf-8"), true);
                add(pw);

                InputStream inputStream = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));

                String content = "";
                while((content = br.readLine()) != null){
                    if(!content.contains(":")){
                        aliasOuts.put(content.trim(), pw);
                    }

                    if(content.startsWith("@") && content.contains(":")){   
                        String pwName = content.substring(content.indexOf("@"), content.indexOf(":"));
                        aliasOuts.get(pwName).println(content.substring(content.indexOf(":")+1));
                    }else{
                        foreach(content);
                    }

                }

                // 支持多个客户发送数据,服务器端只接收数据并展示
                /*InputStream   inputStream = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                String content = "";
                // 接收多行数据
                while((content = br.readLine()) != null){
                    System.out.println("客户端" + ccount + "说:" + content);
                }*/

            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                remove(pw);
                if(socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private synchronized void add(PrintWriter pw){
        allOut.add(pw);
    }
    private synchronized void remove(PrintWriter pw){
        allOut.remove(pw);
    }
    private synchronized void foreach(String content){
        Set<Entry<String, PrintWriter>> entrySet = aliasOuts.entrySet();
        Iterator<Entry<String, PrintWriter>> iterator = entrySet.iterator();

        while(iterator.hasNext()){
            Entry<String, PrintWriter> next = iterator.next();
            String key = next.getKey();
            if(!key.contains(" 上线了")){
                next.getValue().println(content);
            }
        }
    }


}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!