File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .net .Socket ;
2+
3+ import java .net .*;
4+ import java .io .*;
5+
6+ public class Client {
7+
8+ Socket socket ;
9+
10+ BufferedReader br ;
11+ PrintWriter out ;
12+
13+
14+ public Client (){
15+
16+
17+ try {
18+ System .out .println ("Sending request to server" );
19+ socket =new Socket ("127.0.0.1" ,7777 );
20+ System .out .println ("Connection done!!" );
21+
22+ br = new BufferedReader (new InputStreamReader (socket .getInputStream ()));
23+
24+ out = new PrintWriter (socket .getOutputStream ());
25+
26+ startReading ();
27+ startWriting ();
28+
29+ }
30+ catch (Exception e ){}
31+ }
32+
33+ public void startReading (){
34+ Runnable r1 =()->{
35+ System .out .println ("reader started..." );
36+
37+ try {
38+ while (true ){
39+
40+ String msg = br .readLine ();
41+ if (msg .equals ("EXIT" )){System .out .println ("Server terminated the chatting!!" );
42+ socket .close ();
43+ break ;}
44+
45+ System .out .println ("Server : " +msg );
46+
47+
48+ }
49+ }catch (Exception e ){System .out .print ("----Connection is Closed----" );}
50+ };
51+
52+ new Thread (r1 ).start ();
53+ }
54+
55+
56+
57+ public void startWriting (){
58+ Runnable r2 =()->{
59+ System .out .println ("Writer started..." );
60+
61+ try {
62+ while (!socket .isClosed ()){
63+
64+ BufferedReader br1 = new BufferedReader (new InputStreamReader (System .in ));
65+
66+ String content =br1 .readLine ();
67+ out .println (content );
68+ out .flush ();
69+
70+ if (content .equals ("EXIT" )){
71+ socket .close ();
72+ break ;
73+ }
74+
75+ System .out .print ("----Connection is Closed----" );
76+ }
77+ }catch (Exception e ){};
78+ };
79+
80+ new Thread (r2 ).start ();
81+ }
82+
83+
84+
85+ public static void main (String [] args ){
86+
87+ System .out .println ("this is client..." );
88+ new Client ();
89+ }
90+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ import java .net .*;
3+ import java .io .*;
4+
5+ public class mclient {
6+
7+ public static void main (String s []) throws Exception {
8+ Socket s1 = null ;
9+ String line = null ;
10+ DataInputStream br = null ;
11+ DataInputStream is = null ;
12+ PrintWriter os = null ;
13+ try {
14+ s1 = new Socket ("localhost" , 9999 );
15+ br = new DataInputStream (System .in );
16+ is = new DataInputStream (s1 .getInputStream ());
17+ os = new PrintWriter (s1 .getOutputStream ());
18+
19+ } catch (IOException e ) {
20+ System .err .print ("IO Exception" );
21+
22+ }
23+ System .out .println ("Enter data to server (enter QUIT to end) :-> " +s1 .getRemoteSocketAddress ().toString ());
24+ String res = null ;
25+ try {
26+ line = br .readLine ();
27+ while (line .compareTo ("QUIT" ) != 0 ) {
28+ os .println (line );
29+ os .flush ();
30+ res = is .readLine ();
31+ System .out .println ("server response :-> " + res );
32+ line = br .readLine ();
33+ }
34+ is .close ();
35+ os .close ();
36+ br .close ();
37+ s1 .close ();
38+ System .out .println ("close connection " );
39+ } catch (IOException e ) {
40+ System .out .println ("socket read error" );
41+ }
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ import java .net .*;
3+ import java .io .*;
4+
5+ public class mserver {
6+
7+ public static void main (String s []) throws Exception {
8+ Socket sa = null ;
9+ ServerSocket ss2 = null ;
10+ System .out .println ("Host starts accepting response " );
11+ try {
12+ ss2 = new ServerSocket (9999 );
13+ } catch (IOException e ) {
14+ System .out .println ("server error" );
15+ }
16+ while (true ) {
17+ try {
18+ sa = ss2 .accept ();
19+ System .out .println ("connetion established by" + ss2 .getInetAddress ());
20+ ServerThread st = new ServerThread (sa );
21+ st .start ();
22+ } catch (Exception e ) {
23+ System .out .println ("connetion error" );
24+ }
25+ }
26+ }
27+ }
28+
29+ class ServerThread extends Thread {
30+ String line = null ;
31+ DataInputStream is = null ;
32+ PrintWriter od = null ;
33+ Socket s1 = null ;
34+
35+ public ServerThread (Socket s ) {
36+ s1 = s ;
37+ }
38+ +
39+ public void run () {
40+ try {
41+
42+ is = new DataInputStream (s1 .getInputStream ());
43+ od = new PrintWriter (s1 .getOutputStream ());
44+
45+ line = is .readLine ();
46+
47+ while (!line .equals ("QUIT" )) {
48+ od .println (line );
49+ od .flush ();
50+
51+ System .out .println ("response to client " + line );
52+ line = is .readLine ();
53+
54+ }
55+ is .close ();
56+ od .close ();
57+ s1 .close ();
58+
59+ } catch (IOException ie ) {
60+ System .out .println ("socket close error" );
61+ }
62+ }
63+ }
You can’t perform that action at this time.
0 commit comments