We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Newbie-Alert: P2P with Processing
Page Index Toggle Pages: 1
Newbie-Alert: P2P with Processing (Read 2424 times)
Newbie-Alert: P2P with Processing
Feb 15th, 2007, 9:15am
 
Hi Guys,

i've just started experimenting with Processing, which is a great possibility to combine the strength of Java and the vision of designers. So here is my problem. I want to code clients, which can communicate with each other. For example exchange instant messanges and so on. I know, that there is processing.net, which offers functionality like the Server- and Client-Class. But I couldn't handle it, to get two clients running.

I would appreciate a short description of the big deal.

Thanks in forward
Marco
Re: Newbie-Alert: P2P with Processing
Reply #1 - Feb 15th, 2007, 10:32am
 
For better understanding the code. I'm trying to setup an client-server-environment. I thought, that starting the jar-File twice will result in two clients, which IP-addresses are printed out, aren't they?

import processing.net.*;

public class Test extends PApplet {

 Server testServer;
 Client testClient;

 void setup() {

   // setting up environment
   size(screen.width, screen.height);
   background(255);
   smooth();

   // setting up server and client
   testServer = new Server(this, 1234);
   testClient = new Client(this, "localhost", 1234);
 }

 void draw() {
 }

 void serverEvent(Server testServer, Client testClient) {
   //println("New client: " + testClient.ip());
   testServer.write("Client IP: " + testClient.ip());
 }

 void clientEvent(Client testClient) {
   if (testClient.available() > 0) {
     println(testClient.readString());
   }
 }

}
Re: Newbie-Alert: P2P with Processing
Reply #2 - Feb 15th, 2007, 11:02am
 
this example is hybrid, it has a server and a client. running it twice on the same machine will result in two servers and two clients, all listening to the same port. no good.

you need one sketch that is the server and one that is the client. server should run only once, clients can be run multiple times. be aware that this example-server assumes only one client connecting, so you will have to add support for multiples to the server sketch.

F
Re: Newbie-Alert: P2P with Processing
Reply #3 - Feb 15th, 2007, 11:05am
 
for the P2P idea, i guess you will need something like a tracker where the clients register their IPs to be discovered by the other clients.

F
Re: Newbie-Alert: P2P with Processing
Reply #4 - Feb 15th, 2007, 11:06am
 
Running that code will connect to itself. You're starting a server on port 1234 then connecting to it yourself, so the only person you'll be able to talk to is yourself. Running a second instance will cause an error however, as port 1234 will already be in use by the first instance, so the server part of #2 will be unable to start. The second client however would be able to connect to the first instance.

Also, if you use "localhost" you'll always get the IP 127.0.0.1 as "localhost" works over a different interface to normal network traffic.

A server can actually send messages as well as recieve, so only one of the two programs needs to have a server, and the other a client. The problem is that only one can initiate a connection, and the other needs to already be running.

The way to make it so any side can start a connection is for all instances to be running a client and a server, and for the client (or clients for multiple connections) to connect to the other's server.

This is to some extent how P2P works, except that there's some magic middleware that keeps track of who's available to connect to, and how to connect to them, but that's a whole order of magnitude more difficult to write.

Re: Newbie-Alert: P2P with Processing
Reply #5 - Feb 15th, 2007, 11:11am
 
Okay. Now I understand what my problem was. But how do I manage it to setup a network with a multiple number of clients, which can communicate with each other? Is it possible in Processing by the way? Sorry for the nerd questions, but I'm really new into Processing and I can't remember the date of my last Java Development
Smiley

Marco
Re: Newbie-Alert: P2P with Processing
Reply #6 - Feb 15th, 2007, 12:36pm
 
If you want multiple clients then I would suggest writing 2 seperate programs, one server, and one client. Run the server once, and run the client multiple times on different systems.

Make the clients all connect to the one server, and have it forward messages from client to client.
Re: Newbie-Alert: P2P with Processing
Reply #7 - Feb 15th, 2007, 12:47pm
 
Two Classes extending PApplet: one as Server, which is run once, and as many as wished as Client, which connect to the Server.

So we have the following code to start the server and a client:
Server testServer = new Server(this, 1234);
Client testClient = new Client(this, "localhost", 1234);

And which host do I need to specify with starting a Client? Do the Clients get different IP-Adresses, so that I can distinguish them? As I see at the moment, there is also a lack in Client-Server-Knowledge in general :(

Thanks a lot by the way
Marco
Re: Newbie-Alert: P2P with Processing
Reply #8 - Feb 15th, 2007, 1:21pm
 
The server only needs:

Server testServer = new Server(this, 1234);

It's not a client, so doesn't need the client part.

The client needs:
Client testClient = new Client(this, "localhost", 1234);
Except, you need to change "localhost" to the IP or real hostname of the computer you're going to be running the server on.

The client will have whatever IP is the IP of the computer that's connecting to the server.

You can have the clients distinguish themselves either by the IP they come from, or by the data they send to the server.
Re: Newbie-Alert: P2P with Processing
Reply #9 - Feb 15th, 2007, 2:15pm
 
content of testServer.pde


import processing.net.*;

public class testServer extends PApplet {

 private Server server;
 private int port = 1234;

 void setup() {
   server = new Server(this, port);
   loop();
 }

 void draw() {
 }

 void serverEvent(Server server, Client client) {
   println("We have a new client: " + client.ip());
 }
 
}


contents of testClient.pde


import processing.net.*;

public class testClient extends PApplet {

 private Client client;
 private String host = "<real ip-address>";
 private int port = 1234;

 void setup() {
   client = new Client(this, host, port);
   loop();
 }

 void draw() {
 }

 void clientEvent(Client client) {
   print("Server Says:  ");
   String dataIn = client.readString();
   println(dataIn);
 }

}

If I now try to start the testServer.jar, it immediatly quits without any action. Did I made a mistake anywhere? Is my structure correct now?

Marco
Re: Newbie-Alert: P2P with Processing
Reply #10 - Feb 15th, 2007, 2:30pm
 
The code is fine, you're just running it wrong I think, the jar files created are applets, not runnable java programs. They only work in a browser, not when double-clicked on or the like.

To create a program you can run from the desktop, use the "Export Application" option.

You'll also need to add a size(200,200); (or whatever you want) to the start of the setup in both, or they'll quit without starting, they need a window to display stuff in.
You'll also need to get them to print their messages to screen with text() instead of println, since that goes to standard out, and generally dissapears if you're running the application directly.
Re: Newbie-Alert: P2P with Processing
Reply #11 - Feb 15th, 2007, 2:50pm
 
I'm using a Mac, so it normally works by double-clicking the jar Smiley I tried "Export Application" with set size anyway. The error message said, that there is no void main(String[]) method in my testServer-Class. That's true, but business as usual in Processing I thought. Any other ideas?

Thanks in forward
Marco
Re: Newbie-Alert: P2P with Processing
Reply #12 - Feb 15th, 2007, 2:55pm
 
Take out the "public class testServer extends PApplet" stuff, you only need that sort of thing if you're using eclipse to write stuff.
Re: Newbie-Alert: P2P with Processing
Reply #13 - Feb 15th, 2007, 3:42pm
 
Now it works fine. Thank you very much. There is only one disadvantage. If I don't have a Tomcat-Server, which is reachable under a static ip or a hostname, I have to specify the IP-Address of the computer the Server Application is running on by hand in the Client-Class. But anyway the first problem was solved. Thanks a lot.

Marco
Re: Newbie-Alert: P2P with Processing
Reply #14 - Feb 15th, 2007, 4:08pm
 
Just to clear up a point, processing programs will not run in tomcat.. They're not server-side java applications that will run under tomcat, they're either a standalone application, or a browser applet.
Page Index Toggle Pages: 1