Advertisement
Guest User

Processing client with java.net.* for communicating with C#

a guest
Aug 1st, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. //The socket
  5. Socket s;
  6.  
  7. //For write
  8. OutputStream os;
  9. OutputStreamWriter osw;
  10. BufferedWriter bw;
  11.  
  12. //For read
  13. InputStream is;
  14. InputStreamReader isr;
  15. BufferedReader br;
  16.  
  17. //The received message
  18. String data;
  19.  
  20. void setup(){
  21.     size(0,0); //No matter
  22.  
  23.     //Setup the client (java.net socket...) connect in the server thread if it's not connected..
  24.     s = new Socket();
  25.  
  26. }
  27.  
  28. void draw(){
  29.  
  30.     thread("dataReceiverThread"); //Run the networking in another thread every frame (instead of while loop in another thread)
  31.  
  32. }
  33.  
  34. void dataReceiverThread() {
  35.  try {
  36.  
  37.    if (!s.isConnected()){
  38.      s.connect(new InetSocketAddress("localhost", 8080));
  39.    }
  40.  
  41.   //Get the output stream for write message
  42.   if (os == null) {
  43.  
  44.    os = s.getOutputStream();
  45.    osw = new OutputStreamWriter(os);
  46.    bw = new BufferedWriter(osw);
  47.  
  48.   }
  49.  
  50.   //Get the input stream for reading
  51.   if (is == null) {
  52.  
  53.    is = s.getInputStream();
  54.    isr = new InputStreamReader(is);
  55.    br = new BufferedReader(isr);
  56.  
  57.   }
  58.  
  59.   data = br.readLine(); //I can't read anything 'cause the socket is forced to close (C# throws the SocketClosedException 'cause its    
  60.                        //cant communicate with a closed socket.)
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement