I customize this code to my real-time simulation to send an object, but I'm running after each other in exceptions Because this code is blocked, in nature how can this code be converted to block with Javanese
/ * * author * / import java.io.IOException; Import java.io.ObjectOutputStream; Import java.net.InetSocketAddress; Import java.nio.channels.ServerSocketChannel; Import java.nio.channels.SocketChannel; Public Class CleanSandler Runable {Private SIM Manager SM; Private ballstate ballstate = new ballstate (10, 5); Private serverSet channel channel ssChannel; Personal threads tRunSer = new thread (this, "servercreatectread"); Public stationary zero main (string [] args throws IOException {cleaners server = new cleaner (;); Server.startServer (); } Private Zero startServer () throws IOException {ssChannel = ServerSocketChannel.open (); SsChannel.configureBlocking (true); Int port = 2345; SsChannel.socket (). Dam (new InetSocketAddress (port)); // sm = new simanager (this, bs); // SM.start (); // GameEngine thread starts with tRunSer.start (); } Public Zero Run (try {try SocketChannel sChannel = ssChannel.accept (); While (true) {ObjectOutputStream OOS = New ObjectOutputStream (sChannel.socket (.) GetOutputStream ()); Oos.writeObject (ballState); System.out.println ("Sending String:" "+ ballState.X +" '' + ballstate.Y); oos.close (); System.out.println ("Sender Start"); System.out .println ("connection terminated");}} hold (IOException e) {e.printStackTrace ();}}} Customers: who are constantly looking for answers from the server
import java.io.IOException; import java.io.ObjectInputStream; import java.net.InetSocketAddress; Import java.nio.channels.SocketChannel; runs public category CleanReceiver Runnable {Private SocketChannel sChannel ; Private thread receivertrad = new thread (this, "receiverstread"); Private synchronized A zero start server () throws IOException {sChannel = SocketChannel.open (); sChannel.configureBlocking (true); if (sChannel.connect (New InetSocketAddress ("localhost", 2345))} {receiverThread.start ();}} Zero run () {while {true} {try {ObjectInputStream ois = new ObjectInputStream (sChannel.socket) .getInputStream ()); BallState s = (BallState) ois.readObject (); System.out.println ("The string is: '"+ SX +"' '+ sY); Ois.close (); } Hold (IOException e) {e.printStackTrace (); } Grip (Klassnotfound Expression E) {e.printStackTrace (); } System.out.println ("End Receiver"); }} Public static zero master throws IOException (string [] args, ClassNotFoundException {CleanReceiver RC = new cleanerEasyver (); Rc.startServer (); System.out.println ("End Receiver"); }} Will this design work in the scenario when the server needs to be connected to the customer and simultaneously send a simulation state to the connected client?
jibbylala
Comments
Post a Comment