Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import java.net.*; //contains the basics needed for network operations
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class TCP_Echo_Client {
  6.  
  7. public static void main(String[] args) throws IOException{
  8. //String host = "193.188.43.36";
  9. //String host = "tombaker-ict.research.um.edu.mt";
  10. String host ="10.249.45.5"; //Room 6 BLK B Lvl -1
  11. // String host = "10.64.16.146";
  12. byte[] response = new byte[100];
  13. //int port = 1024 + 7;
  14. int port = 7;
  15. Socket clientSock=null;
  16. try {
  17. InetAddress address = InetAddress.getByName(host);
  18. clientSock = new Socket(address, port);
  19.  
  20. if (clientSock != null)
  21. System.out.println("SocketClient is connected");
  22.  
  23. PrintStream outpStream = new PrintStream(clientSock.getOutputStream());
  24. BufferedInputStream binpStream = new BufferedInputStream(clientSock.
  25. getInputStream());
  26.  
  27. String msg = "Hi there CIS 2103, Year 2015\\n";
  28.  
  29. System.out.print("Message sent to the ECHO Server: " + msg);
  30. outpStream.println(msg); // to output stream to the server
  31.  
  32. binpStream.read(response);
  33.  
  34. System.out.print("Message received from the ECHO Server: ");
  35. String instr = "";
  36. for (int k = 0; k < response.length; k++)
  37. {char c = (char)response[k];
  38. if ((int) c!= 13)
  39. instr = instr + c;
  40. }
  41. System.out.println(instr);
  42.  
  43. }
  44. catch (IOException f) {
  45. System.out.println("IOException: " + f);
  46.  
  47. }
  48. catch (Exception g) {
  49. System.out.println("Exception: " + g);
  50. }
  51. finally { clientSock.close();
  52. }
  53.  
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement