I have 2 classes, where I have one to send commands to the socket, the other that receives (and sometimes responds to commands).
Does it need to sync? Or is it not necessary?
Both classes work in their threads with the socket object passed to each of them as an argument in thread.start();
Is this the right way to do this, or can I do something more efficient?
Would it be likely to cause errors? Sending part:
public void run(){ send_chatline("roomf"); int vvv = 0; while (this.socket.isConnected()){ try{ Thread.sleep(10000); vvv++; Thread.sleep(10000); send_chatline("alive"); Thread.sleep(10000); if (vvv == 1) { this.socket.flush(); this.socket.send("T,-1," + this.playerid * 3); this.socket.flush(); } } catch (InterruptedException e) { e.printStackTrace(); } } }
But remember! The recieveFromSock class also sometimes writes when certain commands appear.
The only function of sendTosock is to maintain a connection on the network (to be able to remain on the network).
source share