62,614
社区成员
发帖
与我相关
我的任务
分享
/**
*这个是服务端
*
*/
package Chat02;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class Server {
public static final String HOST = "127.0.0.1";
public static final int PORT = 2233;
/**
* @param args
*/
public static void main(String[] args) {
new Server().startup();
}
private void startup(){
ServerSocket ss = null;
try {
ss = new ServerSocket(PORT);
while(true){
Socket s = ss.accept();
// new Thread(new ServerThread(s)).start();
Thread t = new Thread(new ServerThread(s));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(ss!=null) ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class ServerThread implements Runnable{
private Socket s;
private BufferedReader br;
private PrintWriter out;
private String name;
public ServerThread(Socket s) throws SocketException,IOException{
this.s = s;
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream(),true);
// name = br.readLine();
name += "["+s.getInetAddress().getHostAddress()+"]";
System.out.println(name + "连接了");
}
public void run(){
while(true){
try {
String str = br.readLine();
System.out.println(name + ":" + str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 这个是客户端,聊天界面 跟服务端交互的界面
*/
package Chat02;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
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.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* 聊天页面
* @author sunsichang
*
*/
public class Client extends JFrame {
/**
* 聊天面板
*/
private JTextArea jta;
/**
* 输入框
*/
private JTextField jtf;
private JPanel jp;
/**
* 发送按钮
*/
private JButton jb;
private JScrollPane jsp1,jsp2;
/**
* 在线人数列表
*/
private JList list;
/**
* 列表元素集合
*/
private DefaultListModel<String> model;
private String name;
private Socket s;
private BufferedReader br;
private PrintWriter out;
public Client(String name){
this.setLocation(100,100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(800,600);
this.setResizable(false);
this.name = name;
this.setTitle("网络聊天["+name+"]");
jta = new JTextArea();
jsp1 = new JScrollPane(jta);
jtf = new JTextField(35);
jb = new JButton("发送");
jb.addActionListener(new BtnClick());
model = new DefaultListModel<String>();
list = new JList(model);
list.setFixedCellWidth(100);
jsp2 = new JScrollPane(list);
jp = new JPanel();
this.add(jsp1);
jp.add(jtf); jp.add(jb);
this.add(jp,BorderLayout.SOUTH);
this.add(jsp2,BorderLayout.WEST);
jtf.addKeyListener(new KeyClick());
connect();
this.setVisible(true);
}
/**
* 连接到服务器
*/
private void connect() {
try {
s = new Socket(Server.HOST, Server.PORT);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream(),true);
out.println(name);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(s!=null) s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 增加事件,点击回车发送消息到面板和服务器
* @author sunsichang
*
*/
private class KeyClick extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
send();
}
}
/**
* 发送消息到服务器
*/
public void send(){
String word = jtf.getText();
if (word == null || word.trim().equals("")) {
return;
}
model.addElement(word);
jta.setText(jta.getText() + name + ":" + word + "\n");
jtf.setText("");
out.println(word);
}
/**
* 点击“发送”按钮把文本发送到控制台 和服务器
* @author sunsichang
*
*/
private class BtnClick implements ActionListener{
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jb){
send();
Object[] vals = list.getSelectedValues();
for(Object o:vals){
System.out.println(o);
}
}
}
}
}
/**
* 这个是客户端登录界面,跟服务端不进行交互,只把nam传给Client
*/
package Chat02;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
* 登录窗口
* @author sunsichang
*
*/
@SuppressWarnings("serial")
public class LoginFrame extends JFrame {
private JLabel jl;
private JButton jb;
private JTextField jtf;
public static void main(String[] args) {
new LoginFrame();
}
public void close(){
this.setVisible(false);
}
public LoginFrame(){
this.setLocation(100, 100);
this.setSize(500,130);
this.setTitle("用户登录");
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
jl = new JLabel("输入用户名:");
jtf = new JTextField(22);
jb = new JButton("连接");
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = jtf.getText();
if(name==null||name.trim().equals("")){
JOptionPane.showMessageDialog(null, "请输入用户名");
return;
}
new Client(name);
close();
}
});
this.add(jl); this.add(jtf); this.add(jb);
this.setVisible(true);
}
}