Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

If you need more examples, see

Exploring Network Communication Possibilities

   In the squash game assignment various game elements (racquets,balls, etc.) must to talk with each other over the network. Some of the messages will be critical to the game functioning (player logon messages) and thus will need to be sent via TCP. Other messages which are not critical (ball position updates), so long as not too many are dropped, can be broadcast viaUDP.

  Last week we looked at how a client and server might communicate usingTCP.  Using PrintWriters and BufferedReaders we were ableto send  strings of text over the network. Considering the client andserver don't  need to talk very often - logon and logoff messages mainly-you may be able  to modify this application to handle the initial clientserver communication.  The down side of this is you will have to parsestrings at both ends (client  and server) -which can be time consuming-to get the desired information.  To improve the performance in thisarea you may want to look at the DataOutputStream  and DataInputStreamclasses which allow you to send primitive variable values over the network.

  e.g.)


Server
Client

ServerSocket ss = new ServerSocket(port);

Socket s = ss.accept();
DataInputStream in = new DataInputStream(s.getInputStream());
System.out.println("Client connected");

int i = in.readInt();
System.out.println("Got int : " + i);

s.close();
System.out.println("Connection closed");


Socket s = new Socket(addr, port);

DataOutputStream out = new DataOutputStream(s.getOutputStream());
System.out.println("Connected to server");

int i = 12345;
out.writeInt(i);
System.out.println("Sent integer : " + i);

s.close();
System.out.println("Closed connection");


Client connected
Got int : 12345
Connection closed



Connected to server
Sent integer : 12345
Closed connection



  Via this TCP communication you can send values to the client suchas their  player number, racquet colour, court dimensions, game score,etc.


In the assignment you'll have to send the parameters (via DataOutputStream and DataInputStream objects) to each new player to allow them to construct the game (size of the board ...).

This leads to another problem - identifying messages.

  The server may send a player a message that contains 4 ints - theirplayer  number (1 int) and racquet colour (3 ints). It also may senda message that  contains 30 floats - the squash court parameters. Ifthe client assumes the wrong message it will start parsing the wrong valuesand your program will probably crash horribly. A simple solution to thisproblem is to send an integer (or byte) at the beginning of the message thatidentifies what message type it is, and thus what is contained in the restof the message. For example if the first integer of the message evaluatesto zero the program will know the message is a logon message and that therewill be four more integers following. But if the first integer of the messageevaluates to one the program will know the message is a logoff message andthere will be one more integer in the message.




  Your network implementation will also make use of UDP datagram, allowing  racquets, balls etc. to inform other elements of the game of their position,  orientation etc. without needing direct connections to each of these other  elements. In this section of the application speed is critical. You need to send the critical information in messages that are as small as possible in order to minimise construction and deconstruction time. As such we won't be sending strings over the network. Unfortunately I was not able to find objects similar to DataOutputStream and DataInputStream that allow primitive variables to be converted to byte arrays and then reconstructed from byte arrays. Here are a couple of methods that allow you to convert integers or floats to byte arrays and back again. You should use this lab as an opportunity to test these functions. (btw if anyone does find a way to do this in any of the UDP networking classes I would really like to know.)


   /**
       Convert an integer to an array of bytes
   */
   public byte[] intToBytes(int i)
   {
       byte b[] = {(byte) (i >> 0), (byte) (i >> 8), (byte) (i >> 16), (byte) (i >> 24)};
       return b;
   }

   /**
       Convert an array of bytes to an integer
   */
   public int bytesToInt(byte[] b)
   {
       int i = ((b[0] & 0xff) << 0) + ((b[1] &0xff) << 8) + ((b[2] & 0xff) << 16) + ((b[3] & 0xff) << 24);
       return i;
   }

   /**
       Convert a float to an array of bytes
   */
   public byte[] floatToBytes(float f)
   {
       int i = Float.floatToIntBits(f);
       return intToBytes(i);
   }

   /**
       Convert an array of bytes to a float
   */
   public float bytesToFloat(byte[] b)
   {
       int i = bytesToInt(b);
       float f = Float.intBitsToFloat(i);
       return f;
   }


By using UDP broadcasting out application will not use up as much network resources as it would if we used TCP peer to peer or TCP client server configurations.

Be carreful to use Multicasting instead of UDP Broadcasting as in this example. See the sun trails for a full example :