主题:请各位高手帮忙看一下我的程序,帮忙完成后面的工作,是一个socket聊天室的程序。
[b]//服务器端程序[/b]package chat;
import java.awt.BorderLayout;
import java.awt.Container;
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.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
public class Server extends JFrame {
private JButton jbtListen = new JButton("Listen");
private JButton jbtSend = new JButton("Send");
private JButton jbtExit = new JButton("Exit chat");
private JTextArea jtaShow = new JTextArea();
private JTextField jtfShow = new JTextField(30);
private JToolBar toolBar = new JToolBar();
private JPanel jp1 = new JPanel();
// 构造方法
public Server() {
toolBar.add(jbtListen);
toolBar.add(jbtExit);
Container c = this.getContentPane();
BorderLayout bl = new BorderLayout();
this.setLayout(bl);
jp1.add(jtfShow);
jp1.add(jbtSend);
c.add(toolBar, bl.NORTH);
c.add(jtaShow, bl.CENTER);
c.add(jp1, bl.SOUTH);
ServerActionListener actionListener = new ServerActionListener();
jbtListen.addActionListener(actionListener);
jbtSend.addActionListener(actionListener);
jbtExit.addActionListener(actionListener);
}
// 内部类
class ServerActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtListen) {
// System.out.println("llllll");
listen();
}
if (e.getSource() == jbtExit) {
// System.out.println("eeee");
disconnect();
}
if (e.getSource() == jbtSend) {
// System.out.println("ssss");
send();
String str = jtfShow.getText();
jtaShow.append(str + "\n");
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Server ServerFrame = new Server();
ServerFrame.setResizable(false);
ServerFrame.setTitle("Server");
ServerFrame.setSize(500, 500);
ServerFrame.setVisible(true);
ServerFrame.setLocationRelativeTo(null);
}
// Socket
ServerSocket serverSocket = null;
Socket socket = null;
BufferedReader sockIn;
PrintWriter sockOut;
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
public void listen() {
// 创建一个端口号为8888的ServerSocket
try {
serverSocket = new ServerSocket(8888);
System.out.println("Server listening on port 8888");
// 监听客户端的连接请求,当建立连接时,返回一个代表此连接的Socket对象
socket = serverSocket.accept();
if (socket == null) {
System.out.println("socket null");
System.exit(1);
}
System.out.println("accept connection from:"
+ socket.getInetAddress().getHostAddress());
// 得到输入流
sockIn = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
}
public void send() {
try {
sockOut = new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
sockOut.println("hello,i am client");
sockOut.flush();
}
}
[b]//客户端程序[/b]
package chat;
import java.awt.BorderLayout;
import java.awt.Container;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
public class Client extends JFrame {
// 建组件
private JButton jbtConnect = new JButton("Connect");
private JButton jbtExit = new JButton("Exit chat");
private JButton jbtSend = new JButton("Send");
private JLabel jlServer = new JLabel("Server");
private JLabel jlPort = new JLabel("Port");
private JTextArea jtaShowServer = new JTextArea("127.0.0.1", 1, 10);
private JTextArea jtaShowPort = new JTextArea("8888", 1, 10);
private JTextArea jtaShow = new JTextArea();
private JTextField jtfShow = new JTextField(30);
private JToolBar toolBar = new JToolBar();
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
// 构造方法
public Client() {
jbtExit.setEnabled(false);// 按钮初始化状态
toolBar.add(jbtConnect);
toolBar.add(jbtExit);
jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
jp1.add(toolBar);
jp1.add(jlServer);
jp1.add(jtaShowServer);
jp1.add(jlPort);
jp1.add(jtaShowPort);
jp2.add(jtfShow);
jp2.add(jbtSend);
JScrollPane jsp = new JScrollPane(jtaShow,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//加组件
Container c = this.getContentPane();
BorderLayout bl = new BorderLayout();
this.setLayout(bl);
c.add(jp1, bl.NORTH);
c.add(jtaShow, bl.CENTER);
c.add(jp2, bl.SOUTH);
//注册监听器
ClientActionListener actionListener = new ClientActionListener();
jbtSend.addActionListener(actionListener);
}
//内部类
class ClientActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtSend) {
String str = jtfShow.getText();
jtaShow.append(str + "\n");
}
if (e.getSource() == jbtConnect) {
jbtExit.setEnabled(true);
//jbtConnect.setEnabled(false);
}
if (e.getSource() == jbtExit) {
jbtConnect.setEnabled(true);
}
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
Client ClientFrame = new Client();
ClientFrame.setResizable(false);
ClientFrame.setTitle("Client");
ClientFrame.setSize(500, 500);
ClientFrame.setVisible(true);
ClientFrame.setLocationRelativeTo(null);
}
}
[color=FF0000][size=3][size=1]
小弟只会写这么多了,GUI已经完成了。请各位高手在我的程序上帮忙完成后面的,要求实现多个客户端通信,谢谢大家。[/size][/size][/color]
import java.awt.BorderLayout;
import java.awt.Container;
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.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
public class Server extends JFrame {
private JButton jbtListen = new JButton("Listen");
private JButton jbtSend = new JButton("Send");
private JButton jbtExit = new JButton("Exit chat");
private JTextArea jtaShow = new JTextArea();
private JTextField jtfShow = new JTextField(30);
private JToolBar toolBar = new JToolBar();
private JPanel jp1 = new JPanel();
// 构造方法
public Server() {
toolBar.add(jbtListen);
toolBar.add(jbtExit);
Container c = this.getContentPane();
BorderLayout bl = new BorderLayout();
this.setLayout(bl);
jp1.add(jtfShow);
jp1.add(jbtSend);
c.add(toolBar, bl.NORTH);
c.add(jtaShow, bl.CENTER);
c.add(jp1, bl.SOUTH);
ServerActionListener actionListener = new ServerActionListener();
jbtListen.addActionListener(actionListener);
jbtSend.addActionListener(actionListener);
jbtExit.addActionListener(actionListener);
}
// 内部类
class ServerActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtListen) {
// System.out.println("llllll");
listen();
}
if (e.getSource() == jbtExit) {
// System.out.println("eeee");
disconnect();
}
if (e.getSource() == jbtSend) {
// System.out.println("ssss");
send();
String str = jtfShow.getText();
jtaShow.append(str + "\n");
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Server ServerFrame = new Server();
ServerFrame.setResizable(false);
ServerFrame.setTitle("Server");
ServerFrame.setSize(500, 500);
ServerFrame.setVisible(true);
ServerFrame.setLocationRelativeTo(null);
}
// Socket
ServerSocket serverSocket = null;
Socket socket = null;
BufferedReader sockIn;
PrintWriter sockOut;
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
public void listen() {
// 创建一个端口号为8888的ServerSocket
try {
serverSocket = new ServerSocket(8888);
System.out.println("Server listening on port 8888");
// 监听客户端的连接请求,当建立连接时,返回一个代表此连接的Socket对象
socket = serverSocket.accept();
if (socket == null) {
System.out.println("socket null");
System.exit(1);
}
System.out.println("accept connection from:"
+ socket.getInetAddress().getHostAddress());
// 得到输入流
sockIn = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
}
public void send() {
try {
sockOut = new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
sockOut.println("hello,i am client");
sockOut.flush();
}
}
[b]//客户端程序[/b]
package chat;
import java.awt.BorderLayout;
import java.awt.Container;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
public class Client extends JFrame {
// 建组件
private JButton jbtConnect = new JButton("Connect");
private JButton jbtExit = new JButton("Exit chat");
private JButton jbtSend = new JButton("Send");
private JLabel jlServer = new JLabel("Server");
private JLabel jlPort = new JLabel("Port");
private JTextArea jtaShowServer = new JTextArea("127.0.0.1", 1, 10);
private JTextArea jtaShowPort = new JTextArea("8888", 1, 10);
private JTextArea jtaShow = new JTextArea();
private JTextField jtfShow = new JTextField(30);
private JToolBar toolBar = new JToolBar();
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
// 构造方法
public Client() {
jbtExit.setEnabled(false);// 按钮初始化状态
toolBar.add(jbtConnect);
toolBar.add(jbtExit);
jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
jp1.add(toolBar);
jp1.add(jlServer);
jp1.add(jtaShowServer);
jp1.add(jlPort);
jp1.add(jtaShowPort);
jp2.add(jtfShow);
jp2.add(jbtSend);
JScrollPane jsp = new JScrollPane(jtaShow,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//加组件
Container c = this.getContentPane();
BorderLayout bl = new BorderLayout();
this.setLayout(bl);
c.add(jp1, bl.NORTH);
c.add(jtaShow, bl.CENTER);
c.add(jp2, bl.SOUTH);
//注册监听器
ClientActionListener actionListener = new ClientActionListener();
jbtSend.addActionListener(actionListener);
}
//内部类
class ClientActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtSend) {
String str = jtfShow.getText();
jtaShow.append(str + "\n");
}
if (e.getSource() == jbtConnect) {
jbtExit.setEnabled(true);
//jbtConnect.setEnabled(false);
}
if (e.getSource() == jbtExit) {
jbtConnect.setEnabled(true);
}
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
Client ClientFrame = new Client();
ClientFrame.setResizable(false);
ClientFrame.setTitle("Client");
ClientFrame.setSize(500, 500);
ClientFrame.setVisible(true);
ClientFrame.setLocationRelativeTo(null);
}
}
[color=FF0000][size=3][size=1]
小弟只会写这么多了,GUI已经完成了。请各位高手在我的程序上帮忙完成后面的,要求实现多个客户端通信,谢谢大家。[/size][/size][/color]