home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-07 | 2.6 KB | 86 lines |
- // ExampleClient.java
- // Send a current position to server and then get a new position
- // from the server
- import vrml.*;
- import vrml.field.*;
- import vrml.node.*;
-
- import java.net.*;
- import java.io.*;
- import java.util.*;
-
- public class ExampleClient extends Script{
- public final static int PORT = 4130;
- public final static String HOST = "localhost"; // server host name
-
- Socket socket = null;
- DataInputStream in = null; // input stream from server to client
- DataOutputStream out = null; // output sream from client to server
- Browser b; // for displaying error message to user
- Node target = null;
- SFVec3f trans = null;
- float[] coord = null;
- float[] rotation = null;
-
- public void initialize(){
- b = getBrowser();
- // get the reference to 'translation' field of 'target' node
- target = (Node)((SFNode)getField("target")).getValue();
- trans = (SFVec3f)target.getExposedField("translation");
- coord = new float[3];
- rotation = new float[4];
-
- try {
- // open network and input/output stream
- socket = new Socket("localhost", PORT);
- in = new DataInputStream(socket.getInputStream());
- out = new DataOutputStream(socket.getOutputStream());
- } catch (UnknownHostException e) {
- b.setDescription("Unknown host: " + HOST);
- } catch (Exception e) {
- b.setDescription("Connection error");
- }
- }
-
- public void processEvent(Event ev) {
- ((ConstSFRotation)ev.getValue()).getValue(rotation); // get rotation angle
-
- try {
- // get current position
- trans.getValue(coord);
-
- // send collision angle
- out.writeFloat(rotation[3]);
-
- // send current position to server
- out.writeFloat(coord[0]);
- out.writeFloat(coord[1]);
- out.writeFloat(coord[2]);
-
- // receive new position from server
- coord[0] = in.readFloat();
- coord[1] = in.readFloat();
- coord[2] = in.readFloat();
-
- // display new position
- b.setDescription("position: " + coord[0] + "," +
- coord[1] + "," + coord[2]);
-
- // set new position
- trans.setValue(coord);
- } catch (IOException e) {
- b.setDescription("IOException: " + e);
- }
- }
-
- public void shutdown(){ // closing stream and network
- try {
- out.close();
- in.close();
- socket.close();
- } catch (Exception e) {
- b.setDescription("Connection close error");
- }
- }
- }
-