FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   java sockets in processing
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: java sockets in processing  (Read 3802 times)
edwardgeorge

WWW
java sockets in processing
« on: Mar 12th, 2003, 8:52am »

quick question...
i would've assumed that this would send "ping" to the server on 192.168.0.1 (everything connects fine) at each loop but nothing is sent (and no exception is printed).
Why is this?
 
 
import java.net.*;
import java.io.*;
public class MyDemo extends BApplet {
  Socket socket;
  PrintWriter out;
  BufferedReader in;
  void setup()
  {
    size(200, 200);
    background(255);
    try{
      socket=new Socket("192.168.0.1",6668 );
      in=new BufferedReader(
       new InputStreamReader(socket.getInputStream())
      );
      out=new PrintWriter(socket.getOutputStream());
    }catch(Exception e){println(e);}
  }
 
  void loop()
  {
    try{
     out.println("ping");
     out.flush();
    }catch(Exception e){println(e);}
  }
}
 
thanks
Ed_
*whoops: should this have been in 'Programs'?*
« Last Edit: Mar 12th, 2003, 9:13am by edwardgeorge »  

Ed, Suppose, Nottingham, UK
http://www.suppose.co.uk
http://ed.suppose.co.uk
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: java sockets in processing
« Reply #1 on: Mar 12th, 2003, 2:06pm »

Hope this helps...
 
The Server Code:
BFont iLoveThisFont;
String clientAddress = "";
void setup()
{
  size(400,300);
  background(#003366);
  iLoveThisFont = loadFont("Meta-Bold.vlw.gz");
  setFont(iLoveThisFont);
  fill(255);
  noStroke();
}
 
void draw()
{
  scale(1.3);
  try
  {
    ServerSocket ss = new ServerSocket( 8888 );
    text( "Waiting for connection ...", 10, 20);
    repaint();
    Socket s = ss.accept();
    clientAddress = s.getInetAddress() + "";
    text( "Connection established with " + clientAddress, 10, 30 );
    repaint();
    BufferedReader in = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
    PrintStream out = new PrintStream( s.getOutputStream() );    
 
    int lineNum = 50;
 
    String t = in.readLine();
    do
    {
      fill(255,255,255,90);
      text(clientAddress + " sent " + t,30,lineNum);
      fill(255,255,255,80);
      ellipse(15,lineNum-5,5,5);
      repaint();
      lineNum += 10;
      // println( t );
      t = in.readLine();
    }
    while( !(t.equals("stop")) );
    fill(255);
    text("Ping Done.", 10, lineNum+10);
    out.println( "Ping Done." );
    out.flush();
    try
    {
      Thread.sleep( 500 );
    }
    catch ( Exception e )
    {
      println("error: "+e);
    }
    in.close();
    out.close();
  }
  catch ( Exception e )
  {
    println( "Error: " + e );
  }
}

 
The Client Code:
BFont iLoveThisFont;
 
void setup()
{
  size(400,300);
  background(#FFCC00);
  iLoveThisFont = loadFont("Meta-Bold.vlw.gz");
  setFont(iLoveThisFont);
  fill(0);
  noStroke();
}
 
void draw()
{
  scale(1.3);
  try
  {
    String host = "127.0.0.1";   // change
 
    text( "Connecting to " + host + " ...", 10, 20 );
    Socket s = new Socket( host, 8888 );
 
    text( "Connection established to " + s.getInetAddress(), 10, 30 );
 
    BufferedReader in = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
    PrintStream out = new PrintStream( s.getOutputStream() );
 
    int lineNum = 50;
 
    for(int i = 1; i<=15; i++)
    {
      out.println( "a ping with index number " + i );
      out.flush();
 
      fill(0,0,0,90);
      text("Pinging " + s.getInetAddress() + " with index number " + i, 30, lineNum);
      fill(0,0,0,80);
      ellipse(15,lineNum-5,5,5);
      repaint();
      lineNum += 10;
 
      try
      {
        Thread.sleep( 500 );
      } catch ( Exception e )
      {
      }
 
    }
 
    out.println( "stop" );
    out.flush();
 
    String t = in.readLine();
    System.out.println( t );
 
    fill(0);
    text("Ping Done.", 10, lineNum+10);
 
    try
    {
      Thread.sleep( 1000 );
    } catch ( Exception e )
    {
    }
 
    in.close();
    out.close();
  }
  catch ( Exception e )
  {
    println( "Error" + e );
  }
}

 
Copy and paste codes in two separate Processing sessions running simultaneously.
 
Use Beautify command under the Sketch menu (Ctrl+B) to fix the spacing.
 
Obsessive compulsive reminders:
 
Be sure to have Meta-Bold.vlw.gz inside your current sketch's data folder.
 
Run server first before running client.
« Last Edit: Mar 12th, 2003, 2:09pm by Martin »  
edwardgeorge

WWW
Re: java sockets in processing
« Reply #2 on: Mar 13th, 2003, 12:10pm »

thanks martin, but actually i was looking for an explanation as to why the code fails to work, rather than just use someone elses code.
It would be a lot more helpful for learning that's all.
I don't know why my code wouldn't work and would like to know why.
 
Cheers anyway
Ed_
« Last Edit: Mar 13th, 2003, 2:22pm by edwardgeorge »  

Ed, Suppose, Nottingham, UK
http://www.suppose.co.uk
http://ed.suppose.co.uk
edwardgeorge

WWW
Re: java sockets in processing
« Reply #3 on: Mar 13th, 2003, 2:22pm »

Code:
Socket s;
BufferedReader in;
PrintWriter out;
 
void setup(){
 try{
   s = new Socket("127.0.0.1",3000);
   in=new BufferedReader(
     new InputStreamReader(s.getInputStream())
   );
   out=new PrintWriter(s.getOutputStream());    
 }catch(IOException e){println(e);}
}
void loop(){
 try{
  println(in.readLine());
 }catch(IOException e){println(e);}
}
void mousePressed(){
   so.println("test");
   so.flush();
}

 
[lots of stuff removed here - about writing to socket not working...
It appears it might've been the client program (netserver in pure-data) that was causing the outward communication on the socket to not be received.
with a different program it seems to be sending _ great // i won't waste this thread anymore, thanks everyone]
Ed_
« Last Edit: Mar 13th, 2003, 2:56pm by edwardgeorge »  

Ed, Suppose, Nottingham, UK
http://www.suppose.co.uk
http://ed.suppose.co.uk
madmerv
Guest
Email
Re: java sockets in processing
« Reply #4 on: Nov 14th, 2003, 6:42am »

any suggestions for writing a terminal in proce55ing?
 
madmerv
Guest
Email
Re: java sockets in processing
« Reply #5 on: Nov 14th, 2003, 9:36pm »

i've noticed this code's write portion doesnt work
 
Pages: 1 

« Previous topic | Next topic »