Hello I am doing a job to represent a network and we were asked to use sockets and I ended up using threads for communication. I have three classes: Manager, Server and Switch. When the server connects to the manager it creates a thread to receive messages from the manager and send it to the Switch and when the Switch connects to the Server another thread is created to handle the communication between them, that is, the Server will have two threads with different functionalities, one will handle communication between Manager and Server and the other will handle communication between Server and Switch.
But as they are created separately when I accept the switch's connection, the Receive Receiver thread will execute, but the Receive Receiver thread will stop running. How can I run both at the same time? So when the Server receives a call from the Switch it creates the ReceiverSwictes thread but executes the Receiver Manager thread that was previously created and passes parameters to it.
Class Server
public void ligar(int codigoServidor, String nomeServidor, int portaServidor) throws IOException {
try {
//connection to the manager
Socket ligacaoGestor = new Socket("127.0.0.1", 45000);
//create the input and output channels
saidaGestor = new PrintStream(ligacaoGestor.getOutputStream());
entradaGestor = new BufferedReader(new InputStreamReader(ligacaoGestor.getInputStream()));
}
//sends the data to the manager to add the server to his array
saidaGestor.println("VerificacaoDadosGestor|" + codigoServidor + "|" + nomeServidor + "|" + portaServidor);
//thread to receive messages from the manager
Thread t = new Servidor(entradaGestor, saidaGestor);
t.start();
} catch (IOException ex) {
System.out.println("Erro de IO: " + ex);
janelaServidor.receberDados("Comunicação Indisponível!!! Não foi possivel ligar ao Gestor!!!");
}
//this method is called in the graphical interface and allows sending the data entered by the user to the manager
public void EnviarMsgGestor(String dados) {
saidaGestor.println(dados);
System.out.println("ENVIEI MENSAGEM AO GESTOR!!! " + dados);
}
//creates a socket server to receive connections from switches
public void ReceberSwitches(int portaServidor) throws IOException {
ServerSocket ss = new ServerSocket(portaServidor);
while (true) {
//accepts switch connection
Socket ligacaoSwitch = ss.accept();
//create the input and output channels
saidaSwitch = new PrintStream(ligacaoSwitch.getOutputStream());
entradaSwitch = new BufferedReader(new InputStreamReader(ligacaoSwitch.getInputStream()));
//thread to receive messages from the switches
ReceberSwitches tt = new ReceberSwitches(entradaGestor, saidaGestor, entradaSwitch, saidaSwitch, array_switches);
tt.start();
}
}
//run method that receives messages from the manager
#Override
public void run() {
while (true) {
try {
System.out.println("WAITING FOR SWITCH MESSAGES!!!!");
String dados = entradaGestor.readLine();
if (dados.startsWith("VerificacaoDados")) {
System.out.println("RECEBI A MENSAGEM!!!!");
janelaServidor.receberDados(dados);
}
if (dados.startsWith("inserido")) {
System.out.println("RECEBI A MENSAGEM!!!!");
janelaServidor.receberDados(dados);
}
if (dados.startsWith("DadosSwitch")) {
saidaSwitch.println(dados);
System.out.println("ENVEI PARA O SWITCH");
}
} catch (IOException ex) {
Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Class receive switches(where is the run method to receive messages from the switches)
#Override
public void run() {
while (true) {
try {
String dados = entradaSwitch.readLine();
if (dados.startsWith("DadosSwitch")) {
String[] s = dados.split("[|]");
int codigo = Integer.valueOf(s[1]);
int porta = Integer.valueOf(s[2]);
Boolean res = false;
for (int i = 0; i < array_switches.size(); i++) {
if (codigo == array_switches.get(i).getCodigo()) {
res = true;
}
if (porta == array_switches.get(i).getPorta()) {
res = true;
}
}
if (res == false) {
System.out.println("I WILL SEND SWITCH DATA TO THE MANAGER "+dados);
saidaGestor.println("DadosSwitch|"+codigo+"|"+porta);
} else {
saidaSwitch.println("inserido");
}
}
} catch (IOException ex) {
Logger.getLogger(ReceberSwitches.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
The problem is when the server accepts the connection from the switch goes to the ReceiverSwitches thread and can no longer receive messages from the manager.Because before executing this thread the server is able to receive messages from the manager and send
Anyone who can help me please? Thank you
Related
The situation is as following:
There is a server and a client, which both can initiate a command/message to each other.
Because the server can send a message at any time, the listening on the socket is done in a separate thread (a ListenerThread). This is all fine. The client can send messages and receive at the same time, however, how would you know if a certain response belongs to the command you sent when the server can also initiate a new command/message to notify that something happened?
If I send a message to the server, and the server responds with "OK" in the listening thread. How would you know this is the actual response of the message/command you sent (keeping in mind this is another thread). What if the server received an update from another client and sends that update first.
This like a chat application, though with an actual response for every sent command.
example case:
Let us say that the protocol only consists of a move <playernum> [<x>,<y>] command which indicates that a player has done a move (server notifies client) or that a player wants to do a move (client notifies server). Also, the server responds with "OK" if the move was okay or with "ERR" if not.
Safe state:
move 1 [3,4]
client ---> server
OK
client <--- server
Unsafe state:
move 1 [3,4]
client ---> server
move 2 [1,2]
client <--- server
OK
client <--- server
The client did not expect this response... should responded with OK.
You have a protocol where the client can read one of three possible messages:
OK (The move you made was accepted)
ERR (The move you made was rejected)
move PLAYERID <co-ord1,co-ord2>
It is a reasonable assumption that the messages OK and ERR will only be sent back to the socket which requested a move. However a legal move is broadcast to all other players (perhaps excluding the player who moved).
Since you can receive unsolicited responses (the moves that other players make), you have correctly created a listener thread. You have not described the action your application takes when it receives a move message from another client, but I will assume that your listener thread handles that case. What remains is how to co-ordinate your move commands, and the response to that which will appear in the listener thread.
To synchronize the submission of your move command, and the response, a BlockingQueue (called queue) will be used , and shared between the client and listener. The form of this will be:
Client:
out.println(command); // Where out is the socket PrintWriter stream
String response = queue.take(); // Where queue is the BlockingQueue
// Process either `OK` or `ERR`
Listener Thread:
while ((command = in.readLine()) != null) {
if (command.equalsIgnoreCase("OK") || command.equalsIgnoreCase("ERR"))
queue.put(command);
else if (command.startsWith("move")) {
// Process a move
}
else
System.out.println("Unrecognized command="+command);
}
As you can see, the client simply submits a command, and blocks for the response of "OK" or "ERR". The requirement for processing other player moves has moved into the listener thread.
The listener processes all three conditions (Another player move, an "OK" or an "ERR"). The messages responses "OK" and "ERR" are sent back to the client. A move command is processed separately, and as such is not the responsibility of the client making the moves.
Below I have mocked working code which demonstrates these concepts. The server will randomly (with equal probability) respond with:
OK
ERR
A multiline response which includes OK and another player's move
Code:
public class MoveGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String command = "";
new Thread(new MoveServer()).start();
Socket socket = null;
PrintWriter out = null;
BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);
try {
socket = new Socket("localhost", 5001);
out = new PrintWriter(socket.getOutputStream(), true);
new Thread(new ClientReader(socket, queue)).start();
while (!command.equals("quit")) {
command = scanner.nextLine();
if (command.startsWith("move")) {
out.println(command);
String response = queue.take();
System.out.println("Client got response="+response);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
scanner.close();
out.close();
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static class ClientReader implements Runnable {
private final Socket socket;
private final BlockingQueue<String> queue;
public ClientReader(Socket socket, BlockingQueue<String> queue) {
super();
this.socket = socket;
this.queue = queue;
}
#Override
public void run() {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String command;
while ((command = in.readLine()) != null) {
if (command.equalsIgnoreCase("OK") || command.equalsIgnoreCase("ERR"))
queue.put(command);
else if (command.startsWith("move")) {
System.out.println("A player made a move: command="+command);
}
else
System.out.println("Unrecognized command="+command);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class MoveServer implements Runnable {
#Override
public void run() {
Random random = new Random();
Socket socket = null;
try {
ServerSocket ss = new ServerSocket(5001);
while (true) {
System.out.println("Listening for new connections");
socket = ss.accept();
System.out.println("New session has started");
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String command;
while ((command = in.readLine()) != null) {
System.out.println("Got command="+command);
int responseType = random.nextInt(3);
if (responseType == 0)
out.println("OK");
else if (responseType == 1)
out.println("ERR");
else {
out.println("move 1 [3,4]");
out.println("OK");
}
}
in.close();
out.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
I just started working on Sockets with both client and server threads in the same Node. Below is my code structure -
I have a Node class which makes connections to different Nodes. It sends REQUEST messages to the Nodes and waits till it gets REPLY from all the nodes tracked using the outstandingreplies variable which is static and volatile, also has synchronised getters and setters.
Server Class spawns Processor thread which processes each message that it hears.
It modifies certain variables which furthers the code execution. On getting a REPLY message, it decrements outstandingreplies variable. It also sends messages to node which just sent the messages based on certain conditions.
Below is my code, I have removed certain details.
Node Class
// Creates connections to other Nodes(machines) and sends messages
public class Node {
public static volatile int timestamp = 0;
public static volatile int highestTimestamp = 0;
public static int csCounter = 0;
public static int nodeId;
public static int n;
public static String[] hosts;
public static int[] ports;
public static volatile int[] deferredNodes;
public static volatile boolean requestingCS = false;
public static volatile int outstandingReplies;
// skipping getters and setters for variables shared between server and client threads
// method to connect the node with other nodes
public static Connection connect() {
// skipping standar code to create sockets
}
public static void main(String[] args) {
// start the receiver
Server server = new Server();
server.start();
// make the connections with other nodes
Connection connections = connect();
while (counter <= 2) {
// create a message
Message message = new Message(); // skipping details to create a msg
// send requests to all other nodes
for (int i = 0; i < n; i++) {
if (i != nodeId) {
System.out.println("Sending message " + message + " to node " + i);
try {
connections.outs[i].writeObject(message);
outstandingReplies++;
} catch (Exception e) {
e.printStackTrace();
}
}
}
// All REQUEST messages sent, WAIT for all replies
while (outstandingReplies != 0) {
// System.out.println("Waiting for replies");
}
// send REPLY msg based on some logic
if(logic is true){
// send messages to other nodes
Message replyMessage = new Message("REPLY", timestamp, nodeId);
try {
connections.outs[i].writeObject(replyMessage);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Connections Class
// Stores all the connections created by the Node Class
Server Class
// Listens to requests from nodesand spawns a Processor thread for each socket
public class Server extends Thread {
#Override
public void run() {
try {
System.out.println("Starting the node's receiver thread");
// create a serversocket to listen to requests
ServerSocket serverSocket = new ServerSocket(Node.ports[Node.nodeId]);
for (int i = 0; i < Node.n - 1; i++) {
System.out.println("Opening sockets");
Socket socket = serverSocket.accept();
// create a processor thread for each to read and process the incoming Messages
Processor processor = new Processor(socket);
System.out.println("Starting message processor");
processor.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Processor Class
// Processes the messages and sends reply when needed
public class Processor extends Thread {
Socket socket;
int[] replies = new int[10];
public Processor(Socket socket) {
this.socket = socket;
}
#Override
public void run() {
try {
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(
socket.getOutputStream());
while (true) {
System.out.println("Waiting for msg");
System.out.println("Socket is " + socket.isConnected());
Message message = (Message) in.readObject();
Node.timestamp = Math.max(Node.timestamp + 1, message.timestamp);
// printing the queue here
System.out.println("Processor received " + message);
// if message is a request
if (message.type.trim().equals("REQUEST")) {
if(condition is true){
// do something
} else {
// send reply to node who sent you message
Message replyMessage = new Message("REPLY", Node.timestamp,
Node.nodeId);
System.out.println(
"Send " + replyMessage + " to " + Node.hosts[message.nodeId]);
out.writeObject(replyMessage);
}
}
if (message.type.trim().equals("REPLY")) {
// do something
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
I run this code on two machines. The connections are established successfully and some messages are exchanged. However, in some cases the Processor class does not get message even when the socket is still connected.
I did go through some questions on missing messages with Java sockets which mentioned that when nodes gets two messages at once, it processes only the first message and the second message is buffered. I am not sure how I can check if that is the case with my code.
I am guessing something is wrong with I am handling sockets. Would really help if anyone could point where I am doing things incorrectly.
TIA
I have been working with TCP server/client stuff for a while. I am actully good at UDP programming when it comes to connecting more than one user that is multiple clients. I tried to do the same on a TCP server that i made using Threads but whenever the Thread gets to this piece of code
String reader = (String)in.readObject();
an error is generated and the thread stops executing the code but the thread still runs the program keeping it alive.
Anyway here is the entire source code :
public class TestServer implements Runnable {
private Thread run, streams, connect, receive, send;
private ServerSocket socket;
private Socket conn;
private ObjectInputStream in;
private ObjectOutputStream out;
private boolean running, incomingMessage = false;
private int port;
public TestServer(int port) throws IOException {
this.port = port;
socket = new ServerSocket(port);
console("Server stated on : " + InetAddress.getLocalHost() + " : " + port);
run = new Thread(this, "Run");
run.start();
}
public void run() {
running = true;
connect();
receive();
}
private void connect() {
connect = new Thread("Connect") {
public void run() {
while(running) {
try {
conn = socket.accept();
} catch (IOException e) {
e.printStackTrace();
}
console("You are now connected" + conn.getInetAddress().toString() + " : " + conn.getPort());
try {
setupStreams();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}; connect.start();
}
private void setupStreams() throws IOException {
streams = new Thread("Streams") {
public void run() {
try {
console("Setting up Streams");
out = new ObjectOutputStream(conn.getOutputStream());
out.flush();
in = new ObjectInputStream(conn.getInputStream());
console("Streams are now setup");
incomingMessage = true;
receive.start();
} catch(IOException e) {
e.printStackTrace();
}
}
}; streams.start();
}
private void receive() {
receive = new Thread("Receive") {
public void run() {
while(incomingMessage) {
String message = "";
try {
message = (String) in.readObject();
//This is the only flaw the program
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
console("Client : " + message);
}
}
};
}
private void console(String message) {
System.out.println(message);
}
public static void main(String[] args) {
try {
new TestServer(1234);
} catch (IOException e) {
e.printStackTrace();
}
}
}
FYI am not new to this. The error is caused because the server starts receiving packets even when there are no packets to be received. But because the thread forces it to receive it, i generates the error in the thread and dont know any other way to counter this. So please help. Thanks in Advance.
You shouldn't need 2 threads per connection. One thread is all that's required. After the connection is accepted, pass it to a worker thread to start reading. This can be done in a while loop in the worker thread.
Even though the socket's input stream can be read, the ObjectInputStream() class is more sensitive. If there is any error, its state is corrupted and it can't be used.
while (true) {
try {
Object input = in.readObject();
message = (String) input;
} catch (IOException e) {
e.printStackTrace();
break; //unrecoverable
} catch (ClassNotFoundException e) {
e.printStackTrace();
break; //unrecoverable
}
console("Client : " + message);
}
It's a better design to use a specific message protocol instead of sending serialized Java objects. For example if you are sending Strings like your sample, an InputStreamReader can be used to convert bytes to characters more easily and with less error handling.
These resources would be helpful to you:
https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html#later
Java - Listening to a socket with ObjectInputStream
ObjectInputStream(socket.getInputStream()); does not work
i'm working a client-server based game. The game is mainly caracterized by a Grid object which must be synchronized between clients. To do that I use the Object I/O Streams over Sockets.
However i encounter an issue during the synchronization process. The Grid is sent and recieved by all clients but its state is not modified after the first upload to each client.
I mean by that that clients do recieve the object in its present state when they connect but subsequent receiptions (initiated by either another client connection or previous client moves) don't present any modification over the initial sent state...
here are (stripped-down) snippets of the server side code:
while(true) //continuously accept new connections
{
//wait incoming connection, and accept it
Socket newSocket = serverListener.accept();
//create player details and save it in hashtable
Player newPlayer = new Player(newSocket); //streams saved here
Players.put(newPlayer);
//update all clients
sendGridToAll();
}
The Player class constructor:
public Player(Socket s) throws IOException
{
this.Tx = new ObjectOutputStream(socket.getOutputStream());
this.Rx = new DataInputStream(socket.getInputStream());
}
The SendToAll method:
public void sendGridToAll()
{
synchronized(Players) //do nothing while players HT is being modified
{
for(Enumeration e = Players.elements(); e.hasMoreElements(); )
{
Player tmpPlayer = (Player)e.nextElement();
ObjectOutputStream tmpTx = tmpPlayer.getTx();
try {
tmpTx.writeObject(grid);
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
print.log("Grid update sent");
}
}
and here is the client side snippet handling the object reception (ran in a thread):
public void run()
{
ObjectInputStream RX = new ObjectInputStream(socket.getInputStream());
while(true)
{
try
{
RX_grid = (Grid)RX.readObject();
}
catch (IOException ex)
{
print.log("IO Error");
ex.printStackTrace();
}
catch (ClassNotFoundException ex)
{
print.log("Bad grid class UID");
}
finally
{
print.log("grid recieved");
c.updateGui(RX_grid);
}
}
}
Thank you for your help
In my program I want to send some information to another computer with sockets. The first socket send some text throw the server and when the second socket receive this information it send a answer to the first socket. The problem is that another thread receive the answer and the only thing which I get is deadlock.
This is the server side:
else if(msgArray[0].compareTo("game_request") == 0){
if(userMap.containsKey(msgArray[1])){
Socket socketPlayerTwo = userMap.get(msgArray[1]);
otherPlayer = msgArray[1];
sendResult(socketPlayerTwo, "game_offer?"+username);
Boolean willPlay =Boolean.valueOf(recieveRequest(socketPlayerTwo).split("?")[1]);
if(willPlay)
playingUser.put(username,msgArray[1]);
sendResult(socket, "game_accept?"+willPlay);
}
}
This is the client side:
private void showGameRequest(String usernameOther) {
try{
int status = GameWindow.getInstance().showNotification("Game Offer","The user " + usernameOther + " offers you a game!\nDo you agree?",SWT.ICON_QUESTION|SWT.YES|SWT.NO);
if (status == SWT.YES){
otherPlayer = usernameOther;
sendRequest("send_message_to_user?user="+usernameOther+"&value=true");
GameWindow.getInstance().getDisplay().asyncExec(new Runnable() {
#Override
public void run() {
GameWindow.getInstance().startNewGame();
}
});
}
else
sendRequest("send_message_to_user?user="+usernameOther+"&value=false");
}
catch (IOException exc){
}
}
Here is the sendRequest method:
private void sendResult(Socket socket,String request) throws IOException{
DataOutputStream writer = new DataOutputStream(socket.getOutputStream());
writer.writeUTF(request);
writer.flush();
}
The client socket is created in the class Server
while (true) {
try {
Socket socket = serverSocket.accept();
new GameThread(socket,databaseManager);
} catch (IOException e) {
e.printStackTrace();
}
}
and it is put in hashmap when the user pass the login level:
if(msgArray[0].compareTo("user_info") == 0){
Integer flagUser = -1;
String[] userInfo = {msgArray[1].substring(msgArray[1].indexOf("=") + 1, msgArray[1].indexOf("&")),msgArray[1].substring(msgArray[1].lastIndexOf("=")+ 1, msgArray[1].indexOf(";"))};
Boolean status = databaseManager.checkUser(userInfo[0], userInfo[1]);
if(status){
if(!userMap.containsKey(userInfo[0])){
userMap.put(userInfo[0], socket);
username = userInfo[0];
flagUser = 0;
}
else
flagUser = 1;
}
sendResult(socket, "user_info_status?"+flagUser.toString()+"");
}
I thing I know what is the reason of the deadlock but I can't solve it. When the first user send the information to the other user he wait for response. Of course the other user send a response but this response is handle from other thread. So the deadlock is from a read method which block the thread. How can I send information from one socket to another without deadlock?
public GameThread(Socket socket, DatabaseManager databaseManager) {
this.socket = socket;
this.databaseManager = databaseManager;
parser = new RequestParser();
authorizationControl = new AuthorizationControl(databaseManager);
communication = new SocketCommunication(socket);
start();
}
Could you show us more of your code?
sendResult(socketPlayerTwo, "game_offer?"+username);
recieveRequest(socketPlayerTwo);
sendRequest("send_message_to_user?user="+usernameOther+"&value=true");
for starters.
Judging by the little i'm seeing you have a problem with the order in which you're using them. As far as i can tell you have a inputSteram.readObject() method that has blocked somewhere waiting for a message from the other side.