multithreading - Client/Server Socket Java (pattern MVC) doesn't send correct information to the Client -



multithreading - Client/Server Socket Java (pattern MVC) doesn't send correct information to the Client -

i'm writing game i'm problem server/client socket. game works when play in local , when play online rmi. problem there when seek implements socket (i must socket). how i've implemented code (i utilize pattern model-view-controller):

server: class socketservercreate: create server , create thread receive connection (socketserverconnection) create thread receive message client (socketserverreceive). server instantiate object implements observer called model send info client (socketserveroutput).

client: class socketclientcreate: open connection server, create thread receive message server , instantiate class implements observer called view send info server.

the problem update method of server in class socketserveroutput called lot of time, must called 1 time send message client. clare post code of server , client.

socketservercreate

/* * class creates server. */ public class socketservercreate { private static final int port = 5000; private arraylist<objectoutputstream> arrayplayersocket; private socketserveroutput outputserver; public socketservercreate(model model){ serversocket serversocket; controller controller = new controller(model); arrayplayersocket = new arraylist<objectoutputstream>(); seek { serversocket = new serversocket(port); } grab (ioexception e) { system.out.println("error during creation of server.\n" + e.getstacktrace()); return; } outputserver = new socketserveroutput(this, controller); socketserverconnection connectionserver = new socketserverconnection(this, serversocket, controller); thread receiveserverthread = new thread(connectionserver); receiveserverthread.start(); } public void addplayersocket(objectoutputstream outputstream){ arrayplayersocket.add(outputstream); } public arraylist<objectoutputstream> getarrayplayersocket(){ homecoming (arraylist<objectoutputstream>) arrayplayersocket; } public socketserveroutput getsocketserveroutput(){ homecoming outputserver; } }

socketserveroutput

/* * class manages object must send client. */ public class socketserveroutput implements observer { arraylist<objectoutputstream> arrayplayerssocket; socketservercreate socketservercreate; controller controller; private objectoutputstream outputmessage = null; public socketserveroutput(socketservercreate socketservercreate, controller controller){ this.socketservercreate = socketservercreate; this.controller = controller; } @override public void update(observable o, object arg) { if(o.equals(controller.getmodel().getmodelchanges())){ arrayplayerssocket = socketservercreate.getarrayplayersocket(); iterator<objectoutputstream> arrayiterator = arrayplayerssocket.iterator(); while(arrayiterator.hasnext()){ outputmessage = (objectoutputstream)arrayiterator.next(); seek { outputmessage.writeobject(arg); outputmessage.flush(); /*if(arrayplayerssocket.indexof(outputmessage) != controller.getturn().getcurrentplayer()){ modelchanges modelchanges = new modelchanges(); modelchanges.spegnibottonionline(false, false, false); outputmessage.writeobject(modelchanges); outputmessage.flush(); }*/ } grab (ioexception e) { system.out.println("ioexception while server sends object client!"); return; } } } } }

socketserverconnection

