[原]JavaSocket实现广播聊天室
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) {}
}
}
}
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();
}
}
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();
}
}