java笔记十

 
 
 
 
 
java 代码
  1. package Lesson10;   
  2. import java.net.*;   
  3. import java.io.*;   
  4. public class Lesson10 {   
  5.     public static void main(String[] args) {   
  6.         if(args.length>0)   
  7.             server();   
  8.         else client();   
  9.     }   
  10.     public static void server() {   
  11.         try {   
  12.             ServerSocket ss = new ServerSocket(6001);      
  13.             Socket s = ss.accept();   
  14.             OutputStream os = s.getOutputStream();   
  15.             InputStream is = s.getInputStream();   
  16.             os.write("welcome!".getBytes());   
  17.             byte[] buf = new byte[100];   
  18.             int len = is.read(buf);//输入流写到buf中   
  19.             System.out.println(new String(buf,0,len));   
  20.             os.close();   
  21.             is.close();   
  22.             s.close();   
  23.             ss.close();   
  24.         }   
  25.         catch (Exception e) {   
  26.             e.printStackTrace();   
  27.         }   
  28.     }   
  29.        
  30.     public static void client() {   
  31.         try{   
  32.             Socket s = new Socket(InetAddress.getByName(null),6001);   
  33.             InputStream is = s.getInputStream();   
  34.             OutputStream os = s.getOutputStream();   
  35.             byte[] buf = new byte[100];   
  36.             int len = is.read(buf);   
  37.             System.out.println(new String(buf,0,len));   
  38.             os.write("zhangsan online".getBytes());   
  39.             is.close();   
  40.             os.close();   
  41.             s.close();   
  42.         }   
  43.         catch(Exception e) {   
  44.             e.printStackTrace();           
  45.         }   
  46.     }   
  47. }   
  48.   
  49.   
  50. //运行参数配置:Run ->  Run... ->选择好Project 和 Main Class -> Name 为server     
  51. //(X)Arguments 随便输入一个字符(串)  -> Name  :client  ->运行server 运行client   

 

 

java 代码
  1. package lesson10;   
  2.   
  3. import java.net.*;   
  4. import java.io.*;   
  5. public class Lesson10 extends Thread   
  6. {   
  7.   public static void main(String[] args)   
  8.   {   
  9.     if(args.length>0)   
  10.       recv();   
  11.     else  
  12.       send();   
  13.   }   
  14.   public static void recv()   
  15.   {   
  16.     try {   
  17.       DatagramSocket ds=new DatagramSocket(6000);   
  18.       byte[] buf=new byte[100];   
  19.       DatagramPacket dp=new DatagramPacket(buf,100);   
  20.       ds.receive(dp);   
  21.       System.out.println(new String(buf,0,dp.getLength()));   
  22.       String str="Welcome you!";   
  23.       DatagramPacket dpSend=new DatagramPacket(str.getBytes(),str.length(),   
  24.                                            dp.getAddress(),dp.getPort());   
  25.       ds.send(dpSend);   
  26.       ds.close();   
  27.     }   
  28.     catch (Exception ex) {   
  29.       ex.printStackTrace();   
  30.     }   
  31.   }   
  32.   public static void send()   
  33.   {   
  34.     try {   
  35.       DatagramSocket ds=new DatagramSocket();   
  36.       String str="Hello,this is zhangsan";   
  37.       DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),   
  38.                                            InetAddress.getByName("localhost"),   
  39.                                            6000);   
  40.       ds.send(dp);   
  41.       byte[] buf=new byte[100];   
  42.       DatagramPacket dpRecv=new DatagramPacket(buf,100);   
  43.       ds.receive(dpRecv);   
  44.       System.out.println(new String(buf,0,dpRecv.getLength()));   
  45.       ds.close();   
  46.     }   
  47.     catch (Exception ex) {   
  48.       ex.printStackTrace();   
  49.     }   
  50.   }  

 

java 代码
  1. package Lesson10;   
  2.   
  3. import java.net.*;   
  4. import javax.swing.*;   
  5. import java.awt.event.*;   
  6. import java.io.*;   
  7. public class Download   
  8. {   
  9.   public static void main(String[] args)   
  10.   {   
  11.     JFrame jf=new JFrame("维新下载程序");   
  12.     jf.setSize(600,400);   
  13.     jf.setLocation(100,100);   
  14.     JPanel p=new JPanel();   
  15.     JLabel l=new JLabel("Please input URL:");   
  16.     final JTextField tf=new JTextField(30);   
  17.     p.add(l);   
  18.     p.add(tf);   
  19.     jf.getContentPane().add(p,"North");   
  20.     final JTextArea ta=new JTextArea();   
  21.     jf.getContentPane().add(ta,"Center");   
  22.     JButton btn=new JButton("Download");   
  23.     jf.getContentPane().add(btn,"South");   
  24.     btn.addActionListener(new ActionListener() {   
  25.           public void actionPerformed(ActionEvent e) {   
  26.             String str=tf.getText();   
  27.             try {   
  28.               URL url=new URL(str);   
  29.               URLConnection urlConn=url.openConnection();   
  30.               String line=System.getProperty("line.separator");//行分隔符   
  31.               ta.append("Host: "+url.getHost());   
  32.               ta.append(line);   
  33.               ta.append("Port: "+url.getDefaultPort());   
  34.               ta.append(line);   
  35.               ta.append("ContentType: "+urlConn.getContentType());   
  36.               ta.append(line);   
  37.               ta.append("ContentLength: "+urlConn.getContentLength());   
  38.               InputStream is=urlConn.getInputStream();   
  39.               //InputStreamReader isr=new InputStreamReader(is);//字节流转化为字符流   
  40.               //BufferedReader br=new BufferedReader(isr);//字符流方式读取   
  41.               FileOutputStream fos=new FileOutputStream("1.html");   
  42.               //String strLine;   
  43.               //while((strLine=br.readLine())!=null)   
  44.               int data;   
  45.               while((data=is.read())!=-1)//以字节流的方式读取   
  46.               {   
  47. //                fos.write(strLine.getBytes());//字符流方式读取   
  48.   //              fos.write(line.getBytes());   
  49.                 fos.write(data);   
  50.               }   
  51.               //br.close();   
  52.               is.close();   
  53.               fos.close();   
  54.             }   
  55.             catch (Exception ex) {   
  56.               ex.printStackTrace();   
  57.             }   
  58.   
  59.           }   
  60.         });   
  61.     jf.addWindowListener(new WindowAdapter() {   
  62.          public void windowClosing(WindowEvent e) {   
  63.            System.exit(0);   
  64.          }   
  65.        });   
  66.     jf.setVisible(true);   
  67.   }   
  68. }   
  69.   
  70.   
  71.   
  72.   
  73.   
  74.   
  75.   
  76.   
  77.   
  78.   
  79.   
  80.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值