1、Java项目实训一、将服务器上的资源读到本机1、从键盘输入网址,将该网址的内容读入到本机并输出import .*;import java.io.*;import java.util.*;public class Lx1 public static void main(String args) Scanner scanner; URL url; System.out.println(输入URL资源,例如:); scanner = new Scanner(System.in); String source = scanner.nextLine(); try url = new URL(source
2、); InputStream in = url.openStream(); byte b = new byte1024; int n=-1; while(n=in.read(b)!=-1) String str = new String(b,0,n); System.out.print(str); catch(Exception exp) System.out.println(exp); 2、从键盘输入网址,将该网址的内容读入到本机并输出到一个文件urltext中。import .*;import java.io.*;import java.io.*;import java.util.*;pu
3、blic class Lx2 public static void main(String args) Scanner scanner; URL url; System.out.println(输入URL资源,例如:); scanner = new Scanner(System.in); String source = scanner.nextLine(); try url = new URL(source); InputStream in = url.openStream(); byte b = new byte1024; int n=-1; FileOutputStream out=new
4、 FileOutputStream(urltext.txt); while(n=in.read(b)!=-1) String str = new String(b,0,n); out.write(b,0,n); System.out.print(str); catch(Exception exp) System.out.println(exp); 3、用线程技术实现上述程序import .*;import java.io.*;import java.util.*;public class Example13_1 public static void main(String args) Scan
5、ner scanner; URL url; Thread readURL; Look look = new Look(); System.out.println(输入URL资源,例如:); scanner = new Scanner(System.in); String source = scanner.nextLine(); try url = new URL(source); look.setURL(url); readURL = new Thread(look); catch(Exception exp) System.out.println(exp); readURL = new Th
6、read(look); readURL.start(); import .*;import java.io.*;public class Look implements Runnable URL url; public void setURL(URL url) this.url=url; public void run() try InputStream in = url.openStream(); byte b = new byte1024; int n=-1; while(n=in.read(b)!=-1) String str = new String(b,0,n); System.ou
7、t.print(str); catch(IOException exp) 二、读取主机地址和本机地址1、调试下列读取主机名称及IP地址的程序import .*; public class Example13_2 public static void main(String args) try InetAddress address_1=InetAddress.getByName(); System.out.println(address_1.toString(); InetAddress address_2=InetAddress.getByName(166.111.222.3); Syste
8、m.out.println(address_2.toString(); catch(UnknownHostException e) System.out.println(无法找到 ); 2、从键盘输入任意主机名,读取主机地址、本机地址import .*;import java.io.*;import java.io.*;import java.util.*;public class Ex2 public static void main(String args) Scanner scanner; URL url; System.out.println(输入URL资源,例如:); scanner
9、 = new Scanner(System.in); String source = scanner.nextLine(); try InetAddress address_1=InetAddress.getByName(source); System.out.println(address_1.toString(); InetAddress address_2=InetAddress.getLocalHost(); System.out.println(address_2.toString(); catch(UnknownHostException e) System.out.println
10、(无法找到 ); 三、套接字应用1、编程实现客户端与服务端之间的应答通讯服务端代码:import java.io.*;import .*;public class Server public static void main(String args) String answer =南非,进入世界杯了,哈哈.问题真逗!; ServerSocket serverForClient=null; Socket socketOnServer=null; DataOutputStream out=null; DataInputStream in=null; try serverForClient = ne
11、w ServerSocket(2010); catch(IOException e1) System.out.println(e1); try System.out.println(等待客户呼叫); socketOnServer = serverForClient.accept(); /堵塞状态,除非有客户呼叫 out=new DataOutputStream(socketOnServer.getOutputStream(); in=new DataInputStream(socketOnServer.getInputStream(); for(int i=0;ianswer.length;i
12、+) String s=in.readUTF(); / in读取信息,堵塞状态 System.out.println(服务器收到客户的提问:+s); out.writeUTF(answeri); Thread.sleep(500); catch(Exception e) System.out.println(客户已断开+e); 服务端代码import java.io.*;import .*;public class Server public static void main(String args) String answer =南非,进入世界杯了,哈哈.问题真逗!; ServerSocke
13、t serverForClient=null; Socket socketOnServer=null; DataOutputStream out=null; DataInputStream in=null; try serverForClient = new ServerSocket(2010); catch(IOException e1) System.out.println(e1); try System.out.println(等待客户呼叫); socketOnServer = serverForClient.accept(); /堵塞状态,除非有客户呼叫 out=new DataOut
14、putStream(socketOnServer.getOutputStream(); in=new DataInputStream(socketOnServer.getInputStream(); for(int i=0;ianswer.length;i+) String s=in.readUTF(); / in读取信息,堵塞状态 System.out.println(服务器收到客户的提问:+s); out.writeUTF(answeri); Thread.sleep(500); catch(Exception e) System.out.println(客户已断开+e); 2、通过键盘输
15、入问答方式,实现客户与服务器之间的对话,任何一端回答“bye”时,结束。服务器端程序代码:import java.io.*;import .*;import java.util.*;public class Server1 public static void main(String args) String answer; ServerSocket serverForClient=null; Socket socketOnServer=null; DataOutputStream out=null; DataInputStream in=null; Scanner scanner= new
16、Scanner(System.in); try serverForClient = new ServerSocket(2010); catch(IOException e1) System.out.println(e1); try System.out.println(等待客户呼叫); socketOnServer = serverForClient.accept(); /堵塞状态,除非有客户呼叫 out=new DataOutputStream(socketOnServer.getOutputStream(); in=new DataInputStream(socketOnServer.ge
17、tInputStream(); while(true) String s=in.readUTF(); / in读取信息,堵塞状态 System.out.println(服务器收到客户的发言:+s); System.out.print(服务器发言:); answer = scanner.nextLine(); if (answer.equals(bye) break; out.writeUTF(answer); catch(Exception e) System.out.println(客户已断开+e); 客户端代码:import .*;import java.io.*;import java.
18、util.*;public class Client1 public static void main(String args) Scanner scanner= new Scanner(System.in); String mess; String s; Socket mysocket; DataInputStream in=null; DataOutputStream out=null; try mysocket=new Socket(127.0.0.1,2010); in=new DataInputStream(mysocket.getInputStream(); out=new Dat
19、aOutputStream(mysocket.getOutputStream(); while (true) System.out.print(客户发言:); mess = scanner.nextLine(); if (mess.equals(bye) break; out.writeUTF(mess); s=in.readUTF(); /in读取信息,堵塞状态 System.out.println(客户收到服务器的信息:+s); catch(Exception e) System.out.println(服务器已断开+e); 3、服务器端代码不变,客房端按以下届面设计客户端的程序代码如下:
20、public class Client2 public static void main(String args) Cc win=new Cc(); win.setBounds(100,100,460,360); win.setTitle(聊天程序客户端) import java.awt.event.*;import java.awt.*;import javax.swing.*;import .*;import java.io.*;import java.util.*;public class Cc extends JFrame JTextField inputText; JTextArea
21、 textShow; JButton button; PoliceListen listener; public Cc() init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); void init() Socket mysocket; DataInputStream in=null; DataOutputStream out=null; try mysocket=new Socket(127.0.0.1,2010); in=new DataInputStream(mysocket.getInputSt
22、ream(); out=new DataOutputStream(mysocket.getOutputStream(); setLayout(new FlowLayout(); textShow = new JTextArea(9,30); inputText = new JTextField(10); button = new JButton(发送); listener = new PoliceListen(); listener.setJTextField(inputText); listener.setJTextArea(textShow); listener.setIn(in); li
23、stener.setOut(out); inputText.addActionListener(listener); button.addActionListener(listener); add(new JScrollPane(textShow); add(inputText); add(button); catch(Exception e) System.out.println(服务器已断开+e); System.exit(-1); import java.awt.event.*;import javax.swing.*;import .*;import java.io.*;import
24、java.util.*;public class PoliceListen implements ActionListener JTextField textInput; JTextArea textShow; DataInputStream in; DataOutputStream out; public void setJTextField( JTextField text) textInput = text; public void setJTextArea(JTextArea area) textShow = area; public void setIn(DataInputStrea
25、m inn) in = inn; public void setOut(DataOutputStream out1) out = out1; public void actionPerformed(ActionEvent e) String mess; String s; try mess=textInput.getText(); out.writeUTF(mess); s=in.readUTF(); /in读取信息,堵塞状态 textShow.append(客户收到服务器的信息:+s+n); textInput.setText(); catch(Exception e1) System.out.println(服务器已断开+e1); 4、请参照客户端届面,设计服务器端程序