fork download
  1. //TcpClient.java -------------------------------------------------------------------------------------------------------------------------
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.ObjectInputStream;
  6. import java.net.Socket;
  7. import java.net.UnknownHostException;
  8.  
  9. /**
  10.  * TcpClient.java
  11.  *
  12.  * This class works in conjunction with TcpServer.java and TcpPayload.java
  13.   *
  14.  * This client test class connects to server class TcpServer, and in response,
  15. * it receives a serialized an instance of TcpPayload.
  16.  */
  17.  
  18. public class TcpClient
  19. {
  20. public final static String SERVER_HOSTNAME = "gsoler.arc.nasa.gov";
  21. public final static int COMM_PORT = 5050; // socket port for client comms
  22.  
  23. private Socket socket;
  24. private TcpPayload payload;
  25.  
  26. /** Default constructor. */
  27. public TcpClient()
  28. {
  29. try
  30. {
  31. this.socket = new Socket(SERVER_HOSTNAME, COMM_PORT);
  32. InputStream iStream = this.socket.getInputStream();
  33. ObjectInputStream oiStream = new ObjectInputStream(iStream);
  34. this.payload = (TcpPayload) oiStream.readObject();
  35. }
  36. catch (UnknownHostException uhe)
  37. {
  38. System.out.println("Don't know about host: " + SERVER_HOSTNAME);
  39. System.exit(1);
  40. }
  41. catch (IOException ioe)
  42. {
  43. System.out.println("Couldn't get I/O for the connection to: " +
  44. SERVER_HOSTNAME + ":" + COMM_PORT);
  45. System.exit(1);
  46. }
  47. catch(ClassNotFoundException cne)
  48. {
  49. System.out.println("Wanted class TcpPayload, but got class " + cne);
  50. }
  51. System.out.println("Received payload:");
  52. System.out.println(this.payload.toString());
  53. }
  54.  
  55. /**
  56.   * Run this class as an application.
  57.   */
  58. public static void main(String[] args)
  59. {
  60. TcpClient tcpclient = new TcpClient();
  61. }
  62. }
  63.  
  64. TcpServer.java -------------------------------------------------------------------------------------------------------------------------
  65.  
  66. import java.io.IOException;
  67. import java.io.ObjectOutputStream;
  68. import java.io.OutputStream;
  69. import java.net.InetSocketAddress;
  70. import java.net.ServerSocket;
  71. import java.net.Socket;
  72. import java.net.SocketException;
  73.  
  74. /**
  75.  * This class works in conjunction with TcpClient.java and TcpPayload.java
  76.  *
  77.  * This server test class opens a socket on localhost and waits for a client
  78.  * to connect. When a client connects, this server serializes an instance of
  79.  * TcpPayload and sends it to the client.
  80.  */
  81.  
  82. public class TcpServer
  83. {
  84. public final static int COMM_PORT = 5050; // socket port for client comms
  85.  
  86. private ServerSocket serverSocket;
  87. private InetSocketAddress inboundAddr;
  88. private TcpPayload payload;
  89.  
  90. /** Default constructor. */
  91. public TcpServer()
  92. {
  93. this.payload = new TcpPayload();
  94. initServerSocket();
  95. try
  96. {
  97. while (true)
  98. {
  99. // listen for and accept a client connection to serverSocket
  100. Socket sock = this.serverSocket.accept();
  101. OutputStream oStream = sock.getOutputStream();
  102. ObjectOutputStream ooStream = new ObjectOutputStream(oStream);
  103. ooStream.writeObject(this.payload); // send serilized payload
  104. ooStream.close();
  105. Thread.sleep(1000);
  106. }
  107. }
  108. catch (SecurityException se)
  109. {
  110. System.err.println("Unable to get host address due to security.");
  111. System.err.println(se.toString());
  112. System.exit(1);
  113. }
  114. catch (IOException ioe)
  115. {
  116. System.err.println("Unable to read data from an open socket.");
  117. System.err.println(ioe.toString());
  118. System.exit(1);
  119. }
  120. catch (InterruptedException ie) { } // Thread sleep interrupted
  121. finally
  122. {
  123. try
  124. {
  125. this.serverSocket.close();
  126. }
  127. catch (IOException ioe)
  128. {
  129. System.err.println("Unable to close an open socket.");
  130. System.err.println(ioe.toString());
  131. System.exit(1);
  132. }
  133. }
  134. }
  135.  
  136. /** Initialize a server socket for communicating with the client. */
  137. private void initServerSocket()
  138. {
  139. this.inboundAddr = new InetSocketAddress(COMM_PORT);
  140. try
  141. {
  142. this.serverSocket = new java.net.ServerSocket(COMM_PORT);
  143. assert this.serverSocket.isBound();
  144. if (this.serverSocket.isBound())
  145. {
  146. System.out.println("SERVER inbound data port " +
  147. this.serverSocket.getLocalPort() +
  148. " is ready and waiting for client to connect...");
  149. }
  150. }
  151. catch (SocketException se)
  152. {
  153. System.err.println("Unable to create socket.");
  154. System.err.println(se.toString());
  155. System.exit(1);
  156. }
  157. catch (IOException ioe)
  158. {
  159. System.err.println("Unable to read data from an open socket.");
  160. System.err.println(ioe.toString());
  161. System.exit(1);
  162. }
  163. }
  164.  
  165. /**
  166.   * Run this class as an application.
  167.   */
  168. public static void main(String[] args)
  169. {
  170. TcpServer tcpServer = new TcpServer();
  171. }
  172. }
  173.  
  174. TcpPayload.java -------------------------------------------------------------------------------------------------------------------------
  175.  
  176. import java.io.Serializable;
  177.  
  178. /**
  179.  * This class works in conjunction with TcpClient.java and TcpServer.java
  180.  *
  181.  * This class contains test data representing a 'payload' that is sent from
  182.  * TcpServer to TcpClient. An object of this class is meant to be serialized by
  183.  * the server before being sent to the client. An object of this class is meant
  184.  * to be deserialized by the client after being received.
  185.  */
  186.  
  187. public class TcpPayload implements Serializable
  188. {
  189. // serial version UID was generated with serialver command
  190. static final long serialVersionUID = -50077493051991107L;
  191.  
  192. private int int1;
  193. private transient int int2; // transient members are not serialized
  194. private float float1;
  195. private double double1;
  196. private short short1;
  197. private String str1;
  198. private long long1;
  199. private char char1;
  200.  
  201. /** Default constructor. */
  202. public TcpPayload()
  203. {
  204. this.int1 = 123;
  205. this.int2 = 456;
  206. this.float1 = -90.05f;
  207. this.double1 = 55.055;
  208. this.short1 = 59;
  209. this.str1 = "I am a String payload.";
  210. this.long1 = -23895901L;
  211. this.char1 = 'x';
  212. }
  213.  
  214. /** Get a String representation of this class. */
  215. public String toString()
  216. {
  217. StringBuilder strB = new StringBuilder();
  218. strB.append("int1=" + this.int1);
  219. strB.append(" int2=" + this.int2);
  220. strB.append(" float1=" + this.float1);
  221. strB.append(" double1=" + this.double1);
  222. strB.append(" short1=" + this.short1);
  223. strB.append(" str1=" + this.str1);
  224. strB.append(" long1=" + this.long1);
  225. strB.append(" char1=" + this.char1);
  226. return strB.toString();
  227. }
  228. }
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
Runtime error #stdin #stdout 0.01s 4980KB
stdin
Standard input is empty
stdout
Standard output is empty