Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- //The socket
- Socket s;
- //For write
- OutputStream os;
- OutputStreamWriter osw;
- BufferedWriter bw;
- //For read
- InputStream is;
- InputStreamReader isr;
- BufferedReader br;
- //The received message
- String data;
- void setup(){
- size(0,0); //No matter
- //Setup the client (java.net socket...) connect in the server thread if it's not connected..
- s = new Socket();
- }
- void draw(){
- thread("dataReceiverThread"); //Run the networking in another thread every frame (instead of while loop in another thread)
- }
- void dataReceiverThread() {
- try {
- if (!s.isConnected()){
- s.connect(new InetSocketAddress("localhost", 8080));
- }
- //Get the output stream for write message
- if (os == null) {
- os = s.getOutputStream();
- osw = new OutputStreamWriter(os);
- bw = new BufferedWriter(osw);
- }
- //Get the input stream for reading
- if (is == null) {
- is = s.getInputStream();
- isr = new InputStreamReader(is);
- br = new BufferedReader(isr);
- }
- data = br.readLine(); //I can't read anything 'cause the socket is forced to close (C# throws the SocketClosedException 'cause its
- //cant communicate with a closed socket.)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement