用socket实现简单聊天室

package com.xmut.myChat;


import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

//要想达到多人聊天,必须要使用线程池来控制


public class Server {

static JTextArea jta=new JTextArea();

static JTextArea jta2=new JTextArea();

//线程池

static ExecutorService exec;

//功能:把各个接收到的记录返回给其它客户端

static ArrayList<Socket> list=new ArrayList<Socket>();

static class GetMessage implements Runnable{

Socket socket;

DataInputStream fromClient;

DataOutputStream toClient;

public GetMessage(Socket socket){

this.socket=socket;

}

@Override

public void run() {

try {

fromClient=new DataInputStream(socket.getInputStream());

String s=fromClient.readUTF();

while(true){

jta.append(socket.getPort()+":"+s+"\n");

s=socket.getPort()+":"+s;

for(Socket moresocket:list){

toClient=new DataOutputStream(moresocket.getOutputStream());

toClient.writeUTF(s);

}

s=fromClient.readUTF();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}


public static void main(String[] args) {

JFrame jf=new JFrame("聊天室-服务器");

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setVisible(true);

jf.setBounds(400, 100, 300, 500);

jta.setLineWrap(true);

JScrollPane jsp=new JScrollPane(jta);

jf.add(BorderLayout.CENTER, jsp);

try {

ServerSocket serverSocket=new ServerSocket(8888);

exec=Executors.newCachedThreadPool();

jta.append("服务器已经开启");

while(true){

Socket socket=serverSocket.accept();

GetMessage gm=new GetMessage(socket);

list.add(socket);

exec.execute(gm);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}




package com.xmut.myChat;


import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;


import com.xmut.myChat.Server.GetMessage;

public class Client {


static JTextArea jta=new JTextArea();

static JTextArea jta2=new JTextArea();

static class GetMessage extends Thread{

Socket socket;

public GetMessage(Socket socket){

this.socket=socket;

}

@Override

public void run() {

try {

DataInputStream dis=new DataInputStream(socket.getInputStream());

String s=dis.readUTF();

System.out.println(s);

while(!s.equals("byb")){

jta.append(s+"\n");

s=dis.readUTF();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}


public static void main(String[] args) {

JFrame jf=new JFrame("聊天室-客户端");

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setVisible(true);

jf.setBounds(100, 100, 300, 500);

jta.setLineWrap(true);

JScrollPane jsp=new JScrollPane(jta);

jf.add(BorderLayout.CENTER, jsp);

//下半部文本框使用一个Panel来装载

JPanel jp=new JPanel();

jp.setLayout(new BorderLayout());

jta.setLineWrap(true);

JScrollPane jsp2=new JScrollPane(jta2);

JPanel jp2=new JPanel();

jp2.setLayout(new GridLayout(1, 2, 10, 10));

JButton button=new JButton("发送");

JButton button2=new JButton("取消");

jp2.add(button);jp2.add(button2);

jp.add(BorderLayout.CENTER,jta2);

jp.add(BorderLayout.SOUTH,jp2);

jf.add(BorderLayout.SOUTH,jp);

try {

final Socket socket=new Socket("localhost", 8888);

GetMessage gm=new GetMessage(socket);

gm.start();

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

try {

DataOutputStream dos=new DataOutputStream(socket.getOutputStream());

dos.writeUTF(jta2.getText());

dos.flush();

jta2.setText("");

} catch (IOException e1) {

e1.printStackTrace();

}

}

});

} catch (IOException e) {

e.printStackTrace();

}

}

}


你可能感兴趣的:(用socket实现简单聊天室)