• Java 中使用serversocket通信


    1. 创建一个Java项目 TestMyServerSocket.

    2. 创建一个包 com.weloglog.main

    3. 创建一些使用到的类

    程序启动类 MyServerSccket : 用来启动程序, 启动之后创建一个进程, 用来监视本机当前端口

    监视本机端口进程类 ServerListener : 用来监视本地端口, 当有连接本机端口时会生成一个聊天类 ChatSocket 并把这个聊天类添加到 聊天管理类(单例类)中 ChatManager

    聊天类 ChatSocket : 用来发送消息给所以聊天窗口(除过自己)

    聊天管理类 ChatManager : 用来收集管理所有聊天窗口

    4. 以下为所有类代码:

    MyServerSccket.java

    package com.weloglog.main;
    
    
    public class MyServerSccket {
    
        public static void main(String[] args) {
            new ServerListener().start();
        }
    
    }
    View Code

    ServerListener.java

    package com.weloglog.main;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.swing.JOptionPane;
    
    public class ServerListener extends Thread {
        
        int port = 12345;
        @Override
        public void run() {
            try {
                //端口 : 1 - 65535
                ServerSocket serverSocket = new ServerSocket(port);
                while (true) {                
                    //阻塞的方法
                    Socket socket = serverSocket.accept();
                    //建立连接
                    JOptionPane.showMessageDialog(null, "有客户端连接到本机12345端口");
                    //将Socket传递给线程
                    ChatSocket cs = new ChatSocket(socket);
                    cs.start();
                    ChatManager.getChatManager().add(cs);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    View Code

    ChatSocket.java

    package com.weloglog.main;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import java.net.Socket;
    
    public class ChatSocket extends Thread {
        
        Socket socket;
        
        public ChatSocket(Socket s){
            this.socket = s;
        }
    
        //发送消息
        public void out(String out){
            try {
                socket.getOutputStream().write(out.getBytes("UTF-8"));
                System.out.println(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void run() {
            try {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(
                                socket.getInputStream(),"UTF-8"));
                String line = null;
                while((line = br.readLine()) != null){
                    ChatManager.getChatManager().publish(this, line);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    View Code

    ChatManager.java

    package com.weloglog.main;
    
    import java.util.Vector;
    
    public class ChatManager {
        //单例类
        private ChatManager(){}
        private static final ChatManager cm = new ChatManager();
        public static ChatManager getChatManager(){
            return cm;
        }
        
        //存放聊天进程的容器
        Vector<ChatSocket> vector = new Vector<ChatSocket>();
        
        //添加
        public void add(ChatSocket cs){
            vector.add(cs);
        }
        
        //遍历发送消息
        public void publish(ChatSocket cs, String out){
            for (int i = 0; i < vector.size(); i++) {
                ChatSocket csChatSocket = vector.get(i);
                //把不是自己输出
                if (!cs.equals(csChatSocket)) {
                    csChatSocket.out(out);
                }
            }
        }
    }
    View Code
  • 相关阅读:
    linux之SQL语句简明教程---主键,外来键
    [LeetCode][Java] Best Time to Buy and Sell Stock IV
    S3C2440 IIS操作 uda134x录放音
    Cocos2d-x 3.0 打造一个全平台概念文件夹
    Irrlicht 3D Engine 笔记系列之 教程4
    Swift----编程语言语法
    Nginx优化指南+LINUX内核优化+linux连接数优化+nginx连接数优化
    windows平台是上的sublime编辑远程linux平台上的文件
    POJ 2249-Binomial Showdown(排列组合计数)
    Linux 循环
  • 原文地址:https://www.cnblogs.com/weloglog888/p/7652899.html
Copyright © 2020-2023  润新知