public class socketserverconnection implements runnable { private serversocket serversocket; private socketservercreate socketservercreate; private controller controller; public socketserverconnection(socketservercreate socketservercreate, serversocket serversocket, controller controller){ this.socketservercreate = socketservercreate; this.serversocket = serversocket; this.controller = controller; } @override public void run() { while(true){ seek { socket lastsocket = serversocket.accept(); objectoutputstream outputmessage = new objectoutputstream(lastsocket.getoutputstream()); socketservercreate.addplayersocket(outputmessage); //send view index client setted in server array outputmessage.writeobject((integer)socketservercreate.getarrayplayersocket().lastindexof(outputmessage)); outputmessage.flush(); socketserverreceive receiveserver = new socketserverreceive(lastsocket, controller); thread receivethread = new thread(receiveserver); receivethread.start(); } grab (ioexception e) { system.out.println("ioexception while server take client.\n" + e.getcause()); } } } }

socketserverreceive

public class socketserverreceive implements runnable { private objectinputstream inputmessage = null; private controller controller; private object message; public socketserverreceive(socket socket, controller controller){ this.controller = controller; system.out.println("connection created client has ip address: " + socket.getinetaddress()); if(inputmessage == null){ seek { inputmessage = new objectinputstream(socket.getinputstream()); } grab (ioexception e) { system.out.println("ioexception while server creating stream client.\n" + e.getcause()); } } } @override public void run() { while(true){ seek { message = inputmessage.readobject(); controller.receivemessageview(message); } grab (classnotfoundexception e) { system.out.println("classnotfoundexception in socketserverreceive.\n" + e.getstacktrace()); return; } grab (ioexception e) { } } } }

socketclientcreate

/* * class starts connection server , creates 2 threads, * 1 manage input request, other manage output request */ public class socketclientcreate { private static final int port = 5000; private socket serversocket; private int arrayindex; private socketclientoutput outputclient; public socketclientcreate(string ipaddress, view view){ seek { serversocket = new socket(ipaddress, port); } grab (unknownhostexception e) { system.out.println("unknownhostexception during connection of client @ server"); return; } grab (ioexception e) { system.out.println("ioexception during connection of client @ server"); return; } outputclient = new socketclientoutput(this, serversocket, view); socketclientreceive receiveclient = new socketclientreceive(this, serversocket, view); thread receivethread = new thread(receiveclient); receivethread.start(); } public socketclientoutput getoutputclient(){ homecoming outputclient; } public void setarrayindex (integer arrayindex){ this.arrayindex = arrayindex; } public int getarrayindex(){ homecoming arrayindex; } }

socketclientoutput

/* * class manages message client must send server. methods * called update method. */ public class socketclientoutput implements observer { private objectoutputstream outputmessage = null; private view view; private socketclientcreate socketclientcreate; public socketclientoutput(socketclientcreate socketclientcreate, socket server, view view){ this.view = view; this.socketclientcreate = socketclientcreate; if(outputmessage == null){ seek { outputmessage = new objectoutputstream(server.getoutputstream()); } grab (ioexception e) { system.out.println("ioexception @ opening of output stream between client , server.\n" + e.getcause()); return; } } } /* * update receive object view , send object server. */ @override public void update(observable o, object arg) { if(o.equals(view)){ /* * gestire indice creazione giocatore */ seek { outputmessage.writeobject(arg); outputmessage.flush(); } grab (ioexception e) { system.out.println("ioexception while client sends object server.\n" + e.getcause()); return; } } } }

socketclientreceive

/* * class waits object server , modify view. */ public class socketclientreceive implements runnable { private objectinputstream inputmessage = null; private view view; private socketclientcreate socketclientcreate; private object message; public socketclientreceive(socketclientcreate socketclientcreate, socket server, view view){ this.view = view; this.socketclientcreate = socketclientcreate; if(inputmessage == null){ seek { inputmessage = new objectinputstream(server.getinputstream()); } grab (ioexception e) { system.out.println("ioexception @ opening of input stream between client , server.\n"+e.getstacktrace()); } } } @override public void run() { while(true){ seek { message = inputmessage.readobject(); if(message instanceof integer){ socketclientcreate.setarrayindex((integer)message); }else if(message instanceof modelchanges){ modelchanges modelchanges = (modelchanges)message; view.getmappa().repaintview(modelchanges); } } grab (classnotfoundexception e) { system.out.println("classnotfoundexception waiting message server!\n"+e.getstacktrace()); return; } grab (ioexception e) { } } } }

i advise create boolean, enabled default, turn false when server sends message. then, when server intends send message, turn should turn true. contact me if had in mind, or if looking else.

private object lastmessage; private boolean hassentmessage; @override public void update(observable o, object arg) { //check if message same lastly message. if(!arg.equals(lastmessage)) hassentmessage = false; //also check if has sent message. if(o.equals(controller.getmodel().getmodelchanges()) && !hassentmessage){ arrayplayerssocket = socketservercreate.getarrayplayersocket(); iterator<objectoutputstream> arrayiterator = arrayplayerssocket.iterator(); while(arrayiterator.hasnext()){ outputmessage = (objectoutputstream)arrayiterator.next(); seek { outputmessage.writeobject(arg); outputmessage.flush(); //state message has been sent. hassentmessage = true; lastmessage = arg; /*if(arrayplayerssocket.indexof(outputmessage) != controller.getturn().getcurrentplayer()){ modelchanges modelchanges = new modelchanges(); modelchanges.spegnibottonionline(false, false, false); outputmessage.writeobject(modelchanges); outputmessage.flush(); }*/ } grab (ioexception e) { system.out.println("ioexception while server sends object client!"); return; } } } }

java multithreading sockets

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -