看得破的人,处处都是生机;

看不破的人,处处都是困境;

拿得起的人,处处都是担当;

拿不起的人,处处都是疏忽;

放得下的人,处处都是大道;

放不下的人,处处都是迷茫;

想得开的人,处处都是春天;

想不开的人,处处都是凋枯;

做何人,在自己;

小自我,大天地;

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

 

posted @ 2017-10-11 22:44  心电心  阅读(2152)  评论(0编辑  收藏  举报