Posted By

mouseroot on 11/14/10


Tagged


Versions (?)

Sockets in Java Input and Output


 / Published in: Java
 

A simple implantation of sockets input and output

  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. class ....
  5.  
  6. static String domain;
  7. static int port;
  8. static InetAddress addr;
  9. static BufferedReader in;
  10. static BufferedWriter out;
  11. static Socket sock;
  12. static String line;
  13.  
  14. public ....(String d,int p) {
  15. domain = d;
  16. port = p;
  17. try {
  18. addr = InetAddress.getByName(domain);
  19. sock = new Socket(addr,port);
  20. in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  21. out =new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
  22.  
  23. }
  24. catch(Exception ee)
  25. {
  26. JOptionPane.showMessageDialog(null,"Constructor Error: " + ee);
  27. }
  28.  
  29. }
  30.  
  31. //Data to Output to the socket
  32. try {
  33. out.write("Data to send");
  34. out.flush();
  35. }
  36. catch(Exception ee)
  37. {}
  38.  
  39. //read data from socket
  40.  
  41. try {
  42. while((line = in.readline()) != null)
  43. {
  44. System.out.println(line);
  45. }
  46. }catch(Exception e)
  47. {}

Report this snippet  

You need to login to post a comment.