File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .net .*;
3+
4+ class TCPClient {
5+ public static void main (String argv []) throws Exception
6+ {
7+ String sentence ;
8+ String modifiedSentence ;
9+
10+ BufferedReader inFromUser =new BufferedReader (new InputStreamReader (System .in ));
11+
12+ Socket clientSocket = new Socket ("localhost" , 4444 );
13+
14+ DataOutputStream outToServer = new DataOutputStream (clientSocket .getOutputStream ());
15+ BufferedReader inFromServer = new BufferedReader (new InputStreamReader (clientSocket .getInputStream ()));
16+ System .out .println ("Enter a message:" );
17+ sentence = inFromUser .readLine ();
18+ outToServer .writeBytes (sentence + '\n' );
19+ modifiedSentence = inFromServer .readLine ();
20+ System .out .println ("FROM SERVER: " + modifiedSentence );
21+ clientSocket .close ();
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .net .*;
3+ class Thr extends Thread
4+ {
5+ Socket s ;
6+
7+ Thr (){s =null ; }
8+
9+ Thr (Socket s1 ) { s =s1 ;}
10+
11+ public void run ()
12+ {
13+ String clientSentence ;
14+ String capitalizedSentence ;
15+ try {
16+ BufferedReader inFromClient =new BufferedReader (new InputStreamReader (s .getInputStream ()));
17+ DataOutputStream outToClient =new DataOutputStream (s .getOutputStream ());
18+ clientSentence = inFromClient .readLine ();
19+ System .out .println (clientSentence );
20+ capitalizedSentence = clientSentence .toUpperCase () + '\n' ;
21+ outToClient .writeBytes (capitalizedSentence );
22+ s .close ();
23+ if (clientSentence .equals ("exit" ))
24+ System .exit (1 );
25+
26+ }
27+ catch (Exception e ){}
28+ }
29+
30+ }
31+ class TCPServer {
32+ public static void main (String argv []) throws Exception {
33+
34+ ServerSocket dataReceive = new ServerSocket (4444 );
35+
36+ while (true ) {
37+ Socket connectionSocket = dataReceive .accept ();
38+ Thr t =new Thr (connectionSocket );
39+ t .start ();
40+ }
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments