惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

www.infosecurity-magazine.com
www.infosecurity-magazine.com
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
Cloudbric
Cloudbric
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
N
News and Events Feed by Topic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
DataBreaches.Net
S
Schneier on Security
L
LangChain Blog
Jina AI
Jina AI
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
T
Tenable Blog
B
Blog RSS Feed
V
Visual Studio Blog
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
T
The Exploit Database - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
WordPress大学
WordPress大学
W
WeLiveSecurity
I
InfoQ
The Hacker News
The Hacker News
雷峰网
雷峰网
月光博客
月光博客
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
Hacker News: Ask HN
Hacker News: Ask HN
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
The Last Watchdog
The Last Watchdog
P
Privacy International News Feed
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
L
Lohrmann on Cybersecurity
人人都是产品经理
人人都是产品经理
V
V2EX
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Cybersecurity and Infrastructure Security Agency CISA
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
阮一峰的网络日志
阮一峰的网络日志
SecWiki News
SecWiki News
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 张谊

[转]ubuntu下 手动安装 LAMP 和 JAVA环境 [转]ViewState的使用 [转]Working with user roles and permissions in SharePoint Object Model [原]SharePoint文档库上传文档 [原]SharePoint列表与文档库EventHandeler [转]ASP.NET缓存概念及其应用浅析 window.showModalDialog以及window.open用法简介 [原]欢迎加入QQ群 [原]如何在Silverlight中使用WebService绑定DataGrid [转]Silverlight中调用远程Web Service的权限问题 [转]BeanUtils接口和类 [原]Commons- BeanUtils学习笔记 [转]Apache+Tomcat负载均衡及Session绑定的实现 [原]基于Caché多维数据库的SSH实现 [原]关于支付宝API开发的一点心得 [原]Oracle中列自增的方法 [原]Java反射示例 [转]翻译 一些很酷的.Net技巧 《生命如一泓清水》
[原]JavaSocket实现广播聊天室
张谊 · 2008-09-01 · via 博客园 - 张谊

Server:

package ChatIV;
import java.net.*;
import java.io.*;
import java.util.*;//广播聊天室服务端
public class Server {
    
public static void main(String args[]){
        
try {
            ServerSocket ss 
= new ServerSocket(8186);//实现Socket
            List sockets = new ArrayList();//创建一个集合,保存文字消息
            while(true){
                Socket s 
= ss.accept();//监听8186
                sockets.add(s);//向集合中添加Socket的对象S,把听到的内容保存到集合中
                Thread t1 = new ChatThread(s,sockets);//线程
                t1.start();
            }
        } 
catch (IOException e) {
            e.printStackTrace();
        }
    }    
}
class ChatThread extends Thread{
    Socket s;
    List sockets;
    
public ChatThread(Socket s,List sockets){
        
this.s=s;
        
this.sockets=sockets;
    }
    
public void run(){
        
try {
            BufferedReader in 
= new BufferedReader(
                    
new InputStreamReader(s.getInputStream()));//包装成字符流
            while(true){
                String str 
= in.readLine();//读BufferedReader
                for(int i=0;i<sockets.size();i++){//利用For循环遍历集合
                    Socket s2 = (Socket)sockets.get(i);//创建Socket对象S2,强转成Socket,并获取下标
                    PrintWriter out = new PrintWriter(s2.getOutputStream());//文本流输出
                    out.println(str);//打印字符
                    out.flush();//刷新该流的缓冲
                }
            }
        } 
catch (IOException e) {}
        
finally{
            
try {
                s.close();
            } 
catch (IOException e) {}
        }
    }
}

Client:

package ChatIV;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;//广播聊天室客户端
public class Client {
    JTextArea jta;
    JTextField jtf;
    BufferedReader in;
    PrintWriter out;
    
//Swing画一个界面
    private void initGUI(){
        JFrame f
=new JFrame("Client");
        f.setSize(
400,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jta
=new JTextArea();
        jta.setEditable(
false);
        f.add(
new JScrollPane(jta));
        jtf
=new JTextField();
        f.add(jtf,
"South");
        f.setVisible(
true);
        jtf.addActionListener(
new ActionListener(){
            
public void actionPerformed(ActionEvent arg0) {
                String text
=jtf.getText();
                jtf.setText(
"");
                out.println(text);
                out.flush();
            }
        });
    }
    
//连接初始化
    private void initNet(){
        
try {
            Socket s 
= new Socket("127.0.0.1",8186);//创建Socket端口8186,端口号随意,避免常用端口即可
            out = new PrintWriter(s.getOutputStream());//文本流输出,利用Socket对象获得getOutputStream()
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));//包装成字符流
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } 
catch (IOException e) {
            e.printStackTrace();
        }
    }
    
//消息接收
    private void receive(){
        
try {
            
while(true){
                String str 
= in.readLine();//读BufferedReader
                if(str==null){
                    
return ;
                }
                jta.append(
""+str+"\n");//加载到JTextArea中,显示
            }
        } 
catch (IOException e) {
            e.printStackTrace();
        }
    }
    
private Client(){
        
this.initGUI();
        
this.initNet();
    }
    
public static void main(String args[]){
        Client c 
= new Client();
        c.receive();
    }
}