File tree 8 files changed +102
-0
lines changed
8 files changed +102
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+
3
+ import java .net .*;
4
+ import java .io .*;
5
+ public class GreetingClient {
6
+ public static void main (String [] args ) {
7
+ String serverName = args [0 ];
8
+ int port = Integer .parseInt (args [1 ]);
9
+ try { System .out .println ("Connecting to " + serverName + " on port " + port );
10
+ Socket client = new Socket (serverName , port );
11
+ System .out .println ("Just connected to " + client .getRemoteSocketAddress ());
12
+ OutputStream outToServer = client .getOutputStream ();
13
+ DataOutputStream out = new DataOutputStream (outToServer );
14
+ out .writeUTF ("Hello from " + client .getLocalSocketAddress ());
15
+ InputStream inFromServer = client .getInputStream ();
16
+ DataInputStream in = new DataInputStream (inFromServer );
17
+ System .out .println ("Server says " + in .readUTF ());
18
+ client .close ();
19
+ } catch (IOException e ) { e .printStackTrace (); } } }
Original file line number Diff line number Diff line change
1
+ import java .net .*;
2
+ import java .io .*;
3
+ public class GreetingServer extends Thread {
4
+ private ServerSocket serverSocket ;
5
+ public GreetingServer (int port ) throws IOException {
6
+ serverSocket = new ServerSocket (port );
7
+ serverSocket .setSoTimeout (10000 ); }
8
+ public void run () {
9
+ while (true ) { try {
10
+ System .out .println ("Waiting for client on port " +
11
+ serverSocket .getLocalPort () + "..." );
12
+ Socket server = serverSocket .accept ();
13
+ System .out .println ("Just connected to " + server .getRemoteSocketAddress ());
14
+ DataInputStream in = new DataInputStream (server .getInputStream ());
15
+ System .out .println (in .readUTF ());
16
+ DataOutputStream out = new DataOutputStream (server .getOutputStream ());
17
+ out .writeUTF ("Thank you for connecting to " + server .getLocalSocketAddress ()
18
+ + "\n Goodbye!" );
19
+ server .close (); }
20
+ catch (SocketTimeoutException s ) {
21
+ System .out .println ("Socket timed out!" );
22
+ break ; } catch (IOException e ) {
23
+ e .printStackTrace ();
24
+ break ; } } }
25
+ public static void main (String [] args ) {
26
+ int port = Integer .parseInt (args [0 ]);
27
+ try {
28
+ Thread t = new GreetingServer (port );
29
+ t .start ();
30
+ } catch (IOException e ) {
31
+ e .printStackTrace ();
32
+ }
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ package second ;
2
+
3
+ import java .io .*;
4
+ import java .net .*;
5
+
6
+ public class MyClient {
7
+ public static void main (String args [])throws Exception {
8
+ Socket s =new Socket ("localhost" ,3333 );
9
+ DataInputStream din =new DataInputStream (s .getInputStream ());
10
+ DataOutputStream dout =new DataOutputStream (s .getOutputStream ());
11
+ BufferedReader br =new BufferedReader (new InputStreamReader (System .in ));
12
+
13
+ String str ="" ,str2 ="" ;
14
+ while (!str .equals ("stop" )){
15
+ str =br .readLine ();
16
+ dout .writeUTF (str );
17
+ dout .flush ();
18
+ str2 =din .readUTF ();
19
+ System .out .println ("Server says: " +str2 );
20
+ }
21
+
22
+ dout .close ();
23
+ s .close ();
24
+ }}
25
+ /** url class in java */
Original file line number Diff line number Diff line change
1
+ package second ;
2
+
3
+ import java .net .*;
4
+ import java .io .*;
5
+ class MyServer {
6
+ public static void main (String args [])throws Exception {
7
+ ServerSocket ss =new ServerSocket (3333 );
8
+ Socket s =ss .accept ();
9
+ DataInputStream din =new DataInputStream (s .getInputStream ());
10
+ DataOutputStream dout =new DataOutputStream (s .getOutputStream ());
11
+ BufferedReader br =new BufferedReader (new InputStreamReader (System .in ));
12
+
13
+ String str ="" ,str2 ="" ;
14
+ while (!str .equals ("stop" )){
15
+ str =din .readUTF ();
16
+ System .out .println ("client says: " +str );
17
+ str2 =br .readLine ();
18
+ dout .writeUTF (str2 );
19
+ dout .flush ();
20
+ }
21
+ din .close ();
22
+ s .close ();
23
+ ss .close ();
24
+ }}
You can’t perform that action at this time.
0 commit comments