• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Networked programing code

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is to transfer the X and Y coords of the player to a different computer so i can make a networking adventure code but the networking method is static so i can send the X and Y coords from it. I have tried to remove the static from the method it does not work so i am posting it here. (this is only the server code it will not run proberly if you try and compile it on your comp i just need help how to get the X and Y coords into the networking method)




Any help on this would be great
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

The code is a bit confusing. This class is run as an applet on one or more clients, and as an application on the server?

I don't see any code in the applet that would make a network call to the server. You may want to start reading through the networking chapter of the Java Tutorial. It contains an example of how to create a client/server pair that communicates over sockets.

The server code has a lingering bug: Don't println to a Socket.
 
Jeffrey Bowles
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code itself works as it is now but if i try and take a variable say to transfer player positions i get a error of

"non static variable X cannot be referenced from a static context"

that is on line 98 that i get that error so how do i get rid of that error. The reason i want to this is so i can transfer a players position to another computer on a network running the client code. (which is basically the same) Then draw the player on the map so it can be a networked 2 player game. here is just that code again



The error comes at the line
pw.println("X:"+X );
The X is to transfer the X coords of the player

Thanks
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static code (like the main method) can't access instance variables, only static fields. But since X is set in the client -and not anywhere in the server code- what good does accessing it do?

I think the code would benefit from a strict separation of the client and the server in two different classes.

I'm also a bit sceptical about you saying that the code works - there's no networking code in the client, so how could it work?

(And you will need to tackle the issue discussed in the article I linked to - unless you can guarantee that both client and server will always run on Windows.)
reply
    Bookmark Topic Watch Topic
  • New Topic