实验5 socket网络编程Word文档下载推荐.docx

上传人:b****5 文档编号:21704011 上传时间:2023-01-31 格式:DOCX 页数:31 大小:940.82KB
下载 相关 举报
实验5 socket网络编程Word文档下载推荐.docx_第1页
第1页 / 共31页
实验5 socket网络编程Word文档下载推荐.docx_第2页
第2页 / 共31页
实验5 socket网络编程Word文档下载推荐.docx_第3页
第3页 / 共31页
实验5 socket网络编程Word文档下载推荐.docx_第4页
第4页 / 共31页
实验5 socket网络编程Word文档下载推荐.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

实验5 socket网络编程Word文档下载推荐.docx

《实验5 socket网络编程Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《实验5 socket网络编程Word文档下载推荐.docx(31页珍藏版)》请在冰豆网上搜索。

实验5 socket网络编程Word文档下载推荐.docx

使用URL类下载深圳大学首页,并统计下载得到网页文件的大小

5-2

利用Socket类和ServerSocket类,编写一个C/S程序,实现聊天功能。

5-3

利用Socket类和ServerSocket类,编写一个C/S程序,实现网络文件传输。

实验步骤:

(用文字描述实验过程,可用截图辅助说明)

(1)使用InetAddress类的方法获取本地机的名称和IP地址。

packagesocket;

import.*;

publicclassone{

publicstaticvoidmain(String[]args)

{

try

InetAddressadd=InetAddress.getLocalHost();

System.out.println(add);

}

catch(UnknownHostExceptione)

System.out.println("

error"

);

(2)使用InetAddress类的方法获取网站的IP地址,如果存在多个IP地址,要求全部返回。

InetAddressadd=InetAddress.getByName("

"

(3)

importjava.io.*;

publicclassone

publicstaticvoidmain(Stringaregs[])throwsException

{

URLurl=newURL("

URLConnectionuc=url.openConnection();

intsize=uc.getContentLength();

InputStreamin=url.openStream();

FileOutputStreamfout=newFileOutputStream(newFile("

qwe.html"

));

inta=0;

while(a>

-1)

{

a=in.read();

fout.write(a);

}

fout.close();

System.out.println("

文件大小:

+size+"

B"

}

}

(1)编写完整程序;

一个服务器端程序,一个客户端程序。

服务器端和客户端都需要打印出接受到的消息和发出的命令

客户端

packageudp;

publicclassClient

publicstaticvoidmain(Stringargs[]){

Strings=null;

Stringt="

time"

;

Stringq="

exit"

Socketmysocket;

DataInputStreamin=null;

DataOutputStreamout=null;

try{

mysocket=newSocket("

localhost"

4331);

in=newDataInputStream(mysocket.getInputStream());

out=newDataOutputStream(mysocket.getOutputStream());

服务器启动完毕"

创建客户连接"

out.writeBytes(t);

s=in.readUTF();

System.out.println(s);

Thread.sleep(500);

out.writeBytes(q);

catch(IOExceptione)

无法连接"

catch(InterruptedExceptione){}

服务端

importjava.util.*;

importjava.text.*;

publicclassSS{

publicstaticvoidmain(Stringargs[]){

ServerSockets=null;

Socketfuck=null;

DataInputStreamin=null;

DataOutputStreamout=null;

try{s=newServerSocket(4331);

catch(IOExceptione){System.out.println("

+e);

try{

fuck=s.accept();

in=newDataInputStream(fuck.getInputStream());

out=newDataOutputStream(fuck.getOutputStream());

Stringddate=newSimpleDateFormat("

yyyy-MM-ddHH:

mm:

ss"

).format(Calendar.getInstance().getTime());

out.writeUTF("

服务器当前时间为:

+ddate);

Thread.sleep(500);

Stringg="

Bye"

out.writeUTF(g);

catch(IOExceptione)

System.out.println("

catch(InterruptedExceptione){}

(()

(2)聊天室

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

publicclassChatroomextendsJFrameimplementsActionListener,Runnable,KeyListener

ImageImage;

privateTextAreata;

privateJTextFieldip;

privateJTextFieldport;

privateJButtonss_server;

privateJButtonss_client;

privateJTextFieldsend_text;

privateJTextFieldname;

privateJButtonss_send;

privateJButtonss_exit;

privateJButtonss_clear;

privateSocketskt;

publicvoidkeyReleased(KeyEventf){}

publicvoidkeyPressed(KeyEventf){

if(f.getKeyCode()==KeyEvent.VK_ENTER){

try{

PrintWriterpw=newPrintWriter(skt.getOutputStream());

Strings1=name.getText();

Strings=send_text.getText();

if(s==null)return;

Calendarca=newGregorianCalendar();

inthh=ca.get(Calendar.HOUR_OF_DAY);

intmm=ca.get(Calendar.MINUTE);

intss=ca.get(Calendar.SECOND);

Stringtime=newString(hh+"

:

+mm+"

+ss+"

"

ta.append(time+s1+"

说:

+"

\n"

+s+"

Strings3=time+s1+"

+s;

pw.println(s3);

pw.flush();

send_text.setText("

catch(Exceptione){

ta.append("

发送失败!

publicvoidkeyTyped(KeyEventf){}

publicvoidrun()

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

while(true){

Strings=br.readLine();

if(s==null)break;

ta.append(s+"

e.printStackTrace();

}

publicvoidactionPerformed(ActionEvente)

if(e.getSource()==ss_server){

doServer();

if(e.getSource()==ss_client){

doClient();

if(e.getSource()==ss_send){

doSend();

if(e.getSource()==ss_exit){

doexit();

if(e.getSource()==ss_clear){

doclear();

publicvoiddoServer()

ServerSocketserver=newServerSocket(Integer.parseInt(port.getText()));

skt=server.accept();

连接服务器成功!

newThread(this).start();

服务器启动失败!

publicvoiddoClient()

skt=newSocket(ip.getText(),Integer.parseInt(port.getText()));

连接失败!

publicvoiddoSend()

publicvoiddoclear()

ta.setText("

publicvoiddoexit()

System.exit(0);

publicChatroom()

Panela1=newPanel();

Panela2=newPanel();

Panela3=newPanel();

this.setTitle("

聊天室"

this.setBounds(200,200,600,500);

Containercc=this.getContentPane();

JPanelp1=newJPanel();

cc.add(p1,BorderLayout.NORTH);

p1.setBackground(newColor(200,200,250));

ta=newTextArea();

cc.add(ta,BorderLayout.CENTER);

JPanelp2=newJPanel();

cc.add(p2,BorderLayout.SOUTH);

p2.setBackground(newColor(250,150,200));

ta.setForeground(newColor(0,0,250));

JLabelp3=newJLabel(newImageIcon("

2010.jpg"

cc.add(p3,BorderLayout.EAST);

p1.add(newJLabel("

昵称:

name=newJTextField("

6);

p1.add(name);

name.setFont(newFont("

宋体"

Font.BOLD,14));

IP:

ip=newJTextField("

127.0.0.1"

15);

p1.add(ip);

Port:

port=newJTextField("

7777"

4);

p1.add(port);

ss_server=newJButton("

侦听"

ss_server.setForeground(newColor(150,100,250));

ss_server.setFont(newFont("

p1.add(ss_server);

ss_client=newJButton("

连接"

ss_client.setForeground(newColor(150,100,250));

ss_client.setFont(newFont("

p1.add(ss_client);

ss_clear=newJButton("

清除"

ss_clear.setForeground(newColor(150,100,250));

ss_clear.setFont(newFont("

p1.add(ss_clear);

ss_exit=newJButton("

退出"

ss_exit.setForeground(newColor(150,100,250));

ss_exit.setFont(newFont("

p1.add(ss_exit);

p2.setLayout(newBorderLayout());

p2.add(newJLabel("

发送消息:

),BorderLayout.WEST);

p2.setBackground(newColor(200,200,250));

send_text=newJTextField("

在?

p2.add(send_text,BorderLayout.CENTER);

ss_send=newJButton("

发送"

p2.add(ss_send,BorderLayout.EAST);

ss_clear.addActionListener(this);

ss_exit.addActionListener(this);

send_text.addKeyListener(this);

ss_server.addActionListener(this);

ss_client.addActionListener(this);

ss_send.addActionListener(this);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

publicstaticvoidmain(String[]args)

newChatroom().setVisible(true);

清除聊天信息

退出聊天

作为对比,左边聊天已退出

5-3利用Socket类和ServerSocket类,编写一个C/S程序,实现网络文件传输。

importjava.io.BufferedInputStream;

importjava.io.DataInputStream;

importjava.io.DataOutputStream;

importjava.io.File;

importjava.io.FileInputStream;

import.ServerSocket;

import.Socket;

publicclassNewserver{

intport=8821;

voidstart(){

Sockets=null;

try{

ServerSocketss=newServerSocket(port);

//选择进行传输的文件

StringfilePath="

D:

\\test.txt"

Filefi=newFile(filePath);

//publicSocketaccept()throws

//IOException侦听并接受到此套接字的连接。

此方法在进行连接之前一直阻塞。

s=ss.accept();

服务端运行开始okokok!

!

服务器的线程1启动,与客户端1连接成功"

DataInputStreamdis=newDataInputStream(newBufferedInputStream(s.getInputStream()));

dis.readByte();

DataInputStreamfis=newDataInputStream(newBufferedInputStream(newFileInputStream(filePath)));

DataOutputStreamps=newDataOutputStream(s.getOutputStream());

要传输的文件为:

+filePath

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

当前位置:首页 > PPT模板 > 艺术创意

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

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