Message Transition 5-1 5-2 DataInputStream Class 5-3 DataOutputStream Class 5-4 PrintStream Class 5-5 (Message Transition) (Exercises)
Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Object (implements) DataInput 1 ( 07) public DataInputStream(InputStream in); in DataInputStream in 4-4 Socket.getInputStream() 2 ( ) public final boolean readboolean() throws IOException; public final char readchar() throws IOException; public final byte readbyte() throws IOException; public final short readshort() throws IOException; public final int readint() throws IOException; public final long readlong() throws IOException; public final float readfloat() throws IOException; public final double readdouble() throws IOException; [ 5-2 ]
(Message Transition) 05 public final String readline() throws IOException; ( 07) public final String readutf() throws IOException; ( 08) UTF UTF ASCII 5-3 DataOutputStream Class java.io.dataoutptstream (extends) FilterOutputStream OutputStream Object (implements) DataOutput 1 public DataOutputStream(OutputStream out); out DataOutputStream out 23-4 Socket.getOutputStream() 2 public final boolean writeboolean() throws IOException; public final char writechar() throws IOException; public final byte writebyte() throws IOException; public final short writeshort() throws IOException; public final int writeint() throws IOException; public final long writelong() throws IOException; public final float writefloat() throws IOException; public final double writedouble() throws IOException; PART 1 PART 3 PART 4 PART 2 PART 5 PART 6 [ 5-3 ]
Java Access public final String writeutf() throws IOException; ( 08) UTF UTF ASCII 5-4 PrintStream Class java.io.printstream (extends) FilterOutputStream OutputStream Object DataOutputStream 1 public PrintStream(OutputStream out); public PrintStream(OutputStream out, boolean autoflush); out DataOutputStream out 4-4 Socket.getOutputStream() autoflush true 2 ( 07) public void print(); public void print(boolean b); public void print(char c); public void print(char s[]); public void print(double d); public void print(float f); public void print(int i); public void print(long l); public void print(object o); public void print(string s); [ 5-4 ]
(Message Transition) 05 public void println(); ( 07) public void println(boolean b); public void println(char c); public void println(char s[]); public void println(double d); public void println(float f); public void println(int i); public void println(long l); public void println(object o); public void println(string s); 5-5 (Message Transition) DataInputStream PrintStream / Server/Client 07 Client Server Server Client 007 Server07.java 01 import java.net.*; 02 import java.io.*; Server07.java Client07.java Server/Client Client Server Server Client 03 public class Server07{ 04 ServerSocket SS; 05 Socket socket; 06 DataInputStream in_from_client; PART 1 PART 3 PART 4 PART 2 PART 5 PART 6 [ 5-5 ]
Java Access 07 PrintStream out_to_client; 08 static int port; 09 public Server07(){ 10 try{ 11 SS = new ServerSocket(port); 12 System.out.println("Server is created and waiting Client to connect..."); 13 socket = SS.accept(); 14 System.out.println("Client IP =" + socket.getinetaddress().gethostaddress()); 15 in_from_client = new DataInputStream(socket.getInputStream()); 16 out_to_client = new PrintStream(socket.getOutputStream()); 17 String messagein = in_from_client.readline(); 18 System.out.print(messageIn); 19 out_to_client.println("from Server: English Message and "); 20 } 21 catch(java.lang.exception e){ 22 System.out.println(e.getMessage()); 23 } 24 } 25 public static void main(string args[]){ 26 if(args.length<1) { 27 System.out.println("Usage: java Server [port]"); 28 System.exit(1); 29 } 30 port = Integer.parseInt(args[0]); 31 Server07 ServerStart=new Server07(); 32 } 33 } 06~07 / 15~16 / ( 5-2 5-4 ) 17 ( 5-2 ) 18 19 Client ( 5-4 ) 06 [ 5-6 ]
(Message Transition) 05 Client07.java 34 import java.net.*; 35 import java.io.*; 36 public class Client07{ 37 Socket socket; 38 DataInputStream in_from_server; 39 PrintStream out_to_server; 40 static int port; 41 static String iaddr; 42 static String messageout; 43 public Client07(){ 44 try{ 45 socket = new Socket(InetAddress.getByName(iaddr), port); 46 in_from_server = new DataInputStream(socket.getInputStream()); 47 out_to_server = new PrintStream(socket.getOutputStream()); 48 out_to_server.println(messageout); 49 String info = in_from_server.readline(); 50 System.out.println(info); 51 } 52 catch(ioexception e){ 53 System.out.println(e.getMessage()); } 54 } 55 public static void main(string args[]){ 56 if(args.length < 3) { 57 System.out.println("USAGE: java Client [iaddr] [port] [messageout]"); 58 System.exit(1); 59 } 60 iaddr = args[0]; 61 port = Integer.parseInt(args[1]); 62 messageout = args[2]; 63 Client07 ClientStart = new Client07(); 64 } 65 } PART 1 PART 3 PART 4 PART 2 PART 5 PART 6 [ 5-7 ]
Java Access 38~39 / 46~47 / 48 62 49 Server 06 1 javac *.java 2 Server Client Dos Server Client ( ) (1) Server java Server07 1234 Client [ 5-8 ]
(Message Transition) 05 (2) Client ( IP Port ) java Client07 163.15.40.243 1234 "From Client: English Message and " Server ( 163.15.40.243 Server IP 1234 Server Port Server Server (3) Server Client Client socket Server/Client ( Client IP Client ) 3 5-4 PrintStream println() PART 1 PART 3 PART 4 PART 2 PART 5 PART 6 [ 5-9 ]
Java Access DataOutputStream writeutf() readutf() 008 07 Server08.java Client08.java Server08.java Server/Client Client Server Server Client 01 import java.net.*; 02 import java.io.*; 03 public class Server08{ 04 ServerSocket SS; 05 Socket socket; 06 DataInputStream in_from_client; 07 DataOutputStream out_to_client; 08 static int port; 09 public Server08(){ 10 try{ 11 SS = new ServerSocket(port); 12 System.out.println("Server is created and waiting Client to connect..."); 13 socket = SS.accept(); 14 System.out.println("Client IP =" + socket.getinetaddress().gethostaddress()); 15 in_from_client = new DataInputStream(socket.getInputStream()); 16 out_to_client = new DataOutputStream(socket.getOutputStream()); 17 String messagein = in_from_client.readutf(); 18 System.out.print(messageIn); 19 out_to_client.writeutf("from Server: English Message and "); 20 } 21 catch(java.lang.exception e){ [ 5-10 ]
(Message Transition) 05 22 System.out.println(e.getMessage()); 23 } 24 } 25 public static void main(string args[]){ 26 if(args.length<1) { 27 System.out.println("Usage: java Server [port]"); 28 System.exit(1); 29 } 30 port = Integer.parseInt(args[0]); 31 Server08 ServerStart=new Server08(); 32 } 33 } 07 16 ( 5-3 ) 17 ( 5-3 ) 19 Client ( 5-3 ) 07 Client08.java 34 import java.net.*; 35 import java.io.*; 36 public class Client08{ 37 Socket socket; 38 DataInputStream in_from_server; 39 DataOutputStream out_to_server; 40 static int port; 41 static String iaddr; 42 static String messageout; 43 public Client08(){ 44 try{ 45 socket = new Socket(InetAddress.getByName(iaddr), port); 46 in_from_server = new DataInputStream(socket.getInputStream()); 47 out_to_server = new DataOutputStream(socket.getOutputStream()); PART 1 PART 3 PART 4 PART 2 PART 5 PART 6 [ 5-11 ]
Java Access 48 out_to_server.writeutf(messageout); 49 String info = in_from_server.readutf(); 50 System.out.println(info); 51 } 52 catch(ioexception e){ 53 System.out.println(e.getMessage()); } 54 } 55 public static void main(string args[]){ 56 if(args.length < 3) { 57 System.out.println("USAGE: java Client [iaddr] [port] [messageout]"); 58 System.exit(1); 59 } 60 iaddr = args[0]; 61 port = Integer.parseInt(args[1]); 62 messageout = args[2]; 63 Client08 ClientStart = new Client08(); 64 } 65 } 39 47 48 62 49 Server 07 1 javac *.java [ 5-12 ]
(Message Transition) 05 2 Server Client Dos Server Client ( ) (1) Server java Server08 1234 Client (2) Client ( IP Port ) java Client08 163.15.40.243 1234 "From Client: English Message and " Server ( 163.15.40.243 Server IP 1234 Server Port Server Server ) (3) Server Client Client socket Server/Client ( Client IP Client ) PART 1 PART 3 PART 4 PART 2 PART 5 PART 6 [ 5-13 ]
Java Access 3 5-3 DataOutputStream println() readutf() [ 5-14 ]
(Message Transition) 05 Exercises 1. DataInputStream Class 2. DataOutputStream Class 3. PrintStream Class 4. DataInputStream 5. PrintStream 6. readline() 7. println() PART 1 PART 3 PART 4 PART 2 PART 5 PART 6 [ 5-15 ]