您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 招标投标 > JAVA实验六计算机网络编程
实验八网络编程基础1.实验目的(1)掌握Socket通信。(2)掌握UDP通信2.实验内容实验题1使用InetAddress类的方法获取的主机的IP地址;获取本地机的名称和IP地址。程序代码为:packageNET;/***使用InetAddress类的方法获取的主机的IP地址;获取本地机的名称和IP地址。*/importjava.net.*;publicclassTest_1{publicstaticvoidmain(Stringargs[]){try{InetAddressid1=InetAddress.getByName();System.out.println(该网址的IP为:+id1.getHostAddress());InetAddressid2=InetAddress.getLocalHost();System.out.println(本地机的名称和IP分别为:+id2.getLocalHost());}catch(UnknownHostExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}运行结果为:实验题2使用URL类下载西北农林科技大学首页,并统计下载得到网页文件的大小。程序代码为:packageNET;importjava.awt.*;importjava.awt.event.*;importjava.net.*;importjava.io.*;importjavax.swing.*;publicclassExample16_1{publicstaticvoidmain(Stringargs[]){WindowURLwin=newWindowURL();win.setTitle(读取URL中的资源);}}classWindowURLextendsJFrameimplementsActionListener,Runnable{JButtonbutton;URLurl;JTextFieldtext;JEditorPaneeditPane;byteb[]=newbyte[118];Threadthread;publicWindowURL(){text=newJTextField(20);editPane=newJEditorPane();editPane.setEditable(false);button=newJButton(确定);button.addActionListener(this);thread=newThread(this);JPanelp=newJPanel();p.add(newJLabel(输入网址:));p.add(text);p.add(button);JScrollPanescroll=newJScrollPane(editPane);add(scroll,BorderLayout.CENTER);add(p,BorderLayout.NORTH);setBounds(160,60,420,300);setVisible(true);validate();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicvoidactionPerformed(ActionEvente){if(!(thread.isAlive()))thread=newThread(this);try{thread.start();}catch(Exceptionee){text.setText(我正在读取+url);}}publicvoidrun(){try{intn=-1;editPane.setText(null);url=newURL(text.getText().trim());Filef=newFile(d:/java/url.txt);FileOutputStreamout=newFileOutputStream(f);InputStreamin=url.openStream();while((n=in.read(b))!=-1){Strings=newString(b,0,n);editPane.setPage(url);out.write(b);}out.close();System.out.println(f.length()/1024.0);}catch(Exceptione1){text.setText(+e1);return;}}}运行结果为:实验题3利用Socket类和ServerSocket类编写一个C/S程序,实现C/S通信。客户端向服务器端发送Time命令,服务器端接受到该字符串后将服务器端当前时间返回给客户端;客户端向服务器端发送Exit命令,服务器端向客户端返回“Bye”后退出。[基本要求]编写完整程序;两人一组,一个作为服务器端,另一人作为客户端。服务器端和客户端都需要打印出接受到的消息和发出的命令。程序代码为:Client.java:packageNET;importjava.net.*;importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassClient{publicstaticvoidmain(Stringargs[]){newWindowClient();}}classWindowClientextendsJFrameimplementsRunnable,ActionListener{JButtonconnection,send;JTextFieldinputText;JTextAreashowResult;Socketsocket=null;DataInputStreamin=null;DataOutputStreamout=null;Threadthread;WindowClient(){socket=newSocket();connection=newJButton(连接服务器);send=newJButton(发送);send.setEnabled(false);inputText=newJTextField(6);showResult=newJTextArea();add(connection,BorderLayout.NORTH);JPanelpSouth=newJPanel();pSouth.add(newJLabel(输入Time或Exit));pSouth.add(inputText);pSouth.add(send);add(newJScrollPane(showResult),BorderLayout.CENTER);add(pSouth,BorderLayout.SOUTH);connection.addActionListener(this);send.addActionListener(this);thread=newThread(this);setBounds(10,30,460,400);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicvoidactionPerformed(ActionEvente){if(e.getSource()==connection){try{//请求和服务器建立套接字连接:if(socket.isConnected()){}else{InetAddressaddress=InetAddress.getByName(127.0.0.1);InetSocketAddresssocketAddress=newInetSocketAddress(address,4331);socket.connect(socketAddress);in=newDataInputStream(socket.getInputStream());out=newDataOutputStream(socket.getOutputStream());send.setEnabled(true);if(!(thread.isAlive()))thread=newThread(this);thread.start();}}catch(IOExceptionee){System.out.println(ee);socket=newSocket();}}if(e.getSource()==send){Strings=inputText.getText();try{out.writeUTF(s);}catch(IOExceptione1){}}}publicvoidrun(){Strings=null;while(true){try{s=in.readUTF();showResult.append(\n+s);}catch(IOExceptione){showResult.setText(与服务器已断开+e);socket=newSocket();break;}}}}Server.javapackageNET;importjava.io.*;importjava.net.*;importjava.text.SimpleDateFormat;importjava.util.*;publicclassServer{publicstaticvoidmain(Stringargs[]){ServerSocketserver=null;ServerThreadthread;Socketyou=null;while(true){try{server=newServerSocket(4331);}catch(IOExceptione1){System.out.println(正在监听);//ServerSocket对象不能重复创建}try{System.out.println(等待客户呼叫);you=server.accept();System.out.println(客户的地址:+you.getInetAddress());}catch(IOExceptione){System.out.println(正在等待客户);}if(you!=null){newServerThread(you).start();//为每个客户启动一个专门的线程}}}}classServerThreadextendsThread{Socketsocket;DataOutputStreamout=null;DataInputStreamin=null;Strings=null;ServerThread(Sockett){socket=t;try{out=newDataOutputStream(socket.getOutputStream());in=newDataInputStream(socket.getInputStream());}catch(IOExceptione){}}publicvoidrun(){while(true){Date时间;SimpleDateFormatdateformate=newSimpleDateFormat(hh:mm:ss);try{Strings=in.readUTF();时间=newDate();if(s.equals(Time)){out.writeUTF(dateformate.format(时间));}elseif(s.equals(Exit)){out.writeUTF(Bye);}}catch(IOExceptione){System.out.println(客户离开);return;}}}}运行结果为:二、
本文标题:JAVA实验六计算机网络编程
链接地址:https://www.777doc.com/doc-3704479 .html