320 likes | 460 Views
Java. Chin-Yi Tsai cyt@pmlab.iecs.fcu.edu.tw http://140.134.26.25/~cyt. Outline. Java Network Programming. Application 應用層. Application. 提 供 上 層 一 些 服 務. 使 用 下 層 所 提 供 的 服 務. TCP/UDP 傳輸層. TCP/UDP. IP 網路層. IP. Network Access 網路存取層. Network Access. TCP/IP. 應用程式 1.
E N D
Java Chin-Yi Tsai cyt@pmlab.iecs.fcu.edu.tw http://140.134.26.25/~cyt
Outline • Java Network Programming
Application 應用層 Application 提 供 上 層 一 些 服 務 使 用 下 層 所 提 供 的 服 務 TCP/UDP 傳輸層 TCP/UDP IP 網路層 IP Network Access 網路存取層 Network Access TCP/IP
應用程式1 應用程式2 應用程式3 應用程式4 socket() socket() 網路程式介面API bind() connect() listen() TCP/IP BSD Socketc 建立連線 accept() Client Server 作業系統 Java Net SocketAPI 網路卡 WinSock write() read() read() write() 應用程式與Socket關係
Java Network API • 在Java Programming中,對於網路相關的功能,提供了以下三大類: • Datagram • Socket • URL
InetAddress類別 • InetAddress類別代表Internet Protocol (IP)位址。 • InetAddress addr1 = InetAddress.getLocalHost(); • IentAddress addr2 = InetAddress.getByname("www.kimo.com.tw");
URL類別 • URL類別代表Uniform Resource Locator。 • URL ko = new URL("http://www.kimo.com.tw/book/seller"); • System.out.println(ko.getProtocol()); • System.out.println(ko.getHost());
Socket相關 • Socket類別 • Socket類別是指client socket,不同於ServerSocket,建立一個client socket,使用Socket和Server連接,再利用InputStream從Server接收資料,利用OutputStream將資料送回給Server。 • ServerSocket類別 • ServerSocket指Server端的Socket。
java.net支援Stream Socket (TCP)相關的API • Server • java.net.ServerSocket • accept • (java.io.DataInputStream) read • (java.io.DataOutputStream) write • close • Client • java.net.Socket • (java.io.DataInputStream) read • (java.io.DataOutputStream) write • close
java.net支援Datagram Socket (UDP)相關的API • java.net.DatagramSocket • java.net.DatagramPacket • (DatagramPacket) receive • (DatagramPacket) send • close
TCP之傳送 • 取得連線IP位址 • 取得輸入 • 建立Socket物件 • 建立輸出資料流物件 • 送出資料並關閉Socket
InetAddress addr = null; BufferedReader key_in = new BufferedReader(new InputStreamReader(System.in)); addr = InetAddress.getByName(args[0]); Socekt socket = new Socket(addr, port); BufferedReader s_in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter s_out = new PrintWriter(socket.getOutputStream()); while(true) { String data = key_in.readLine(); s_out.println(data); s_out.flush(); System.out.println(s_in.readLine()); }
TCP之接收 • 建立ServerSocket物件 • 建立輸入資料流物件 • 讀取字串資料
ServerSocket sc = new ServerSocket(port); Soekct socket = sc.accept(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStrea())); PrintWriter output = new PrintWriter(socket.getOutputStream()); while(true) { String data = input.getLine(); output.println("Server get it"+data); output.flush(); } socket.close();
如果要使一個Server可以同時接收多個Client的連線時,就必須要使用Thread才可以,否則只有單一使用者可以使用Server。
一個簡單的Client/Server的程式 import java.net.*;import java.io.*;public class Client { public static void main(String[] args) throws Exception { String data; Socket socket_client; int port=1024; BufferedReader key_in = new BufferedReader(new InputStreamReader(System.in)); InetAddress addr = InetAddress.getByName(args[0]); socket_client = new Socket(addr, port); PrintWriter out = new PrintWriter(socket_client.getOutputStream()); while(true) { System.out.println("Client>"); data=key_in.readLine(); //取得輸入資料來當送出到Server的資料if (data == "quit") break; out.println(data); out.flush(); } } }
一個簡單的Client/Server的程式 import java.net.*;import java.io.*;public class Server { public static void main(String[] args) throws Exception { String data; ServerSocket socket_server; Socket socket1; int port=1024; socket_server = new ServerSocket(port); socket1=socket_server.accept(); //用BufferedReader來承接InputStream資料流BufferedReader in = new BufferedReader(new InputStreamReader(socket1.getInputStream())); while(true) { System.out.println("Server>"); data=in.readLine(); if (data=="quit") break; System.out.println(data); } } }
Client Server Client
處理多個用戶端連線 用戶1 Server 用戶2 用戶3 用戶4
MServer Client1 Client2
import java.io.*;import java.net.*;public class Mserver extends Thread { ServerSocket socket_server; Socket socket1; int count = 0; public Mserver(){ try { socket_server = new ServerSocket(1024); System.out.println("listen...."); } catch(Exception e){ ; } this.start(); }
public void run() { try { while(true) { socket1 = socket_server.accept(); Conn con = new Conn(socket1, ++count); } } catch(Exception ee){ ; } } public static void main(String[] args) { new Mserver(); }}
//處理不同的連線class Conn extends Thread{ Socket socket_new; int id; BufferedReader in; PrintWriter out; public Conn(Socket socket_new, int id){ this.socket_new = socket_new; this.id = id; try { in = new BufferedReader(new InputStreamReader(socket_new.getInputStream())); out = new PrintWriter(socket_new.getOutputStream()); }catch(Exception ceee){ ; } this.start(); }
public void run(){ System.out.println(id); try {while(true) { String msg = in.readLine(); System.out.println("Received:" + msg); out.println("收到第" +id + "連線資料:" + msg); out.flush(); } }catch(Exception kkk){ ; } }}
Chat程式 import java.net.*; import java.io.*; //java ChatClient localhost Matt public class ChatClient { public static void main(String[] args) throws Exception { String data; String name; Socket socket_client; int port=1024; BufferedReader key_in = new BufferedReader(new InputStreamReader(System.in)); InetAddress addr = InetAddress.getByName(args[0]); name = args[1]; socket_client = new Socket(addr, port); PrintWriter out = new PrintWriter(socket_client.getOutputStream()); BufferedReader in = new BufferedReader( new InputStreamReader(socket_client.getInputStream()) );
Chat程式 while(true) { System.out.print(name + ">"); data=key_in.readLine(); if (data == "quit") break; out.println(name + ">>>>" + data); out.flush(); System.out.println("Wait Response"); data = in.readLine(); System.out.println(data); } } }
Chat程式 import java.io.*; import java.net.*; public class ChatServer { public static void main(String[] args) throws Exception { String data; String name; ServerSocket socket_server; Socket socket_conn; int port=1024; BufferedReader key_in = new BufferedReader(new InputStreamReader(System.in)); name = args[0]; socket_server = new ServerSocket(port); System.out.println("Waiting..."); socket_conn = socket_server.accept(); PrintWriter out = new PrintWriter(socket_conn.getOutputStream()); BufferedReader in = new BufferedReader( new InputStreamReader(socket_conn.getInputStream()));
Chat程式 while(true) { data = in.readLine(); System.out.println(data); System.out.print(name + ">"); data=key_in.readLine(); if (data == "quit") break; out.println(name + ">>" + data); out.flush(); System.out.println("Wait Response"); } } }