局域网聊天系统论文.docx

上传人:b****6 文档编号:5031126 上传时间:2022-12-12 格式:DOCX 页数:13 大小:719.47KB
下载 相关 举报
局域网聊天系统论文.docx_第1页
第1页 / 共13页
局域网聊天系统论文.docx_第2页
第2页 / 共13页
局域网聊天系统论文.docx_第3页
第3页 / 共13页
局域网聊天系统论文.docx_第4页
第4页 / 共13页
局域网聊天系统论文.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

局域网聊天系统论文.docx

《局域网聊天系统论文.docx》由会员分享,可在线阅读,更多相关《局域网聊天系统论文.docx(13页珍藏版)》请在冰豆网上搜索。

局域网聊天系统论文.docx

局域网聊天系统论文

课程设计报告书

题目:

局域网聊天系统

课程:

TCP/IP协议课程设计

班级:

10网工

学号:

20109346008

姓名:

学期:

2013-9——2013-12

 

二0一三年十月

(一)目的与要求:

为了进一步加强同学们课堂上学习的知识,加强学生对理论知识的理解和进一步的应用,本课程最后推出了这分课程设计报告,要求同学们充分的运用课堂上学习的知识,按照自己的理解和布局设计出本系统。

(二)系统需求分析:

该系统模块实现了局域网之间的聊天,应用java语言对TCP/IP的运用,使得在同一局域网中的多个主机可以通过连接一个共同的服务器来相互聊天,可以给人们的生活工作带来一定的便捷性。

(三)系统实现步骤:

代码一(ClientServer类)

packageTest07;

importjava.io.*;

import.*;

importjava.nio.Buffer;

importjava.util.*;

publicclassChatServer{

booleanstarted=false;

ServerSocketss=null;

Listclients=newArrayList();

publicstaticvoidmain(String[]args){

newChatServer().start();

}

publicvoidstart(){

try{

ss=newServerSocket(8888);

started=true;

}catch(BindExceptione){

System.out.println("端口使用中....");

System.out.println("请关掉相关程序并重新运行服务器!

");

System.exit(0);

}catch(IOExceptione){

e.printStackTrace();

}

try{

while(started){

Sockets=ss.accept();

Clientc=newClient(s);

System.out.println("aclientconnected!

");

newThread(c).start();

clients.add(c);

}

}catch(IOExceptione){

e.printStackTrace();

}finally{

try{

ss.close();

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

classClientimplementsRunnable{

privateSockets;

privateDataInputStreamdis=null;

privateDataOutputStreamdos=null;

privatebooleanbConnected=false;

privateFileOutputStreamfos=null;

privateFileOutputStreamfos1=null;

privateBufferedReaderbr=null;

publicClient(Sockets){

this.s=s;

try{

dis=newDataInputStream(s.getInputStream());

dos=newDataOutputStream(s.getOutputStream());

fos=newFileOutputStream("E:

\\Text.txt");

bConnected=true;

}catch(IOExceptione){

e.printStackTrace();

}

}

publicvoidsend(Stringstr){

Stringss=null;

try{

dos.writeUTF(str);

fos.write(str.getBytes());

}catch(IOExceptione){

clients.remove(this);

System.out.println("对方退出了!

我从List里面去掉了!

");

}

}

publicvoidrun(){

try{

while(bConnected){

Stringstr=dis.readUTF();

System.out.println(str);

for(inti=0;i

Clientc=clients.get(i);

c.send(str);

}

}

}catch(EOFExceptione){

System.out.println("Clientclosed!

");

}catch(IOExceptione){

e.printStackTrace();

}finally{

try{

if(dis!

=null)

dis.close();

if(dos!

=null)

dos.close();

if(s!

=null){

s.close();

}

}catch(IOExceptione1){

e1.printStackTrace();

}

}

}

}

}

代码二(ChatClient类)

packageTest07;

importjava.awt.BorderLayout;

importjava.awt.Frame;

importjava.awt.Label;

importjava.awt.TextArea;

importjava.awt.TextField;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

importjava.io.DataInputStream;

importjava.io.DataOutputStream;

importjava.io.EOFException;

importjava.io.FileOutputStream;

importjava.io.IOException;

import.Socket;

import.SocketException;

import.UnknownHostException;

importjava.util.Date;

importjava.util.Scanner;

publicclassChatClientextendsFrame{

privateStringstr1;

Framef=newFrame();

Sockets=null;

Dated=null;

DataOutputStreamdos=null;

DataInputStreamdis=null;

FileOutputStreamfos=null;

privatebooleanbConnected=false;

TextFieldtfTxt=newTextField();

TextFieldtfTxt2=newTextField();

TextAreataContent=newTextArea();

ThreadtRecv=newThread(newRecvThread());

Scannerinput=newScanner(System.in);

//publicstaticvoidmain(String[]args)

//{

//newChatClient().launchFrame();

//}

publicvoidlaunchFrame(Stringstr){

str1=str;

f=newFrame(str);

f.setLocation(400,300);

f.setSize(300,300);

f.add(tfTxt,BorderLayout.SOUTH);

f.add(taContent,BorderLayout.NORTH);

pack();

f.addWindowListener(newWindowAdapter(){

@Override

publicvoidwindowClosing(WindowEventarg0){

disconnect();

System.exit(0);

}

});

tfTxt.addActionListener(newTFListener());

f.setVisible(true);

connect();

tRecv.start();

}

publicvoidconnect(){

try{

s=newSocket("127.0.0.1",8888);

dos=newDataOutputStream(s.getOutputStream());

dis=newDataInputStream(s.getInputStream());

fos=newFileOutputStream("E:

\\Text.txt");

System.out.println("connected!

");

bConnected=true;

}catch(UnknownHostExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

publicvoiddisconnect(){

try{

dos.close();

dis.close();

s.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

privateclassTFListenerimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

d=newDate();

Stringstr=str1+":

"+d+"\n"+tfTxt.getText().trim()+"\n";

//taContent.setText(str);

tfTxt.setText("");

try{

//System.out.println(s);

dos.writeUTF(str);

dos.flush();

//dos.close();

}catch(IOExceptione1){

e1.printStackTrace();

}

}

}

privateclassRecvThreadimplementsRunnable{

publicvoidrun(){

try{

while(bConnected){

Stringstr=dis.readUTF();

//System.out.println(str);

taContent.setText(taContent.getText()+str+'\n');

}

}catch(SocketExceptione){

System.out.println("退出了,bye!

");

}catch(EOFExceptione){

System.out.println("推出了,bye-bye!

");

}catch(IOExceptione){

e.printStackTrace();

}

}

}

}

 

代码三(Test类)

packageTest07;

importjava.awt.BorderLayout;

importjava.awt.Button;

importjava.awt.Frame;

importjava.awt.GridLayout;

importjava.awt.Label;

importjava.awt.Panel;

importjava.awt.TextField;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

publicclassTest{

privateStringstr;

Framef=newFrame();

LabelL=newLabel("请输入用户名");

LabelL2=newLabel("请输入密码");

Buttonb=newButton("登录");

TextFieldtf=newTextField(15);

TextFieldtf1=newTextField(15);

Panelp1=newPanel();

Panelp2=newPanel();

Panelp3=newPanel();

publicvoidmethod(){

p1.add(L);

p1.add(tf);

p2.add(L2);

p2.add(tf1);

p3.add(b);

f.setLocation(300,300);

f.setSize(300,200);

f.setLayout(newGridLayout(3,1));

f.add(p1);

f.add(p2);

f.add(p3);

f.addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEventarg0){

System.exit(0);

}

});

b.addActionListener(newActionListener(){

@Override

publicvoidactionPerformed(ActionEvente){

str=tf.getText();

if(tf1.getText().equals("123456")){

f.setVisible(false);

newChatClient().launchFrame(str);

}

}

});

f.setVisible(true);

}

publicstaticvoidmain(String[]args){

newTest().method();

}

}

本系统是通过java语言开发,通过Eclipse工具进行编写,分成了三个部分,分别是ChatClient(客户端)、ChatServer(服务器端)、Test(测试端),用到了tcp/Ip的知识,首先系统先运行起来ChatServer端,它提供了一一个端口号供客户端连接,客户端通过本地的IP地址和ChatServer提供的端口进行连接,在这里用到了Socket知识,此类实现客户端套接字(也可以就叫“套接字”)。

套接字是两台机器间通信的端点。

也用到ServerSocket知识,此类实现服务器套接字。

服务器套接字等待请求通过网络传入。

它基于该请求执行某些操作,然后可能向请求者返回结果。

当服务端启动后ServerSocket的accept()方法就开始对客户端的Socket开始监听并一直保持阻塞状态,一旦发现有客户端的Socket链接时立马执行accept()方法,之后服务端和客户端就可以进行实时通讯了,具体如下图所示:

(1)首先必须先启动ChatServer()类;

 

(2)Test()类启动客户端程序;

模仿了qq输入用户名和密码;

一个客户端已经连接等待了,

 

接下来我们以同样的步骤再启动一个客户端让它们之间;

接下来可以开始聊天了;

过程中俩客户端之间的聊天记录都会被完全的记录下来,包括说话人和说话时的时间日期;

最后点击右上方的叉叉表示此客户端退出;

(四)总结:

通过这次聊天系统的设计和编写,让我进一步的了解了TCP/IP的知识和应用,同时也让我进一步的学会编写程序和独立测试,独立排查的能力,期间也遇到很多不懂得问题和知识,通过查阅资料上网XX以及同学之间的交流学习一一的克服,对我以后的学习和工作会有深远的影响。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 军事

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1