网络编程课程设计报告模版javaWord下载.docx

上传人:b****6 文档编号:21975242 上传时间:2023-02-02 格式:DOCX 页数:24 大小:75.15KB
下载 相关 举报
网络编程课程设计报告模版javaWord下载.docx_第1页
第1页 / 共24页
网络编程课程设计报告模版javaWord下载.docx_第2页
第2页 / 共24页
网络编程课程设计报告模版javaWord下载.docx_第3页
第3页 / 共24页
网络编程课程设计报告模版javaWord下载.docx_第4页
第4页 / 共24页
网络编程课程设计报告模版javaWord下载.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

网络编程课程设计报告模版javaWord下载.docx

《网络编程课程设计报告模版javaWord下载.docx》由会员分享,可在线阅读,更多相关《网络编程课程设计报告模版javaWord下载.docx(24页珍藏版)》请在冰豆网上搜索。

网络编程课程设计报告模版javaWord下载.docx

1、套接字Socket的建立、连接、关闭,客户端和服务器端的输入/输出流的连接。

Java中的多线程及线程的常用方法、Runnable接口。

基于SWING的图形用户界面设计:

布局设计、文本组件、按钮与标签组件、列表组件等。

InetAddress类。

输入/输出流:

文件、文件字节流、文件字符流、缓冲流、数据流。

2、Socketsocket=newSocket(Stringhost,intport);

客户端创建Socket对象,host是服务器端的IP地址,port是一个端口号,该对象用于连接服务器。

3、BufferedReaderbr=newBufferedReader

(newInputStreamReader(socket.getInputStream()));

创建一个使用默认大小输入缓冲区的缓冲字符输入流。

该输入流的指向是一个Reader流,Reader流将数据读入缓冲区,BufferedReader再从缓冲区中读取数据。

InputStreamReader是字节流通向字符流的桥梁:

它使用指定的charset读取字节并将其解码为字符。

getInputStream()获取字节输入流。

4、PrintStreamps=newPrintStream(socket.getOutputStream());

创建新的打印流,PrintStream为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。

它还提供其他两项功能。

与其他输出流不同,PrintStream永远不会抛出IOException;

而是,异常情况仅设置可通过checkError方法测试的内部标志。

5、Filefile=getFile();

调用getFile()函数返回一个file的的路径,提示用户输入一个路径,判断是否存在该文件,如果输入非法给予提示,重新输入。

6、ps.println(file.getName());

ps.println(file.length());

将文件名和大小发送到服务端。

7、Stringmsg=br.readLine();

if("

已存在"

.equals(msg)){}接收服务器发送回来的是否存在的结果.如果文件已存在,打印提示,客户端退出.如果不存在,准备开始上传。

8、longfinishLen=Long.parseLong(msg);

服务器端文件的长度。

9、FileInputStreamfis=newFileInputStream(file);

创建FileInputStream从文件中读取数据,OutputStreamout=socket.getOutputStream();

输出字节流,输出流接收输出字节并将这些字节写出到Socket的输出流。

10、fis.skip(finishLen);

从输入流中跳过并丢弃finishLen个字节的数据,即跳过服务端已完成的大小。

11、len=fis.read(byte[]buffer));

从此输入流中将最多b.length个字节的数据读入一个byte数组中。

12、out.write(byte[] 

b,int 

off,int 

len)将指定byte数组中从偏移量off开始的len个字节写入此输出流。

2.2关键代码解释

