File tree 2 files changed +58
-0
lines changed
10-security-components/socket
2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .net .*;
3
+
4
+ public class EchoClient {
5
+ public static void main (String [] args )
6
+ throws UnknownHostException , IOException {
7
+ Socket socket = null ;
8
+ try {
9
+ socket = new Socket ("localhost" , 9999 );
10
+ PrintWriter out = new PrintWriter (socket .getOutputStream (), true );
11
+ BufferedReader in = new BufferedReader (
12
+ new InputStreamReader (socket .getInputStream ()));
13
+ BufferedReader stdIn = new BufferedReader (
14
+ new InputStreamReader (System .in ));
15
+ String userInput ;
16
+ while ((userInput = stdIn .readLine ()) != null ) {
17
+ out .println (userInput );
18
+ System .out .println (in .readLine ());
19
+ }
20
+ } finally {
21
+ if (socket != null ) {
22
+ try {
23
+ socket .close ();
24
+ } catch (IOException e ) {
25
+ System .err .println (e );
26
+ }
27
+ }
28
+ }
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .net .*;
3
+
4
+ public class EchoServer {
5
+ public static void main (String [] args ) throws IOException {
6
+ ServerSocket serverSocket = null ;
7
+ try {
8
+ serverSocket = new ServerSocket (9999 );
9
+ Socket socket = serverSocket .accept ();
10
+ PrintWriter out = new PrintWriter (socket .getOutputStream (), true );
11
+ BufferedReader in = new BufferedReader (
12
+ new InputStreamReader (socket .getInputStream ()));
13
+ String inputLine ;
14
+ while ((inputLine = in .readLine ()) != null ) {
15
+ System .out .println (inputLine );
16
+ out .println (inputLine );
17
+ }
18
+ } finally {
19
+ if (serverSocket != null ) {
20
+ try {
21
+ serverSocket .close ();
22
+ } catch (IOException e ) {
23
+ System .err .println (e );
24
+ }
25
+ }
26
+ }
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments