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 › does anybody use the net library got a problem...
Page Index Toggle Pages: 1
does anybody use the net library? got a problem... (Read 906 times)
does anybody use the net library? got a problem...
Apr 18th, 2009, 5:40pm
 
I'm trying to make a little multiplayer rts by extending an old sketch i made. (original at http://openprocessing.org/visuals/?visualID=843)

I'm using the processing net library to try to transfer the x and y position of all the sprites on the screen and then receive them on the client side and overwrite the client side sprite's positions just before rendering. Just the first test before i get any deeper into net code.

i more or less copied the code from the sharedcanvas examples, and im concatenating the x and y values with spaces between them on to a string then sending them all off, once per frame (i have tried slowing this down, too). if i run the program with 50 sprites to control and i take a look at the number of values in the string before sending it out, it always has 100 values which is one x and y for all 50 sprites. On the other end it seems to receive generally around 100 but it ranges from 80 to 140. The sprites on the client side mostly resemble those on the server, but they flicker and do weird things. It seems like something is screwing up in the net library itself, because i can see the string going out the server and coming in the other side, and they don't always match.

Is there some sort of buffer concept i'm missing?
Re: does anybody use the net library? got a problem...
Reply #1 - Apr 18th, 2009, 5:51pm
 
as an example, if i save all the values in the string to a text file before sending out from the server, i see:
379
451
196
350
276
256
118
4
207
266
454
386
195
456
294
395
470
423
230
333
270
432
28
413
214
386
49
49
252
481
143
231
80
23
144
272
124
350
180
214
239
271
316
452
264
311
331
379
178
295
463
67
254
392
127
309
108
254
421
64
5
381
473
470
174
384
179
254
404
482
5
446
374
262
160
331
409
26
481
103
121
43
375
386
349
212
391
299
264
354
334
257
299
333
218
227
217
304
300
287


but when i read the string in on the client side it looks like this after i print it to a file:
379
451
196
350
276
256
118
4
207
266
454
386
195
456
294
395
470
423
230
333
270
432
28
413
214
386
49
49
252
481
143
231
80
23
144
272
124
350
180
214
239
271
316
452
264
311
331
379
178
295
463
67
254
392
127
309
108
254
421
64
5
381
473
470
174
384
179
254
404
482
5
446
374
262
160
331
409
26
481
103
121
43
375
386
349
212
391
299
264
354
334
257
299
333
218
227
217
304
300
287

498
0
498
0


....where are these extra values coming from at the end? when i try adding a \n and then parsing it out like the sharedcanvas examples, the newline is often not at the end and it crashes with a subscript out of range error. and when i print to the console the postion it finds the newline at, ive had it give many different values. sometimes it gets cut off, sometimes its in the middle...

any ideas?
Re: does anybody use the net library? got a problem...
Reply #2 - May 12th, 2009, 6:40am
 
bump!

someone must know this. if nobody has the net library working up to this point, it must mean nobody uses the damn net library.

sending one string and getting it on the other end HAS to be the simplest way to use this.

or is the net library just in processing so it can say it has a net library? im wondering because there is the odd broken thing in the main libraries, maybe net was rushed? i've had an easier time with some of the external libraries for processing, even. pcontrol, interfascia, sdrop, id3, even p5sunflow! a raytracer works damn it and the net library doesn't?

sorry, just trying to work the system, i really want to get a project started based on this. its the linux troll principal. if you ask about a problem, nobody cares. if you post "linux sucks, on windows i used to _____" you get all the linux nerds screaming the answer at you. well processing sucks right now. any processing trolls want to prove me wrong?
Re: does anybody use the net library? got a problem...
Reply #3 - May 12th, 2009, 8:17am
 
Could you post a piece of the program that does this?

I just don't think we have much to go on...
(btw, I have had success sending messages with the net library)

Edit: preferably the host side, since it is writing the file.
Re: does anybody use the net library? got a problem...
Reply #4 - May 12th, 2009, 9:07am
 
As said, you post some general remarks and a bunch of unreadable numbers (too much of them!) and expect a sane answer... Smiley
The examples given using this library work, I saw others on the forum, so it does work. In my experience, people yelling it is a language/library bug often exhibit actually a bug in their code... But not always! Smiley
So, in short, please show us some code (simplified to the essential if it is complex, the latter exercise sometime helps pinning out the problem).

Well, it appears your provocation is working, after all! Wink
Re: does anybody use the net library? got a problem...
Reply #5 - May 12th, 2009, 12:30pm
 
haha, sorry, had to try...

since you need both the server and the client program i thought nobody would be crazy enough to download them both and test it out, i mean i don't expect anyone to spend real time on this. i wish i could just throw them both up on openprocessing.org and have it work that easily, but it wont.

ill copy the server and client's main file contents here and if anyone is adventurous enough to copy the code into my sketch at http://openprocessing.org/visuals/?visualID=843 it will work but maybe someone will be able to see the problem just by looking at it?

here is the server:
Code:
//import processing.video.*;

//MovieMaker mm;
import processing.net.*;

Server s;
Client c;
String input;
int data[];

PImage redDudeImage, blueDudeImage;
int numDudes = 50;
dude[] dudes = new dude[numDudes];
int x1, y1, x2, y2;
boolean selected = false;
boolean selectTeamOne = true;
boolean attack = false;
boolean blur = true;
boolean recording = false;
int counter = 0;

void setup()
{
 x1 = 0;
 y1 = 0;
 x2 = 0;
 y2 = 0;
 selected = false;
 frameRate(60);
 size(500,500);
 noFill();
 redDudeImage = loadImage("dude.png");
 blueDudeImage = loadImage("bluedude.png");
 for(int i = 0; i < numDudes; i++)
 {
   dudes[i] = new dude();
 }
 s = new Server(this, 12345); // Start a simple server on a port
 //mm = new MovieMaker(this, width,height, "drawing.mov", 25, MovieMaker.ANIMATION, MovieMaker.HIGH);
}

void draw()
{
 counter++;
 if(blur)
 {
   noStroke();
   fill(255,150);
   rect(0,0,width, height);
 }
 else
 {
   background(color(255,255,255));
 }
 String locations = new String("");
 int numlocations = 0;
 for(int i = 0; i < numDudes; i++)
 {
   if(dudes[i].active)
   {
     dudes[i].update();
     dudes[i].render();
   }
   else
   {
     dudes[i] = new dude();
   }
   numlocations+=2;
   locations = locations + (int)dudes[i].position.x + " " + (int)dudes[i].position.y + " ";
 }
 locations = locations + "\n";
 String stringlist[] = split(locations, ' ');
 saveStrings("out.txt",stringlist);
 s.write(locations);

 //println(stringlist.length);
 strokeWeight(2);
 if(selectTeamOne)
 {
   stroke(255,0,0);
 }
 else
 {
   stroke(0,0,255);
 }
 
 if(mousePressed)
 {
   if(mouseButton==LEFT)
   {
     rect(x1,y1,mouseX-x1,mouseY-y1);
   }
 }
 else
 {
   if(selected)
   {
     //rect(x1,y1,x2-x1,y2-y1);
   }
 }
 if(recording)
 {
   //mm.addFrame();
 }
 
 s.write(pmouseX + " " + pmouseY + " " + mouseX + " " + mouseY + "\n");
}

void mousePressed()
{
 selected = false;
 if(mouseButton == LEFT)
 {
   x1 = mouseX;
   y1 = mouseY;
 }
}

void mouseReleased()
{
 if(mouseButton == LEFT)
 {
   if(abs(mouseX-x1)>20 || abs(mouseY-y1)>20)
   {
     x2 = mouseX;
     y2 = mouseY;
     if(x2<x1)
     {
       int temp = x1;
       x1 = x2;
       x2 = temp;
     }
     if(y2<y1)
     {
       int temp = y1;
       y1 = y2;
       y2 = temp;
     }
     selected = true;
   }
   else
   {
     selected = false;
     deselectDudes();
   }
   if(selected)
   {
     for(int i = 0; i < numDudes; i++)
     {
       if(dudes[i].position.x > x1 && dudes[i].position.x < x2 && dudes[i].position.y > y1 && dudes[i].position.y < y2)
       {
         if(selectTeamOne && dudes[i].team == 0)
         {
           dudes[i].selected = true;
         }
         else if(!selectTeamOne && dudes[i].team == 1)
         {
           dudes[i].selected = true;
         }
       }
       else
       {
         dudes[i].selected = false;
       }
     }
   }
 }
 if(mouseButton == RIGHT)
 {
   for(int i = 0; i < numDudes; i++)
   {
     PVector target = new PVector();
     target.x = mouseX;
     target.y = mouseY;
     dudes[i].attack = true;
     dudes[i].move(target);
     if(attack)
     {
       dudes[i].attack = true;
     }
     else
     {
      dudes[i].attack = false;
     }
   }
   attack = false;
 }
}

void deselectDudes()
{
 for(int i = 0; i < numDudes; i++)
 {
   dudes[i].selected = false;
 }
}

void keyPressed()
{
 if(key=='a'||key=='A')
 {
   attack = true;
 }
 if(key=='1')
 {
   if(!selectTeamOne)
   {
     selectTeamOne = true;
     deselectDudes();
   }
 }
 if(key=='2')
 {
   if(selectTeamOne)
   {
     selectTeamOne = false;
     deselectDudes();
   }
 }
 if (key == ' ')
 {
   if(!recording)
   {
     recording=true;
   }
   else
   {
     // Finish the movie if space bar is pressed
     //mm.finish();
    // Quit running the sketch once the file is written
     exit();
   }
 }
}


oops, past the max length.. i'll post the client in another message...
Re: does anybody use the net library? got a problem...
Reply #6 - May 12th, 2009, 12:31pm
 
the client:
Code:
//import processing.video.*;

//MovieMaker mm;
import processing.net.*;

Client c;
String input;
int data[];

PImage redDudeImage, blueDudeImage;
int numDudes = 50;
dude[] dudes = new dude[numDudes];
int x1, y1, x2, y2;
boolean selected = false;
boolean selectTeamOne = true;
boolean attack = false;
boolean blur = true;
boolean recording = false;

void setup()
{
 x1 = 0;
 y1 = 0;
 x2 = 0;
 y2 = 0;
 selected = false;
 frameRate(60);
 size(500,500);
 noFill();
 redDudeImage = loadImage("dude.png");
 blueDudeImage = loadImage("bluedude.png");
 for(int i = 0; i < numDudes; i++)
 {
   dudes[i] = new dude();
 }
 c = new Client(this, "127.0.0.1", 12345); // Replace with your server's IP and port
 //mm = new MovieMaker(this, width,height, "drawing.mov", 25, MovieMaker.ANIMATION, MovieMaker.HIGH);
}

void draw()
{
 if(blur)
 {
   noStroke();
   fill(255,150);
   rect(0,0,width, height);
 }
 else
 {
   background(color(255,255,255));
 }

 // Receive data from server
 //if(mousePressed)
 {
   if (c.available() > 0)
   {
     input = c.readString();
     //c.clear();
     //input = input.substring(0, input.indexOf("\n")); // Only up to the newline
     data = int(split(input, ' ')); // Split values into an array
     for(int i = 0; i < numDudes; i++)
     {
         if(i*2<data.length-1)
         {
           dudes[i].position.x=(float)data[i*2];
           dudes[i].position.y=(float)data[i*2+1];
         }
     }
   }
 }
 
 for(int i = 0; i < numDudes; i++)
 {
   if(dudes[i].active)
   {
     //dudes[i].update();
     dudes[i].render();
   }
 }
 
 strokeWeight(2);
 if(selectTeamOne)
 {
   stroke(255,0,0);
 }
 else
 {
   stroke(0,0,255);
 }
 
 if(mousePressed)
 {
   if(mouseButton==LEFT)
   {
     rect(x1,y1,mouseX-x1,mouseY-y1);
   }
 }
 else
 {
   if(selected)
   {
     //rect(x1,y1,x2-x1,y2-y1);
   }
 }
 if(recording)
 {
   //mm.addFrame();
 }

}

void mousePressed()
{
 selected = false;
 if(mouseButton == LEFT)
 {
   x1 = mouseX;
   y1 = mouseY;
 }
}

void mouseReleased()
{
 if(mouseButton == LEFT)
 {
   if(abs(mouseX-x1)>20 || abs(mouseY-y1)>20)
   {
     x2 = mouseX;
     y2 = mouseY;
     if(x2<x1)
     {
       int temp = x1;
       x1 = x2;
       x2 = temp;
     }
     if(y2<y1)
     {
       int temp = y1;
       y1 = y2;
       y2 = temp;
     }
     selected = true;
   }
   else
   {
     selected = false;
     deselectDudes();
   }
   if(selected)
   {
     for(int i = 0; i < numDudes; i++)
     {
       if(dudes[i].position.x > x1 && dudes[i].position.x < x2 && dudes[i].position.y > y1 && dudes[i].position.y < y2)
       {
         if(selectTeamOne && dudes[i].team == 0)
         {
           dudes[i].selected = true;
         }
         else if(!selectTeamOne && dudes[i].team == 1)
         {
           dudes[i].selected = true;
         }
       }
       else
       {
         dudes[i].selected = false;
       }
     }
   }
 }
 if(mouseButton == RIGHT)
 {
   for(int i = 0; i < numDudes; i++)
   {
     PVector target = new PVector();
     target.x = mouseX;
     target.y = mouseY;
     dudes[i].attack = true;
     dudes[i].move(target);
     if(attack)
     {
       dudes[i].attack = true;
     }
     else
     {
      dudes[i].attack = false;
     }
   }
   attack = false;
 }
}

void deselectDudes()
{
 for(int i = 0; i < numDudes; i++)
 {
   dudes[i].selected = false;
 }
}

void keyPressed()
{
 if(key=='a'||key=='A')
 {
   attack = true;
 }
 if(key=='1')
 {
   if(!selectTeamOne)
   {
     selectTeamOne = true;
     deselectDudes();
   }
 }
 if(key=='2')
 {
   if(selectTeamOne)
   {
     selectTeamOne = false;
     deselectDudes();
   }
 }
 if (key == ' ')
 {
   if(!recording)
   {
     recording=true;
   }
   else
   {
     // Finish the movie if space bar is pressed
     //mm.finish();
     // Quit running the sketch once the file is written
     exit();
   }
 }
}
Re: does anybody use the net library? got a problem...
Reply #7 - May 12th, 2009, 2:10pm
 
I am not sure, but you might have the same issue as in Data question thread. It is about serial, but it might not apply, but the APIs are similar...
In short, perhaps you should use a combination of readStringUntil() with clear().
Page Index Toggle Pages: 1