JAVA上机实验9
实验名称:
网络编程
实验目的:学会使用URL对象;
学会使用套接字读取服务器端的对象;
掌握DatagramSocket类的使用。
实验内容:
1、创建一个URL对象,然后让URL对象返回输入流,通过该输入流读取URL所包含的资源文件。
程序模版
请按照模版要求,将【代码】替换为程序代码。
ReadFile.java
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ReadURLSource
{ public static void
main(String args[])
{ new
NetWin();
}
}
class NetWin extends Frame implements
ActionListener,Runnable
{ Button
button;
URL
url;
TextField text;
TextArea area;
byte b[]=new byte[118];
Thread thread;
NetWin()
{ text=new
TextField(20);
area=new TextArea(12,12);
button=new Button("确定");
button.addActionListener(this);
thread=new Thread(this);
Panel p=new Panel();
p.add(new Label("输入网址:"));
p.add(text);
p.add(button);
add(area,BorderLayout.CENTER);
add(p,BorderLayout.NORTH);
setBounds(60,60,360,300);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(!(thread.isAlive()))
thread=new Thread(this);
try{
thread.start();
}
catch(Exception ee)
{ text.setText("我正在读取"+url);
}
}
public void
run()
{
try {
int n=-1;
area.setText(null);
String name=text.getText().trim();
【代码1】 //使用字符串name创建url对象
String hostName=【代码2】 //url调用getHost()
int urlPortNumber=【代码3】 //url调用getPort()
String fileName=【代码4】 //url调用getFile()
InputStream in=【代码5】 //url调用方法返回一个输入流
area.append("\n主机:"+hostName+"端口:"+urlPortNumber+
"包含的文件名字:"+fileName);
area.append("\n文件的内容如下:");
while((n=in.read(b))!=-1)
{
String s=new String(b,0,n);
area.append(s);
}
}
catch(MalformedURLException e1)
{
text.setText(""+e1);
return;
}
catch(IOException e1)
{
text.setText(""+e1);
return;
}
}
2、客户端将服务器端的文本区对象读取到客户端,并添加到窗口中。首先将服务器端的程序编译通过,并运行,等待客户呼叫。
程序模版
请按照模版要求,将【代码】替换为程序代码
客户端模板:Client.java
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class Client extends Frame implements
Runnable,ActionListener
{ Button
connection;
Socket socket=null;
ObjectInputStream in=null;
ObjectOutputStream out=null;
Thread thread;
public
Client()
{
socket=new Socket();
connection=new Button("连接服务器,读取文本区对象");
add(connection,BorderLayout.NORTH);
connection.addActionListener(this);
thread = new Thread(this);
setBounds(100,100,360,310);
setVisible(true);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void run()
{
while(true)
{
try{
TextArea text=(TextArea)in.readObject();
add(text,BorderLayout.CENTER);
validate();
}
catch(Exception e)
{ break;
}
}
}
public void
actionPerformed(ActionEvent e)
{
if(e.getSource()==connection)
{
try
{
if(socket.isConnected())
{
}
else
{ InetAddress
address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=【代码1】//创建端口为4331、地址为
//address的socketAddress
【代码2】
//socket建立和socketAddress的连接呼叫。
in =new ObjectInputStream(【代码3】);
//socket返回输入流
out = new ObjectOutputStream(【代码4】);
//socket返回输出流
thread.start();
}
}
catch (Exception ee){}
}
}
public static void
main(String args[])
{
Client win=new Client();
}
}
服务器端模板:Server.java
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
public class Server
{ public static void
main(String args[])
{
ServerSocket server=null;
ServerThread thread;
Socket you=null;
while(true)
{
try{
server=【代码1】//创建在端口4331上负责监听的 ServerSocket对象
}
catch(IOException e1)
{
System.out.println("正在监听");
}
try{
you=【代码2】 //
server返回和客户端相连接的Socket对象
System.out.println("客户的地址:"+you.getInetAddress());
}
catch (IOException e)
{
System.out.println("正在等待客户");
}
if(you!=null)
{ new
ServerThread(you).start();
}
else{
continue;
}
}
}
}
class ServerThread extends Thread
{ Socket
socket;
ObjectInputStream in=null;
ObjectOutputStream out=null;
String s=null;
ServerThread(Socket t)
{
socket=t;
try {
out=new ObjectOutputStream(【代码3】);
//socket返回输出流。
in=new ObjectInputStream(【代码4】);
//socket返回输入流。
}
catch (IOException e)
{}
}
public void run()
{ TextArea
text=new TextArea("你好,我是服务器",12,12);
try{
out.writeObject(text);
}
catch (IOException e)
{
System.out.println("客户离开");
}
}
}
3、编写客户/服务器程序,客户端使用DatagramSocket对象将数据包发送到服务器,请求获取服务器端的图像。服务器端将图像文件包装成数据包,并使用DatagramSocket对象将该数据包发送到客户端。首先将服务器端的程序编译通过,并运行起来,等待客户的请求。
程序模版
请按照模版要求,将【代码】替换为程序代码
客户端模板:Client.java
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class ImageCanvas extends Canvas
{ Image
image=null;
public ImageCanvas()
{
setSize(200,200);
}
public void paint(Graphics g)
{
if(image!=null)
g.drawImage(image,0,0,this);
}
public void setImage(Image image)
{
this.image=image;
}
}
class Client extends Frame implements
Runnable,ActionListener
{ Button b=new
Button("获取图像");
ImageCanvas canvas;
Client()
{ super("I
am a client");
setSize(320,200);
setVisible(true);
b.addActionListener(this);
add(b,BorderLayout.NORTH);
canvas=new ImageCanvas();
add(canvas,BorderLayout.CENTER);
Thread thread=new Thread(this);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
thread.start();
}
public void actionPerformed(ActionEvent event)
{ byte
b[]="请发图像".trim().getBytes();
try{
InetAddress address=InetAddress.getByName("127.0.0.1");
DatagramPacket data=【代码1】 //创建数据包,该数据包的目标地址和端口分别是
//address和1234,其中的数据为数组b中的全部字节。
DatagramSocket mailSend=【代码2】 //创建负责发送数据的DatagramSocket对象。
【代码3】
// mailSend发送数据data。
}
catch(Exception e){}
}
public void
run()
{
DatagramPacket pack=null;
DatagramSocket mailReceive=null;
byte b[]=new byte[8192];
ByteArrayOutputStream out=new ByteArrayOutputStream();
try{
pack=new DatagramPacket(b,b.length);
【代码4】 //创建在端口5678负责收取数据包的DatagramSocket对象。
}
catch(Exception e){}
try{
while(true)
{
mailReceive.receive(pack);
String message=new
String(pack.getData(),0,pack.getLength());
if(message.startsWith("end"))
{
break;
}
out.write(pack.getData(),0,pack.getLength());
}
byte imagebyte[]=out.toByteArray();
out.close();
Toolkit tool=getToolkit();
Image image=tool.createImage(imagebyte);
canvas.setImage(image);
canvas.repaint();
validate();
}
catch(IOException e){}
}
public static void
main(String args[])
{
new Client();
}
}
服务器端模板:Server.java
import java.net.*;
import java.io.*;
public class Server
{
public static void main(String args[])
{
DatagramPacket pack=null;
DatagramSocket mailReceive=null;
ServerThread thread;
byte b[]=new byte[8192];
InetAddress address=null;
pack=new DatagramPacket(b,b.length);
while(true)
{
try{
mailReceive=【代码1】//创建在端口1234负责收取数据包的
//DatagramSocket对象。
}
catch(IOException e1)
{
System.out.println("正在等待");
}
try{
mailReceive.receive(pack);
address=【代码2】 //pack返回InetAddress对象。
System.out.println("客户的地址:"+address);
}
catch (IOException e) { }
if(address!=null)
{ new
ServerThread(address).start();
}
else
{
continue;
}
}
}
}
class ServerThread extends Thread
{ InetAddress
address;
DataOutputStream out=null;
DataInputStream in=null;
String s=null;
ServerThread(InetAddress address)
{
this.address=address;
}
public void run()
{
FileInputStream in;
byte b[]=new byte[8192];
try{
in=new
FileInputStream ("a.jpg");
int n=-1;
while((n=in.read(b))!=-1)
{
DatagramPacket data=【代码3】 //创建数据包,目标地址和端口分别是
//address和5678,其中的数据为数组b中的前n个字节
DatagramSocket mailSend=【代码4】 //创建发送数据的DatagramSocket对象
【代码5】
// mailSend发送数据data
}
in.close();
byte end[]="end".getBytes();
DatagramPacket data=【代码6】 //创建数据包,目标地址和端口分别是
//address和5678,其中的数据为数组end中的全部字节
DatagramSocket mailSend=【代码7】 //创建负责发送数据的DatagramSocket对象
【代码8】
//
mailSend发送数据data
}
catch(Exception e){}
}}
加载中,请稍候......