您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业文化 > 上机实践10Java中的网络编程
上机实践10Java中的网络编程实验1读取服务器端文件1.答案:【代码1】:url=newURL(name);【代码2】:url.getHost();【代码3】:url.getPort();【代码4】:url.getFile();【代码5】:url.openStream();2.模板代码ReadFile.javaimportjava.awt.*;importjava.awt.event.*;importjava.net.*;importjava.io.*;publicclassReadURLSource{publicstaticvoidmain(Stringargs[]){newNetWin();}}classNetWinextendsFrameimplementsActionListener,Runnable{Buttonbutton;URLurl;TextFieldtext;TextAreaarea;byteb[]=newbyte[118];Threadthread;NetWin(){text=newTextField(20);area=newTextArea(12,12);button=newButton(确定);button.addActionListener(this);thread=newThread(this);Panelp=newPanel();p.add(newLabel(输入网址:));p.add(text);p.add(button);add(area,BorderLayout.CENTER);add(p,BorderLayout.NORTH);setBounds(60,60,360,300);setVisible(true);validate();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}publicvoidactionPerformed(ActionEvente){if(!(thread.isAlive()))thread=newThread(this);try{thread.start();}catch(Exceptionee){text.setText(我正在读取+url);}}publicvoidrun(){try{intn=-1;area.setText(null);Stringname=text.getText().trim();【代码1】//使用字符串name创建url对象StringhostName=【代码2】//url调用getHost()inturlPortNumber=【代码3】//url调用getPort()StringfileName=【代码4】//url调用getFile()InputStreamin=【代码5】//url调用方法返回一个输入流area.append(\n主机:+hostName+端口:+urlPortNumber+包含的文件名字:+fileName);area.append(\n文件的内容如下:);while((n=in.read(b))!=-1){Strings=newString(b,0,n);area.append(s);}}catch(MalformedURLExceptione1){text.setText(+e1);return;}catch(IOExceptione1){text.setText(+e1);return;}}实验2使用套接字读取服务器端对象客户端模板:Client.java答案:【代码1】:newInetSocketAddress(address,4331);【代码2】:socket.connect(socketAddress);【代码3】:socket.getInputStream()【代码4】:socket.getOutputStream()服务器端模板:Server.java答案:【代码1】:newServerSocket(4331);【代码2】:server.accept();【代码3】:socket.getOutputStream()【代码4】:socket.getInputStream()2.模板代码客户端模板:Client.javaimportjava.net.*;importjava.io.*;importjava.awt.*;importjava.awt.event.*;classClientextendsFrameimplementsRunnable,ActionListener{Buttonconnection;Socketsocket=null;ObjectInputStreamin=null;ObjectOutputStreamout=null;Threadthread;publicClient(){socket=newSocket();connection=newButton(连接服务器,读取文本区对象);add(connection,BorderLayout.NORTH);connection.addActionListener(this);thread=newThread(this);setBounds(100,100,360,310);setVisible(true);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}publicvoidrun(){while(true){try{TextAreatext=(TextArea)in.readObject();add(text,BorderLayout.CENTER);validate();}catch(Exceptione){break;}}}publicvoidactionPerformed(ActionEvente){if(e.getSource()==connection){try{if(socket.isConnected()){}else{InetAddressaddress=InetAddress.getByName(127.0.0.1);InetSocketAddresssocketAddress=【代码1】//创建端口为4331、地址为//address的socketAddress【代码2】//socket建立和socketAddress的连接呼叫。in=newObjectInputStream(【代码3】);//socket返回输入流out=newObjectOutputStream(【代码4】);//socket返回输出流thread.start();}}catch(Exceptionee){}}}publicstaticvoidmain(Stringargs[]){Clientwin=newClient();}}服务器端模板:Server.javaimportjava.io.*;importjava.net.*;importjava.util.*;importjava.awt.*;publicclassServer{publicstaticvoidmain(Stringargs[]){ServerSocketserver=null;ServerThreadthread;Socketyou=null;while(true){try{server=【代码1】//创建在端口4331上负责监听的ServerSocket对象}catch(IOExceptione1){System.out.println(正在监听);}try{you=【代码2】//server返回和客户端相连接的Socket对象System.out.println(客户的地址:+you.getInetAddress());}catch(IOExceptione){System.out.println(正在等待客户);}if(you!=null){newServerThread(you).start();}else{continue;}}}}classServerThreadextendsThread{Socketsocket;ObjectInputStreamin=null;ObjectOutputStreamout=null;Strings=null;ServerThread(Sockett){socket=t;try{out=newObjectOutputStream(【代码3】);//socket返回输出流。in=newObjectInputStream(【代码4】);//socket返回输入流。}catch(IOExceptione){}}publicvoidrun(){TextAreatext=newTextArea(你好,我是服务器,12,12);try{out.writeObject(text);}catch(IOExceptione){System.out.println(客户离开);}}}实验3基于UDP的图像传输客户端模板:Client.java答案:【代码1】:newDatagramPacket(b,b.length,address,1234);【代码2】:newDatagramSocket();【代码3】:mailSend.send(data);【代码4】:mailReceive=newDatagramSocket(5678);服务器端模板:Server.java答案:【代码1】:newDatagramSocket(1234);【代码2】:pack.getAddress();【代码3】:newDatagramPacket(b,n,address,5678);【代码4】:newDatagramSocket();【代码5】:mailSend.send(data);【代码6】:newDatagramPacket(end,end.length,address,5678);【代码7】:newDatagramSocket();【代码8】:mailSend.send(data);2.模板代码客户端模板:Client.javaimportjava.net.*;importjava.awt.*;importjava.awt.event.*;importjava.io.*;classImageCanvasextendsCanvas{Imageimage=null;publicImageCanvas(){setSize(200,200);}publicvoidpaint(Graphicsg){if(image!=null)g.drawImage(image,0,0,this);}publicvoidsetImage(Imageimage){this.image=image;}}classClientextendsFrameimplementsRunnable,ActionListener{Buttonb=newButton(获取图像);ImageCanvascanvas;Client(){super(Iamaclient);setSize(320,200);setVisible(true);b.addActionListener(this);add(b,BorderLayout.NORTH);canvas=newImageCanvas();add(canvas,BorderLayout.CENTER);Threadthread=newThread(this);validate();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});
本文标题:上机实践10Java中的网络编程
链接地址:https://www.777doc.com/doc-2782147 .html