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();
}
}
程序運行結果以下展現: