Networking with JAVA

I'm trying to make server and client program. server is made of Processing. client is made of JAVA(finally I will make android application that is not made of processing but ADT and connect to Processing server)

Processing Server

import processing.net.*;

Server server;

PFont f;
String incomingMessage = "";

void setup() {
  size(400,200);

  server = new Server(this, 7777);
  f = createFont("Arial",20,true);
}

void draw() {
  background(255);

  fill(0);
  textFont(f);
  textAlign(CENTER);
  text(incomingMessage,width/2,height/2);
  Client client = server.available();

  if (client != null) {
    incomingMessage = client.readString();
    println(client);
    if(mousePressed == true)//If mouse is pressed, message "TRUE" send to Clients
    {
      server.write("TRUE");
    }
  }
}

void serverEvent(Server server, Client client) {
  incomingMessage = "A new client has connected:" + client.ip();
  println(incomingMessage);
}

==========================================

JAVA Client

import processing.core.*;
import processing.net.*;

public class TCPClient
{
    public static void main(String args[])
    {
        Client c = new Client(new PApplet(),"localhost",7777);
        c.write("JAVA-Processing Connection");//This message send to server successfully!

        while(true)
        {
            if(c.available() > 0)
            {
                System.out.println(c.readString());//Cannot receive message from server
                //I do not know how to receive the message "TRUE" from server successfully.
            }
        }
    }
}

do not answer like "use -> class ... extends PApplet" I already used. It works well in JAVA, but I will make android application "extends PApplet"is doesn't work.

If Processing to Processing -> It works well.

Please answer how JAVA client get the message from Processing Server.

Answers

  • How to post code

    when you post your code:

    • in the editor hit ctrl-t to auto format

    • copy it

    • paste it in the browser

    • leave 2 empty lines before it, 1 line after it

    • mark the code (without the 3 empty lines)

    • press C in the small command bar.

  • edited September 2014

    Do not answer like "use -> class ... extends PApplet". I already used.

    I've got somewhat bad news for your "ego":
    Both classes Server & Client demand an actually running PApplet reference! /:)
    Not some lifeless & empty new PApplet() 1 you sent as argument to Client's constructor! [-X
    A proper PApplet is initialized via its static methods PApplet.main() or PApplet.runSketch()!

    Either make your TCPClient class inherit from PApplet or write another class that does it.
    For the latter, TCPClient's gonna need to communicate w/ that PApplet "proxy" in order to get the net data! 8-X

Sign In or Register to comment.