DataInputStream和InputStreamReader都可以用一个InputStream类做为其参数,而且目的也是一样的,就是把字节级的读取转换为字符级的读取!
#### Basing on Byte stream
Socket Operations at Client Side
• create a client socket:
Socket (host, port)
s = new Socket (“java.sun.com”, 13)
• get input / output data streams out of the socket:
in = new DataInputStream(s.getInputStream ());
out = new DataOutputStream( s.getOutputStream());
out = new PrintStream( s.getOutputStream());
• read from input / write to output data streams:
String str = in.readLine();
out.println ( “Echo:” + str + “\r”);
• close the socket:
s.close();
Socket Operations at Server Side
A server is always waiting for being connected. It need not initiate a connection to a host. So a server socket need only specify its own port no.
• create a server socket:
ServerSocket (port)
ServerSocket s = new ServerSocket(8189);
• accept an incoming connection:
Socket snew = s.accept ();
• get input / output data streams out of the socket for the incoming client:
in = new DataInputStream(snew.getInputStream());
out = new PrintStream(snew.getOutputStream());
• close the socket for the incoming client:
snew.close();
#### Basing on Character strem
Socket Operations at Client Side
• create a client socket:
Socket echoSocket = new Socket(args[0],9999);
out = new PrintWriter(echoSocket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
• read from input streams:
System.out.println("server: " + in.readLine());
• write to output streams:
String str = in.readLine();
out.println ( “Echo:” + str + “\r”);
• close the socket
echoSocket.close();
Socket Operations at Server Side
• create a client socket:
ServerSocket serviceSocket = new ServerSocket(9999);
r=new InputStreamReader(serviceSocket.getInputStream());
in=new BufferedReader(r);
out = new PrintWriter(serviceSocket.getOutputStream(),true);
• read from input streams:
String line=in.readLine()
System.out.println("Received from client "+line);
• write to output streams:
String str = in.readLine();
out.println("Server says: "+str);
• close the socket
serviceSocket.close();
#### Basing on Byte stream
Socket Operations at Client Side
• create a client socket:
Socket (host, port)
s = new Socket (“java.sun.com”, 13)
• get input / output data streams out of the socket:
in = new DataInputStream(s.getInputStream ());
out = new DataOutputStream( s.getOutputStream());
out = new PrintStream( s.getOutputStream());
• read from input / write to output data streams:
String str = in.readLine();
out.println ( “Echo:” + str + “\r”);
• close the socket:
s.close();
Socket Operations at Server Side
A server is always waiting for being connected. It need not initiate a connection to a host. So a server socket need only specify its own port no.
• create a server socket:
ServerSocket (port)
ServerSocket s = new ServerSocket(8189);
• accept an incoming connection:
Socket snew = s.accept ();
• get input / output data streams out of the socket for the incoming client:
in = new DataInputStream(snew.getInputStream());
out = new PrintStream(snew.getOutputStream());
• close the socket for the incoming client:
snew.close();
#### Basing on Character strem
Socket Operations at Client Side
• create a client socket:
Socket echoSocket = new Socket(args[0],9999);
out = new PrintWriter(echoSocket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
• read from input streams:
System.out.println("server: " + in.readLine());
• write to output streams:
String str = in.readLine();
out.println ( “Echo:” + str + “\r”);
• close the socket
echoSocket.close();
Socket Operations at Server Side
• create a client socket:
ServerSocket serviceSocket = new ServerSocket(9999);
r=new InputStreamReader(serviceSocket.getInputStream());
in=new BufferedReader(r);
out = new PrintWriter(serviceSocket.getOutputStream(),true);
• read from input streams:
String line=in.readLine()
System.out.println("Received from client "+line);
• write to output streams:
String str = in.readLine();
out.println("Server says: "+str);
• close the socket
serviceSocket.close();
Powered by ScribeFire.
No comments:
Post a Comment