3. 请补充下面的Socket通信程序内容:
(1)Socket通信中的服务端程序:ChatServerSocket.java
package naizi;
import java.io.*;
import java.net.*;
public class ChatServerSocket{
private ChatJFrame chatframe; //聊天室的图形用户界面
private ServerSocket server;
private Socket client;
public ChatServerSocket(int port, String name) //约定端口号、网名
{
try {
server = new ServerSocket(port);
client = server.accept();//等待接收客户端的连接申请
BufferedReader cin = new BufferedReader(new InputStreamReader(client.getInputStream()));//获得字符输入流
PrintWriter cout = new PrintWriter(client.getOutputStream(), true);//获得字符输出流
chatframe = new ChatJFrame(name," 服务端端口"+port,cout);
String aline = "";
do{
aline = cin.readLine();//从输入流接收数据(读取一行数据)
if (aline!=null && !aline.equals("bye"))
chatframe.receive(aline);
}while (aline!=null && !aline.equals("bye"));
chatframe.setWriter(null);
cin.close();
cout.close();
client.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//创建服务端Socket对象
}
public static void main(String args[])
{
new ChatServerSocket(2018,"王奕"); //约定端口号,指定网名
}
}
(2)Socket通信中的客户端程序:ChatSocket.java
package naizi;
import java.io.*;
import java.net.*;
public class ChatSocket{
private ChatJFrame chatframe; //聊天室的图形用户界面
private Socket client;
public ChatSocket(String host, int port, String name) //主机名、端口号、网名
{
try {
client = new Socket(host,port);
BufferedReader cin = new BufferedReader(new InputStreamReader(client.getInputStream()));//获得字符输入流
PrintWriter cout = new PrintWriter(client.getOutputStream(),true);//获得字符输出流
chatframe = new ChatJFrame(name,"客户端主机"+host+" 端口"+port,cout);
String aline = "";
do{
aline = cin.readLine();
if (aline!=null && !aline.equals("bye"))
chatframe.receive(aline);
}while (aline!=null && !aline.equals("bye"));
chatframe.setWriter(null);
cin.close();
cout.close();
client.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//创建客户端Socket对象向服务端发出连接请求
}
public static void main(String args[])
{
new ChatSocket("localhost",2018,"阮磊"); //指定主机和端口号,指定网名
}
}
(3)聊天框的图形界面程序:ChatJFrame.java
package naizi;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
@SuppressWarnings("serial")
public class ChatJFrame extends JFrame implements ActionListener
{
private JTextArea text_receiver; //显示对话内容的文本区
private JTextField text_sender; //输入发送内容的文本行
private PrintWriter cout; //字符输出流对象
private String name; //网名
public ChatJFrame(String name, String title, PrintWriter cout) //构造方法
{
super("聊天室 "+name+" "+title);
this.setSize(320,240);
this.setLocation(300,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.text_receiver = new JTextArea();
this.text_receiver.setEditable(false); //不可编辑
this.getContentPane().add(this.text_receiver);
JPanel panel = new JPanel();
this.getContentPane().add(panel,"South");
this.text_sender = new JTextField(12);
panel.add(this.text_sender);
this.text_sender.addActionListener(this); //注册单击事件监听器
JButton button_send = new JButton("发送");
panel.add(button_send);
button_send.addActionListener(this);
JButton button_leave = new JButton("离线");
panel.add(button_leave);
button_leave.addActionListener(this);
this.setVisible(true);
this.setWriter(cout);
this.name = name;
}
public ChatJFrame()
{
this("","",null);
}
public void setWriter(PrintWriter cout) //设置字符输出流对象
{
this.cout = cout;
}
public void receive(String message) //显示对方发来的内容
{
text_receiver.append(message+"\r\n");
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand()=="离线")
{
if (this.cout!=null)
{
this.cout.println(name+"离线");
this.cout.println("bye");
this.cout = null;
}
text_receiver.append("我离线\n");
}
else //发送
{
if (this.cout!=null)
{
this.cout.println(name+" 说:"+text_sender.getText());
text_receiver.append("我说:"+text_sender.getText()+"\n");
text_sender.setText("");
}
else
text_receiver.append("已离线,不能再发送。\n");
}
}
public static void main(String args[])
{
new ChatJFrame();
}
}
程序运行结果如下展示: