局域网聊天系统论文
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
课程设计报告书
题目:局域网聊天系统
课程:TCP/IP协议课程设计
班级:10网工
学号:20109346008
姓名:
学期:2013-9——2013-12
二0一三年十月
(一)目的与要求:
为了进一步加强同学们课堂上学习的知识,加强学生对理论知识的理解和进一步的应用,本课程最后推出了这分课程设计报告,要求同学们充分的运用课堂上学习的知识,按照自己的理解和布局设计出本系统。
(二)系统需求分析:
该系统模块实现了局域网之间的聊天,应用java语言对TCP/IP的运用,使得在同一局域网中的多个主机可以通过连接一个共同的服务器来相互聊天,可以给人们的生活工作带来一定的便捷性。
(三)系统实现步骤:
代码一(ClientServer类)
package Test07;
import java.io.*;
import .*;
import java.nio.Buffer;
import java.util.*;
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while (started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
private FileOutputStream fos = null;
private FileOutputStream fos1 = null;
private BufferedReader br = null;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
fos = new FileOutputStream("E:\\Text.txt");
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
String ss = null;
try {
dos.writeUTF(str);
fos.write(str.getBytes());
} catch (IOException e) {
clients.remove(this);
System.out.println("对方退出了!我从List里面去掉了!");
}
}
public void run() {
try {
while (bConnected) {
String str = dis.readUTF();
System.out.println(str);
for (int i = 0; i < clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
}
}
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null)
dis.close();
if (dos != null)
dos.close();
if (s != null) {
s.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
代码二(ChatClient类)package Test07;
import java.awt.BorderLayout;
import java.awt.Frame;
import bel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileOutputStream;
import java.io.IOException;
import .Socket;
import .SocketException;
import .UnknownHostException;
import java.util.Date;
import java.util.Scanner;
public class ChatClient extends Frame {
private String str1;
Frame f = new Frame();
Socket s = null;
Date d = null;
DataOutputStream dos = null;
DataInputStream dis = null;
FileOutputStream fos = null;
private boolean bConnected = false;
TextField tfTxt = new TextField();
TextField tfTxt2 = new TextField();
TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
Scanner input = new Scanner(System.in);
// public static void main(String[] args)
// {
// new ChatClient().launchFrame();
// }
public void launchFrame(String str) {
str1 = str;
f = new Frame(str);
f.setLocation(400, 300);
f.setSize(300, 300);
f.add(tfTxt, BorderLayout.SOUTH);
f.add(taContent, BorderLayout.NORTH);
pack();
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
f.setVisible(true);
connect();
tRecv.start();。