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();
}
}

程序运行结果如下展示:

最新文章

  1. secureCRT The remote system refused the connection.
  2. 踩坑所引发出的appendChild方法的介绍
  3. oracle DBMS_LOCK.SLEEP()的使用
  4. Azure ARM (3) ARM支持的服务类型
  5. 昨天在公司加班,上午好像就是弄一个ftp的linux服务问题
  6. 修改git commit默认触发的编辑器
  7. [Environment Build] Win10下Appach配置
  8. oracle学习 十二 使用.net程序调用带返回值的存储过程(持续更新)
  9. Yii中CDbCriteria常用方法
  10. JAVA HashMap与HashTable 区别
  11. 戏说Java多线程
  12. JavaScript 奇技淫巧
  13. ACM第四次积分赛
  14. ansible理解
  15. Swift GCD的使用1
  16. kubernetes1.5.2 DNS
  17. groupID和artifactID填什么
  18. Windows系统,Tensorflow的Tensorboard工具细节问题
  19. eclipse经常出现——未响应!!!
  20. hihoCoder.1513.小Hi的烦恼(bitset 五维偏序)

热门文章

  1. html5标签整理
  2. The 3n + 1 problem UVA - 100
  3. js 设计模式——状态模式
  4. Mongodb最基础入门教程
  5. js学习重点难点知识总结 (巩固闭包、原型、原型链)
  6. .NET CORE 怎么样从控制台中读取输入流
  7. unity之初级必备知识
  8. CodeForces 909F
  9. MySql优化相关概念的理解笔记
  10. [code] python+selenium实现打开一个网页