(对设计任务中的核心代码按照功能进行分类介绍。

服务器端:

packagechat;

import.*;

importjava.io.*;

importjava.util.*;

publicclassChatServer{

publicstaticvoidmain(String[]args)throwsException{

ServerSocketsvSocket=null;

Vectorthreads=newVector();

try{

svSocket=newServerSocket(5555);

System.out.println("

listening..."

);

}catch(Exceptionex){

ServercreateServerSocketfailed!

"

return;

}

try{

intnid=0;

while(true){

Socketsocket=svSocket.accept();

System.out.println("

acceptaclient"

ServerThreadst=newServerThread(socket,threads);

st.setID(nid++);

threads.add(st);

newThread(st).start();

for(inti=0;

i<

threads.size();

i++){

ServerThreadtemp=(ServerThread)threads.elementAt(i);

}

Listenagain..."

}

}catch(Exceptionex){

serverisdown"

}

}

classServerThreadimplementsRunnable{

privateVectorthreads;

privateSocketsocket=null;

privateDataInputStreamin=null;

privateDataOutputStreamout=null;

privateintnid;

publicServerThread(Socketsocket,Vectorthreads){

this.socket=socket;

this.threads=threads;

in=newDataInputStream(socket.getInputStream());

out=newDataOutputStream(socket.getOutputStream());

catch(Exceptionex){

publicvoidrun(){

System.out.println("

Threadisrunning"

Stringreceive=in.readUTF();

if(receive==null)return;

//下线通知

if(receive.contains("

黯然下线了"

)){

for(inti=0;

ServerThreadst=(ServerThread)threads.elementAt(i);

st.write("

***"

+receive+"

}

//上线通知

elseif(receive.contains("

上线了"

<

+getID()+"

>

:

"

+receive);

}

//作为服务器监听中

监听中"

***"

//发送消息

说"

//发送广播消息

if(receive.contains("

大家"

for(inti=0;

ServerThreadst=(ServerThread)threads.elementAt(i);

st.write("

}

//发送指定消息

else{

inttemp=receive.indexOf("

inttemp1=receive.indexOf("

//获得接收者ID号

StringtempS=receive.substring(temp+1,temp1);

inti=Integer.parseInt(tempS);

//指定接收者输出

//发送者自己也输出

st=(ServerThread)threads.elementAt(getID());

else{

ServerThreadst=(ServerThread)threads.elementAt(getID());

st.write("

threads.removeElement(this);

ex.printStackTrace();

socket.close();

ex.printStackTrace();

publicvoidwrite(Stringmsg){

synchronized(out){

try{

out.writeUTF(msg);

}catch(Exceptionex){

publicintgetID(){

returnthis.nid;

publicvoidsetID(intnid){

this.nid=nid;

服务器端的文件传送:

importjava.io.BufferedReader;

importjava.io.File;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.io.PrintStream;

import.ServerSocket;

import.Socket;

importjava.text.SimpleDateFormat;

importjava.util.Date;

importjavax.swing.JOptionPane;

publicclassfilesendServer{

publicvoidfilereceive()throwsException{

//1.创建ServerSocket,在循环中接收客户端请求,每接收到一个请求就开启新线程处理

ServerSocketserverSocket=newServerSocket(1234);

JOptionPane.showMessageDialog(null,"

服务已启动,绑定1234端口!

while(true){

Socketsocket=serverSocket.accept();

newfileServerThread(socket).start();

classfileServerThreadextendsThread{

Socketsocket;

publicfileServerThread(Socketsocket){

publicvoidrun(){

FileOutputStreamfos=null;

//3.获取输入输出流

BufferedReaderbr=newBufferedReader(newInputStreamReader(socket.getInputStream()));

PrintStreamps=newPrintStream(socket.getOutputStream());

//7.接收文件,查找是否存在.如果存在,给写一个消息给客户端,服务端线程结束.如果不存在,写消息给客户端,准备开始上传.

StringfileName=br.readLine();

longfileLen=Long.parseLong(br.readLine());

Filedir=newFile("

upload"

dir.mkdir();

Filefile=newFile(dir,fileName);

if(file.exists()&

&

file.length()==fileLen){//文件已存在,length()即为文件大小,文件不存在length()为0

ps.println("

return;

else{

ps.println(file.length());

//文件已存在,length()即为文件大小,文件不存在length()为0

//10.从Socket的输入流中读取数据,创建FileOutputStream写出到文件中

Stringtime=newSimpleDateFormat("

yyyy-MM-ddHH:

mm:

ss"

).format(newDate());

System.out.println(time+"

+(file.exists()?

开始断点续传:

:

开始上传文件:

)+file.getName());

longstart=System.currentTimeMillis();

InputStreamin=socket.getInputStream();

fos=newFileOutputStream(file,true);

//文件存在就追加,文件不存在则创建

byte[]buffer=newbyte[1024];

intlen;

while((len=in.read(buffer))!

=-1){//这个地方会堵塞,如果客服端没有关闭socket.服务器端读不到流末尾(-1)

fos.write(buffer,0,len);

if(file.length()==fileLen)//文件大小等于客户端文件大小时,代表上传完毕

break;

fos.close();

longend=System.currentTimeMillis();

time=newSimpleDateFormat("

+"

上传文件结束:

+file.getName()+"

耗时:

+(end-start)+"

毫秒"

ps.println("

上传成功"

}catch(IOExceptione){

if(fos!

=null)

try{

fos.close();

}catch(IOExceptione1){

e1.printStackTrace();

客户端的界面设计:

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

publicclassClient

{

publicstaticvoidmain(Stringargs[])

{

newChatClient();

classChatClientextendsFrameimplementsActionListener,Runnable{

privatestaticfinallongserialVersionUID=-4149839042245330513L;

publicButtonlisten,connection,sendtoall,sendtoone,exit,filesend;

publicJComboBoxemote;

publicTextFieldinputName,inputContent;

publicTextAreachatResult;

publicSocketsocket=null;

publicDataInputStreamin=null;

publicDataOutputStreamout=null;

publicThreadthread;

ChatClient()

socket=newSocket();

Boxbox1=Box.createHorizontalBox();

listen=newButton("

作为服务器监听"

connection=newButton("

连接服务器"

filesend=newButton("

发送文件"

exit=newButton("

退出"

sendtoall=newButton("

群聊"

sendtoone=newButton("

私聊"

listen.setEnabled(false);

filesend.setEnabled(false);

sendtoall.setEnabled(false);

sendtoone.setEnabled(false);

inputName=newTextField(6);

inputName.setBackground(newColor(162,231,250));

inputContent=newTextField(22);

inputContent.setBackground(newColor(162,231,250));

chatResult=newTextArea("

17,20,TextArea.SCROLLBARS_VERTICAL_ONLY);

chatResult.setBackground(newColor(162,231,250));

JLabeljlname=newJLabel("

输入昵称"

box1.add(jlname);

box1.add(inputName);

box1.add(listen);

box1.add(connection);

box1.add(filesend);

box1.add(exit);

Boxbox2=Box.createHorizontalBox();

emote=newJComboBox();

emote.setModel(newDefaultComboBoxModel(newString[]{"

表情"

"

微笑"

"

甜笑"

惊喜"

嘻嘻"

扮酷"

嘿嘿"

傻笑"

好奇"

媚眼"

鬼脸"

陶醉"

害羞"

生气"

嚷嚷"

发怒"

伤心"

高明"

菜鸟"

问号"

狂笑"

大哭"

示爱"

呻吟"

想想"

}));

emote.setE

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

当前位置:首页 > 经管营销 > 金融投资

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

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