/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. ////////////////  ConnServer.java   ////////////// 

2. import java.io.*; 

3. import java.net.*; 

4.  

5. class ConnServer

6.   public static void main ( String [ ] args ) { 

7.  

8.     String iln, outline; 

9.  

10.     ServerSocket srvSock = null

11.  

12.     Socket clnSock = null

13.  

14.     BufferedReader br = null

15.  

16.     PrintWriter pw = null

17.  

18.     try

19.     srvSock = new  ServerSocket ( 7654 ) ; 

20.     } 

21.     catch ( IOException ioe) { 

22.       System.out.println ( "Alas, port 7654 no go : "+ ioe ); 

23.       System.exit (1); 

24.     } 

25.  

26.     System.out.println ("\nWho's next ?!....\n"); 

27.  

28.     try

29.     clnSock = srvSock.accept();  //  Waiting for client......... 

30.     } 

31.     catch (IOException ioe) { 

32.       System.out.println("Accept error on 7654 : "  + ioe ) ; 

33.       System.exit (1); 

34.     } 

35.  

36.     System.out.println ("\nAttending client............"); 

37.  

38.     try

39.     br = new BufferedReader ( new InputStreamReader 

40.             ( clnSock.getInputStream () ) ) ; 

41.     pw = new PrintWriter (  clnSock.getOutputStream() ); 

42.  

43.     iln = br.readLine();  // what does the client say ? 

44.     System.out.println("Received  : " + iln ); 

45.     } 

46.     catch (IOException e) 

47.     { System.out.println("Error  : "  + e ) ; 

48.       System.exit (1); 

49.     } 

50.  

51.     try

52.     pw.println ( "OK" ); pw.flush();  // answering the client 

53.  

54.     iln = br.readLine();  // client says ? 

55.     System.out.println("Received  : " + iln ); 

56.     } 

57.  

58.     catch (IOException e) 

59.     { System.out.println("Error  : "  + e ) ; 

60.       System.exit (1); 

61.     } 

62.  

63.     System.out.println ("=CLOSING==="); 

64.     try

65.       if (br != null ) br.close(); 

66.       if (pw != null ) pw.close(); 

67.       if (clnSock != null ) clnSock.close(); 

68.       if (srvSock != null ) srvSock.close(); 

69.       br = null; pw = null; srvSock= null; } 

70.     catch (IOException e) 

71.     {   System.out.println("Error  : "  + e );     } 

72.  

73.   System.out.println ("======DONE========"); 

74.   } //end main() 

75.

76.  

77.  

78. /********************* ConnClient.java ****************** 

79.  

80. import java.awt.*; 

81. import java.awt.event.*; 

82. import java.io.*; 

83. import java.net.*; 

84.  

85. public class ConnClient   { 

86.  

87. public static void main (String args [ ] ) { 

88.  

89.   Socket sock = null

90.   InputStreamReader isr = null

91.   BufferedReader br = null

92.   PrintWriter pw = null

93.   String ln ; 

94.   try

95.   sock = new Socket ( "127.0.0.1", 7654  ); 

96.  

97.   pw = new PrintWriter (  sock.getOutputStream () ) ; 

98. //  pw = new PrintWriter ( new BufferedOutputStream 

99. //    ( sock.getOutputStream (),1024) ) ; 

100.   isr = new InputStreamReader ( sock.getInputStream () ); 

101.   br = new BufferedReader(isr); 

102.  

103.   pw.println ("HELLO" ); 

104.   pw.flush(); // now ! 

105.  

106.   ln =  br.readLine() ;  // server says ? 

107.  

108.   System.out.println ( "Received: "  + ln ) ; 

109.   if ( ln.equals( "OK" ) )   { 

110.      pw.println ("THANX"); 

111.      pw.flush(); 

112.   } 

113.   else 

114.   { 

115.      pw.println ("SAY WHAT ??"); 

116.      pw.flush(); 

117.   } 

118.   } 

119.   catch ( UnknownHostException uhe ) 

120.     { System.out.println("Error  : "  + uhe ) ; 

121.       System.exit (1); 

122.     } 

123.   catch ( IOException ioe ) 

124.     { System.out.println("Error  : "  + ioe ) ; 

125.     } 

126.   if ( pw != null )   { 

127.   try

128.       pw.close(); pw=null; } catch (Exception ioe)  { } } 

129.   if ( br != null )   { 

130.   try

131.       br.close(); br=null; } catch (Exception ioe)  { } } 

132.   if ( sock!= null )  { 

133.   try

134.       sock.close(); sock=null; } catch (Exception ioe)  { } } 

135.  

136. System.out.println ("======DONE========"); 

137.

138.

139. //============================ 

140. // clnSock.setSoTimeout( 60000 ); 

141. */