Java聊天系统实训报告.docx
《Java聊天系统实训报告.docx》由会员分享,可在线阅读,更多相关《Java聊天系统实训报告.docx(28页珍藏版)》请在冰点文库上搜索。
Java聊天系统实训报告
郑州轻工业学院
实训报告
实训名称:
即时聊天系统
姓名:
王路超
院(系):
软件学院
专业班级:
java01
学号:
3
指导教师:
刘育熙胡春辉
成绩:
时间:
2014年06月3日至2014年06月20日
一、学生信息
专业软件技术学号3姓名王路超
二、用户需求及约束条件
完成一个即时聊天程序,使用服务器、客户端(C/S)架构,实现多人之间和个体对个体的聊天。
服务器端使用线程非阻塞方式。
使用数据库保存用户信息,允许其注册修改。
当用户不在线时,支持信息留言
三、实训目的:
1、理解C/S模式的软件系统结构,掌握网络编程的基本概念;
2、了解Java的多线程机制,掌握Java多线程技术的应用;
3、熟练掌握基于TCP协议的Socket编程;
4、熟练掌握基于UDP协议的Socket编程;
5、了解Socket编程的协议约定以及信息交互方法;
四、实训要求:
使用JBuilder提供的组件及网络包,开发一个实用的C/S模式聊天室系统。
(1)功能要求
要求该系统由客户端及服务器端两部分组成。
(2)系统界面要求:
要求系统具有友好的用户界面,界面简洁、操作方便;
本程序客户端主要完成了:
1.客户端发送信息可以广播客户。
2.广播客户,则将信息转发到所有客户端。
服务器主要完成了:
客户-服务器之间信息的交互。
例如hello发送信息到服务器,服务器将信息转发到所有与之连接的客户(hello、boy),通过这种形式,就可以实现客户之间的信息广播。
本程序可以实现发送信息广播,只要进入客户端都可以看到消息,本程序采用了流的概念,java多线程,Socket应用
程序中用到的代码如下
启动端口如下:
package聊天系统;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
import.Socket;
importjava.util.logging.Level;
importjava.util.logging.Logger;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclass聊天系统extendsJFrameimplementsActionListener,Runnable
{
privateJFrameapp;
privateJPanela1,a2,a3,a4,a5;
privateJLabel输入昵称;
privateJTextFieldl1,l2;
privateJTextAreatextArea;
privateJButtonb1,b2,b3;
protectedSocketsocket;
protectedThreadthread;
protectedDataInputStreamin;
protectedDataOutputStreamout;
protectedStringnickname;
public聊天系统()
{
app=newJFrame("");
app.setSize(400,300);
app.setLocation(500,500);
app.setLayout(newBorderLayout());
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1=newJPanel();
a1.setBackground(Color.yellow);
a2=newJPanel();
a2=newJPanel();
a4=newJPanel();
a4.setBackground(Color.red);
a5=newJPanel();
输入昵称=newJLabel("输入昵称");
l1=newJTextField(12);
b1=newJButton("确定");
a1.add(输入昵称);
a1.add(l1);
a1.add(b1);
textArea=newJTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setFont(newFont("幼圆",Font.PLAIN,16));
l2=newJTextField(20);
a4.add(l2);
b2=newJButton("发送");
b2.setEnabled(false);
a4.add(b2);
b3=newJButton("离线");
b3.setEnabled(false);
a4.add(b3);
app.add(a1,BorderLayout.NORTH);
app.add(newJScrollPane(textArea),BorderLayout.CENTER);
app.add(a4,BorderLayout.SOUTH);
app.setVisible(true);
}
publicstaticvoidmain(String[]args)
{
new聊天系统();
}
@Override
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==b1)
{
b1Button();
}
if(e.getSource()==b2)
{
b2Button();
}
if(e.getSource()==b3)
{
b3Button();
}
}
privatevoidb1Button()//确定
{
}
privatevoidb2Button()//发送
{
try
{
out.writeUTF(nickname+"说:
"+l2.getText());
textArea.append(nickname+"说:
"+l2.getText()+"\n");
l1.setText("");
}
catch(IOExceptione)
{
e.printStackTrace();
}
}
privatevoidb3Button()//离线
{
try
{
out.writeUTF("bye");
b2.setEnabled(false);
b3.setEnabled(false);
textArea.append("连接断开!
");
socketClosing();
}
catch(IOExceptionioe)
{
ioe.printStackTrace();
}
}
publicvoidrun()
{
System.out.println("线程启动");
Stringi="";
while(true)
{
try
{
i=in.readUTF();
if(i.equals("bye"))
{
b2.setEnabled(false);
b3.setEnabled(false);
textArea.append("连接断开!
");
socketClosing();
break;
}
else
{
textArea.append(i+"\n");
}
}
catch(IOExceptionex)
{
socketClosing();
System.out.println("有异常,连接中断!
");
break;
}
}
System.out.println("线程中断!
");
}
privatevoidsocketClosing()
{
try
{
in.close();
out.close();
socket.close();
}
catch(IOExceptionex)
{
System.out.println("关闭socket和流时,发生异常");
}
}
}
客户端代码:
package聊天系统;
//shuoyoujiekou
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
import.Socket;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclass客户端extendsJFrameimplementsActionListener,Runnable//窗口工具
{
privateJFrameapp;
privateJPanelp1,p2,p3,p4,p5;
privateJLabel输入昵称;//面板
privateJTextFieldl1,l2;
privateJTextAreatextArea;
privateJButtonb1,b2,b3;
protectedSocketsocket;
protectedThreadthread;
protectedDataInputStreamin;
protectedDataOutputStreamout;
protectedStringnickname;
public客户端()
{
app=newJFrame("客户端");
app.setSize(400,300);//设置框架大小
app.setLocation(500,500);//设置位置
app.setLayout(newBorderLayout());
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭按钮关闭窗口
p1=newJPanel();
p1.setBackground(Color.red);
p2=newJPanel();
p3=newJPanel();
p4=newJPanel();
p4.setBackground(Color.yellow);
p5=newJPanel();
输入昵称=newJLabel("昵称");
输入昵称.setFont(newFont("宋体",Font.PLAIN,16));
输入昵称.setForeground(Color.red);
l1=newJTextField(12);
b1=newJButton("确定");
b1.setFont(newFont("宋体",Font.PLAIN,16));
b1.setForeground(Color.red);
b1.addActionListener(this);
p1.add(输入昵称);
p1.add(l1);
p1.add(b1);
textArea=newJTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setFont(newFont("宋体",Font.PLAIN,16));
textArea.setForeground(Color.red);
l2=newJTextField(20);
p4.add(l2);
b2=newJButton("发送");
b2.setFont(newFont("宋体",Font.PLAIN,16));
b2.setForeground(Color.red);
b2.addActionListener(this);
b2.setEnabled(false);
p4.add(b2);
b3=newJButton("离线");
b3.setFont(newFont("宋体",Font.PLAIN,16));
b3.setForeground(Color.red);
b3.addActionListener(this);
b3.setEnabled(false);
p4.add(b3);
app.add(p1,BorderLayout.NORTH);
app.add(newJScrollPane(textArea),BorderLayout.CENTER);
app.add(p4,BorderLayout.SOUTH);
app.setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==b1)
{
b1Button();
}
if(e.getSource()==b2)
{
b2Button();
}
if(e.getSource()==b3)
{
b3Button();
}
}
privatevoidb1Button()//确定
{
textArea.append("连接服务器!
···\n");
try
{
nickname=l1.getText();
textArea.append(nickname+"连接服务器···\n");
socket=newSocket("127.0.0.1",8888);
in=newDataInputStream(socket.getInputStream());
out=newDataOutputStream(socket.getOutputStream());
thread=newThread(this);
thread.start();
b1.setEnabled(false);
b2.setEnabled(true);
b3.setEnabled(true);
out.writeUTF(nickname+"说:
在干嘛呢");
}
catch(IOExceptione)
{
e.printStackTrace();
}
}
privatevoidb2Button()//发送
{
try
{
out.writeUTF(nickname+"说:
"+l2.getText());
textArea.append(nickname+"说:
"+l2.getText()+"\n");
l1.setText("");
}
catch(IOExceptione)
{
e.printStackTrace();
}
}
privatevoidb3Button()//离线
{
try
{
out.writeUTF("bye");
b2.setEnabled(false);
b3.setEnabled(false);
textArea.append("连接断开!
");
socketClosing();
}
catch(IOExceptionioe)
{
ioe.printStackTrace();
}
}
publicvoidrun()
{
System.out.println("线程启动");
Stringi="";
while(true)
{
try
{
i=in.readUTF();
if(i.equals("bye"))
{
b2.setEnabled(false);
b3.setEnabled(false);
textArea.append("连接断开!
");
socketClosing();
break;
}
else
{
textArea.append(i+"\n");
}
}
catch(IOExceptionex)
{
socketClosing();
System.out.println("有异常,连接中断!
");
break;
}
}
System.out.println("线程中断!
");
}
privatevoidsocketClosing()
{
try
{
in.close();
out.close();
socket.close();
}
catch(IOExceptionex)
{
System.out.println("关闭socket和流时,发生异常");
}
}
publicstaticvoidmain(String[]args)
{
new客户端();
}
}
服务器代码如下:
package聊天系统;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
import.ServerSocket;
import.Socket;
importjava.util.logging.Level;
importjava.util.logging.Logger;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclass服务端extendsJFrameimplementsActionListener,Runnable
{//shiyoudeduixiang
privateJFrameapp;
privateJPanela1,a2,a3,a4,a5;
privateJLabel输入昵称;
privateJTextFieldl1,l2;
privateJTextAreatextArea;
privateJButtonb1,b2,b3;
//shoubaohude
protectedSocketsocket;
protectedThreadthread;
protectedDataInputStreama;
protectedDataOutputStreamout;
protectedStringnickname;
public服务端()
{
app=newJFrame("服务器");
app.setSize(400,300);
app.setLocation(500,500);
app.setLayout(newBorderLayout());
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1=newJPanel();
a1.setBackground(Color.yellow);
a2=newJPanel();
a3=newJPanel();
a4=newJPanel();
a4.setBackground(Color.red);
a5=newJPanel();
输入昵称=newJLabel("输入昵称");
输入昵称.setFont(newFont("宋体",Font.PLAIN,16));
输入昵称.setForeground(Color.yellow);
l1=newJTextField(12);
b1=n