1. 程式人生 > 其它 >Superannuation funds, mutual funds and hedge funds are example of Question 2 options

Superannuation funds, mutual funds and hedge funds are example of Question 2 options

1.網路程式設計有兩個主要的問題:

  • 如何準確的定位到網路上的一臺或多臺主機
  • 找到主機後如何進行通訊

2.網路程式設計的要素

  • IP和埠號
  • 網路通訊協議

1.IP

  cmd->ipconfig:檢視本機IP地址

 1 package com.KingJ.lesson1;
 2 
 3 import java.net.Inet4Address;
 4 import java.net.InetAddress;
 5 import java.net.UnknownHostException;
 6 
 7 public class TestInetAddress {
 8     public
static void main(String[] args) { 9 try { 10 //查詢本機地址 11 InetAddress inetaddress1 = InetAddress.getByName("127.0.0.1"); 12 System.out.println(inetaddress1); 13 InetAddress inetaddress2 = InetAddress.getByName("localhost"); 14 System.out.println(inetaddress2);
15 InetAddress inetaddress3 = InetAddress.getLocalHost(); 16 System.out.println(inetaddress3); 17 18 //查詢網站地址 19 InetAddress inetaddress4 = InetAddress.getByName("www.baidu.com"); 20 System.out.println(inetaddress4); 21 22 //常用方法 23
System.out.println(inetaddress4.getAddress()); 24 System.out.println(inetaddress4.getCanonicalHostName());//獲取規範的名字 25 System.out.println(inetaddress4.getHostAddress());//IP 26 System.out.println(inetaddress4.getHostName());//域名,或自己電腦的名字 27 28 29 } catch (UnknownHostException e) { 30 e.printStackTrace(); 31 } 32 } 33 }

 

2.埠

  常見DOS命令:

 1 package com.KingJ.lesson1;
 2 
 3 import java.net.InetSocketAddress;
 4 
 5 public class TestInetSocketAddress {
 6     public static void main(String[] args) {
 7         InetSocketAddress inetSocketAddress1 = new InetSocketAddress("127.168.0.1", 8080);
 8         System.out.println(inetSocketAddress1);
 9         InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 8080);
10         System.out.println(inetSocketAddress2);
11 
12         System.out.println(inetSocketAddress1.getAddress());
13         System.out.println(inetSocketAddress1.getHostName());//地址
14         System.out.println(inetSocketAddress1.getPort());//
15     }
16 }

 

3.TCP

  3.1TCP實現聊天

客戶端:

 1 package com.KingJ.lesson2;
 2 
 3 import java.io.IOException;
 4 import java.io.OutputStream;
 5 import java.net.InetAddress;
 6 import java.net.Socket;
 7 import java.net.UnknownHostException;
 8 import java.nio.charset.StandardCharsets;
 9 
10 public class TestClientdemo01 {
11     public static void main(String[] args) {
12         Socket socket = null;
13         OutputStream os = null;
14         //1.要知道伺服器的地址,埠號
15         try {
16             //1.要知道伺服器的地址,埠號
17             InetAddress serverIP = InetAddress.getByName("127.0.0.1");
18             int port = 9999;
19             //2.建立一個socket連線
20             socket = new Socket(serverIP, port);
21             //3.傳送訊息 I/O流
22             os = socket.getOutputStream();
23             os.write("你好,請滾出去".getBytes());
24         } catch (Exception e) {
25             e.printStackTrace();
26         } finally {
27             if (os != null) {
28                 try {
29                     os.close();
30                 } catch (IOException e) {
31                     e.printStackTrace();
32                 }
33             }
34             if (socket != null) {
35                 try {
36                     socket.close();
37                 } catch (IOException e) {
38                     e.printStackTrace();
39                 }
40             }
41 
42         }
43     }
44 
45 }

 

  伺服器端:

 1 package com.KingJ.lesson2;
 2 
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.net.ServerSocket;
 7 import java.net.Socket;
 8 
 9 public class TestServerdemo02 {
10 
11     public static void main(String[] args) {
12         ServerSocket serverSocket = null;
13         Socket socket = null;
14         InputStream is = null;
15         ByteArrayOutputStream baos = null;
16 
17         try {
18             //1.我得有一個地址
19             serverSocket = new ServerSocket(9999);
20             //2.等待客戶端連線過來
21             socket = serverSocket.accept();
22             //3.讀取客戶端發來的訊息
23             is = socket.getInputStream();
24 
25             //管道流
26             baos = new ByteArrayOutputStream();
27             byte[] buffer = new byte[1024];
28             int len;
29             while ((len = is.read(buffer)) != -1) {
30                 baos.write(buffer, 0, len);
31             }
32             System.out.println(baos.toString());
33 
34 
35         } catch (IOException e) {
36             e.printStackTrace();
37         } finally {
38             //關閉資源
39             if (baos != null) {
40                 try {
41                     baos.close();
42                 } catch (IOException e) {
43                     e.printStackTrace();
44                 }
45             }
46             if (is != null) {
47                 try {
48                     is.close();
49                 } catch (IOException e) {
50                     e.printStackTrace();
51                 }
52             }
53             if (socket != null) {
54                 try {
55                     socket.close();
56                 } catch (IOException e) {
57                     e.printStackTrace();
58                 }
59             }
60             if (serverSocket != null) {
61                 try {
62                     serverSocket.close();
63                 } catch (IOException e) {
64                     e.printStackTrace();
65                 }
66             }
67 
68 
69         }
70     }
71 }

 

   3.2 檔案上傳

伺服器端:

 1 package com.KingJ.lesson2.TestFile;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6 import java.net.ServerSocket;
 7 import java.net.Socket;
 8 
 9 public class TestServer1 {
10     public static void main(String[] args) throws Exception {
11         //1.建立服務
12         ServerSocket serverSocket = new ServerSocket(9999);
13         //2.監聽客戶端的連線
14         Socket socket = serverSocket.accept();
15         //3.獲取輸入流
16         InputStream is = socket.getInputStream();
17 
18         //4.檔案輸出
19         FileOutputStream fos = new FileOutputStream("receive.PNG");
20         byte[] buffer = new byte[1024];
21         int len;
22         while ((len = is.read(buffer)) != -1) {
23             fos.write(buffer, 0, len);
24         }
25 
26         //通知客戶端接收完畢
27         OutputStream os = socket.getOutputStream();
28         os.write("接收完畢".getBytes());
29 
30         //關閉資源
31         os.close();
32         fos.close();
33         is.close();
34         socket.close();
35         serverSocket.close();
36     }
37 }

客戶端

 1 package com.KingJ.lesson2.TestFile;
 2 
 3 import java.io.*;
 4 import java.net.InetAddress;
 5 import java.net.Socket;
 6 
 7 public class TestClient1 {
 8     public static void main(String[] args) throws Exception {
 9         //1.建立一個socket連線
10         Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
11         //2.建立一個輸出流
12         OutputStream os = socket.getOutputStream();
13         //3.讀取檔案
14         FileInputStream fis = new FileInputStream(new File("HTML.PNG"));
15         //4.寫出檔案
16         byte[] buffer = new byte[1024];
17         int len;
18         while ((len = fis.read(buffer)) != -1) {
19             os.write(buffer, 0, len);
20         }
21         //通知伺服器我已經發送完了
22         socket.shutdownOutput();
23         //確定伺服器接收完畢
24         InputStream inputStream = socket.getInputStream();
25         ByteArrayOutputStream baos = new ByteArrayOutputStream();
26         byte[] buffer2 = new byte[1024];
27         int len2;
28         while ((len = inputStream.read(buffer2)) != -1) {
29             baos.write(buffer, 0, len);
30         }
31         System.out.println(baos.toString());
32 
33 
34         //5.關閉資源
35         baos.close();
36         inputStream.close();
37         fis.close();
38         os.close();
39         socket.close();
40 
41 
42     }
43 }

 

4.UDP

傳送方:

 1 package com.KingJ.lesson1.lesson4;
 2 
 3 import java.net.DatagramPacket;
 4 import java.net.DatagramSocket;
 5 import java.net.InetAddress;
 6 import java.net.SocketException;
 7 
 8 public class UdpClient1 {
 9     public static void main(String[] args) throws Exception {
10         //1.建立一個socket
11         DatagramSocket socket = new DatagramSocket();
12         //要傳送的訊息
13         String msg = "你好,請滾出去!";
14         //傳送給誰
15         InetAddress localhost = InetAddress.getByName("localhost");
16         int port = 9999;
17         //2.建個包
18         DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
19 
20         //3.傳送包
21         socket.send(packet);
22 
23         //關閉流
24         socket.close();
25 
26     }
27 }

接收方:

 1 package com.KingJ.lesson1.lesson4;
 2 
 3 import java.net.DatagramPacket;
 4 import java.net.DatagramSocket;
 5 import java.net.SocketException;
 6 
 7 public class UdpServer1 {
 8     public static void main(String[] args) throws Exception {
 9         //開放埠
10         DatagramSocket socket = new DatagramSocket(9999);
11         //接收資料包
12         byte[] buffer = new byte[1024];
13         DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
14 
15         socket.receive(packet);
16         System.out.println(new String(packet.getData(), 0, packet.getData().length));
17 
18         //關閉連線
19         socket.close();
20 
21     }
22 }

 

線上諮詢實現:

 1 package com.KingJ.chat;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.InputStreamReader;
 5 import java.net.DatagramPacket;
 6 import java.net.DatagramSocket;
 7 import java.net.InetSocketAddress;
 8 
 9 public class TalkSend implements Runnable {
10     DatagramSocket socket = null;
11     BufferedReader reader = null;
12 
13     private int fromPort;//自己的埠
14     private String toIP;//對方的IP
15     private int toPort;//對方的埠號
16 
17 
18     public TalkSend(int fromPort, String toIP, int toPort) {
19         this.fromPort = fromPort;
20         this.toIP = toIP;
21         this.toPort = toPort;
22         try {
23             socket = new DatagramSocket(fromPort);
24             reader = new BufferedReader(new InputStreamReader(System.in));
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28 
29     }
30 
31     @Override
32     public void run() {
33 
34         while (true) {
35             try {
36                 String data = reader.readLine();
37                 byte[] datas = data.getBytes();//資料轉換為位元組
38                 DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));
39                 socket.send(packet);
40                 if (data.equals("bye")) {
41                     break;
42                 }
43 
44             } catch (Exception e) {
45                 e.printStackTrace();
46             }
47 
48         }
49 
50         socket.close();
51     }
52 }
 1 package com.KingJ.chat;
 2 
 3 import java.io.IOException;
 4 import java.net.DatagramPacket;
 5 import java.net.DatagramSocket;
 6 import java.net.SocketException;
 7 
 8 public class TalkReceive implements Runnable {
 9     DatagramSocket socket = null;
10     private int port;
11     private String msgFrom;
12 
13     public TalkReceive(int port, String msgFrom) {
14         this.port = port;
15         this.msgFrom = msgFrom;
16         try {
17             socket = new DatagramSocket(port);
18         } catch (SocketException e) {
19             e.printStackTrace();
20         }
21 
22     }
23 
24     @Override
25     public void run() {
26         //準備接收包裹
27         byte[] container = new byte[1024];
28         while (true) {
29 
30             try {
31                 DatagramPacket packet = new DatagramPacket(container, 0, container.length);
32                 socket.receive(packet);
33 
34                 byte[] data = packet.getData();
35                 String receiveDatas = new String(data, 0, packet.getLength());
36                 System.out.println(msgFrom + ":" + receiveDatas);
37 
38                 if (receiveDatas.equals(("bye"))) {
39                     break;
40                 }
41             } catch (IOException e) {
42                 e.printStackTrace();
43             }
44 
45 
46         }
47         socket.close();
48 
49     }
50 }
1 package com.KingJ.chat;
2 
3 public class TalkStudent {
4     public static void main(String[] args) {
5         new Thread(new TalkSend(7777, "localhost", 9999)).start();
6         new Thread(new TalkReceive(8888, "老師")).start();
7     }
8 }
1 package com.KingJ.chat;
2 
3 public class TalkTeacher {
4     public static void main(String[] args) {
5         new Thread(new TalkSend(5555, "localhost", 8888)).start();
6         new Thread(new TalkReceive(9999, "老師")).start();
7     }
8 }