How to check if client is connected to server in UDP Java - java

I am doing a UDP program in Java language. I wish to send a message from the server to the client. However, as I am using UDP protocol.
How do I ensure that the client is connected before the datagram packet is sent?
buf = stringMessage.getBytes();
serversocket.send(new DatagramPacket(buf, stringMessage.length(), ia, cport));
// how to ensure that client is connected before sending?

UDP protocol doesn't have state, so there is no "connection".
You either use TCP or have to make your server respond to confirm that message is received.

there is no such 'connected' state in UDP protocol, however, you can create your own function to kinda have a list of connected clients.
Below I will present you some code I created to retrieve UDP clients and maintain them in a connected list of clients,
When you create the server you can wait for incoming connections, ( UDP clients sending "connect" messages ), then when the server receive those request check on the client list if that client is already there, if not, it creates a new client, it assigns an ID and send a response to the client with the ID assigned and message connected something like:
"1001#connected", after client send the request is waiting for the response, when the response arrives then ID is retrieved and set to the ID property of the client, and execute socket.connect( ip, port ) in order to only allow request/response from/to server
/**#TODO consider add variable to specify number of clients
* this class contains main server connection with all clients
* connected to a game, this connection is using UDP and it is really
* simple, if you need to use other kind of connection you are free
* to create your own
* #author PavulZavala
*/
public class Server
implements Conectable
{
protected DatagramSocket serverSocket;
protected boolean isAccepting;
protected List<Client> clientList;
protected String ip;
protected int port;
private Thread connectThread;
/**
*
* #param port
* #throws IOException
*/
public Server( int port ) throws IOException
{
serverSocket = new DatagramSocket( port );
this.port = port;
this.isAccepting = true;
clientList = new ArrayList<>();
}//
/**
* Accept UDP connections and store in clientList
* ----------------------------------------------
* this method is used to receive packages from UDP clients,
* and store their IP and ADDRESS in the client list,
* - you can change isAccepting to false to no receive more
* client connections or simple, call stopIsAcception to finish
* the Tread.
* #TODO it can be changed to accept like server socket
*/
#Override
public void connect()
{
connectThread = new Thread( ()->
{
while( isAccepting )
{
try {
//datagram packet to receive incoming request from client
DatagramPacket request =
new DatagramPacket( new byte[ Config.UDP_BUFFER_SIZE ], Config.UDP_BUFFER_SIZE );
serverSocket.receive( request );
//get Port and Address from client
//and check if exists in clientList
Client c = clientList
.stream()
.filter( client ->
{
return client.getIp().equals( request.getAddress().getHostAddress() );
}).findFirst().orElse( null );
//if no exists add it and send response
if( null == c )
{
Client client = new Client();
client.setIp( request.getAddress().getHostAddress() );
client.setPort( request.getPort() );
client.setId( generateId() );
//adding new client to the list
clientList.add( client );
byte[] bufResp = (client.getId() + "#connected").getBytes( "UTF-8" );
DatagramPacket resp =
new DatagramPacket(bufResp, bufResp.length,
InetAddress.getByName( client.getIp() ),
client.getPort());
System.err.println( client.getId()+ " Connected, response Sent" );
serverSocket.send( resp );
}//
} //
catch (IOException ex)
{
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
});//.start();
connectThread.start();
}//
/**
* this stops thread to accepts client socket connections
* #throws java.lang.InterruptedException
*/
public void stopAccepting() throws InterruptedException
{
connectThread.join();
}
/**
* this closes the DatagramSocket that is acting
* as server
*/
public void closeServer()
{
serverSocket.close();
}
/**
* used to receive UDP packets from clients
* this method creates its own Thread so it can
* receive packages without blocking the game
* #param r
*/
public void receive( Requestable r)
{
new Thread(()->
{
while( true )
{
r.receiveData();
}
}).start();
}//
/**
* used to generate id for connected clients
* #return
*/
private int generateId()
{
return ++Config.SOCKET_ID_COUNTER;
}
/**
* used to send UDP packets to clients
* #param r
*/
public void send( Responsable r )
{
r.sendData();
}
public DatagramSocket getServerSocket() {
return serverSocket;
}
public void setServerSocket(DatagramSocket serverSocket) {
this.serverSocket = serverSocket;
}
public boolean isIsAccepting() {
return isAccepting;
}
public void setIsAccepting(boolean isAccepting) {
this.isAccepting = isAccepting;
}
public List<Client> getClientList() {
return clientList;
}
public void setClientList(List<Client> clientList) {
this.clientList = clientList;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}//
Client class:
how do you now client was connected with the server, simple id property must be different than cero, in my implementation all ID are numeric starting on 1001, and that ID is only created by server, the only gap i have right now is how the server now if the client is still active, i am thinking to create another Thread where i can send messages periodically from client to server in order to ensure we are still communication, if the server does not receive for example a request from a client in 5 minutes, servers disconnects the client or it can ignore send broadcast messages to the client until it receives a new message ( i am currently working on this )
public class Client
implements Conectable
{
protected DatagramSocket socket;
protected String ip;
protected int port;
protected int id;
private Thread connectThread;
/**
* constructor without arguments to use with getters and setters
* #throws java.net.SocketException
*/
public Client() throws SocketException
{
this.socket = new DatagramSocket();
id = 0;
//id set after increasement
//id = ++Config.SOCKET_ID_COUNTER;
}
/**
* this constructor creates a client indicating the ip and port
* where the server
* #param ip
* #param port
* #throws SocketException
*/
public Client( String ip, int port ) throws SocketException
{
this();
this.setIp( ip );
this.setPort(port);
}
public DatagramSocket getSocket() {
return socket;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
/**
* this method send a request to the server to connect
*/
#Override
public void connect()
{
try
{
//send connect request to server
send( "connect" );
}
catch (IOException ex)
{
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
connectThread =
new Thread( ()->
{
while( id == 0 )
{
DatagramPacket response =
new DatagramPacket(new byte[ Config.UDP_BUFFER_SIZE], Config.UDP_BUFFER_SIZE );
try
{
socket.receive( response );
String resp = new String( response.getData(), "UTF-8" );
resp = resp.trim();
System.err.println("getting DATA: "+resp);
if( resp.trim().contains( "connected" ) )
{
id = Integer.parseInt( resp.trim().split( "#" )[0] ) ;
socket.connect( InetAddress.getByName( ip ), port );
stopConnecting();
}
}
catch ( IOException | InterruptedException ex)
{
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
connectThread.start();
}
/**
* method used to receive responses from server,
* every time this method is called a new Thread is created
* be careful not to call many times this
* #param r
*/
public void receive( Requestable r )
{
new Thread(()->
{
while( true )
{
r.receiveData();
}
}).start();
}
/**
* this method is used to send requests to server
* #param r
*/
public void send( Responsable r )
{
r.sendData();
}
/**
* this method will send a request to the server to
* the specific IP and port set by this class
* #param data request data
* #throws UnknownHostException
*/
public void send( String data ) throws UnknownHostException, IOException
{
byte[] dataBuf = data.getBytes();
DatagramPacket request =
new DatagramPacket(dataBuf,
dataBuf.length, InetAddress.getByName( ip ), port );
socket.send( request );
}
/**
* this method kills Thread used that is created
* when we attempt to connect to the server
* #throws InterruptedException
*/
public void stopConnecting() throws InterruptedException
{
connectThread.join();
}
}//
Server implementation, this can be done in the main of the app that will be the client
try
{
System.err.println("starting server");
Server s = new Server( 24586 );
//accept incoming conenctions
s.connect();
}
catch (IOException ex)
{
Logger.getLogger(DemoLevel.class.getName()).log(Level.SEVERE, "::: error con algo", ex);
}
Client implementation:
try
{
client = new Client( "127.0.0.1" , 24586 );
System.err.println("waiting to connect to the server");
client.connect();
}
catch ( SocketException ex )
{
Logger.getLogger(DemoLevel.class.getName()).log(Level.SEVERE, "::: error with server connection", ex);
}
i hope this can be usefull for you.
Console Messages from server:
> Task :run
starting server
1001 Connected, response Sent
Console Messages from client:
> Task :run
waiting to connect to the server
getting DATA: 1001#connected

Related

Creating a Server class and Client class for a custom chatroom in Java

Explanation of my Program
I have created a chat room server using Java that consists of 3 classes, a server class, a user class and a chat room class. I have also created a client class to interact with the Server. The server creates a user object every time a client connects to the server. Each user object keeps track of the chatrooms the user is in and runs a thread that listens for user input. A server object loops through each chatroom to see if it has been used in the last week.
The Problem
I'm wondering how to actually make a connection to my server with a client object. Currently I open two instances of eclipse. I run my server program in one, and my client program in the other, but I receive nothing in the console of either, which should happen because the server should send information to the client, which the client would then display on the console. The person on the client end could then give input that the client would take and send to the server.
I'm wondering why nothing is happening right now, and what improvements I can make.
Main Files
Server.java
/*
* Creates a server that can host up to 10 clients who can join chat rooms, post messages in chatrooms, and view posts made by other clients within the same chat room
*/
public class Server implements Runnable{
protected ArrayList<User> userList; //A list of users, where each user is a client connected to the server
protected LinkedList<Chatroom> chatRooms; //A list of chatrooms where each client can post messages in, where messages can be seen by all clients in the chatroom
private ServerSocket serverSocket; //The socket for the server
/*
* Constructor for the server class. Initializes the server attributes,
*/
public Server() {
this.userList = new ArrayList<User>(10);
this.chatRooms = new LinkedList<Chatroom>();
try {
this.serverSocket = new ServerSocket(5000);
}catch (IOException e) {
e.printStackTrace();
}
}
/*
* Creates a new user when a client connects to the server, and starts a user thread
*/
public void createUser() {
try {
Socket userSocket = serverSocket.accept();
Thread user = new Thread(new User(userSocket, this));
user.start();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Creates a chatroom for clients to interact in
* #param roomName: The name of the chat room to be created
*/
protected Chatroom createChatRoom(String roomName) {
Chatroom room = new Chatroom(roomName);
return room;
}
/*
* Receives messages from clients and performs actions based on the requests of the client
* (non-Javadoc)
* #see java.lang.Thread#run()
*/
public void run() {
long currentTime;
while(true) {
try {
currentTime = System.currentTimeMillis() / 1000;
//Loop through each chatroom and check if the chatroom has been used(joined or had a message sent to it) and remove that chatroom if it hasn't been used in a week
for (int i = 0; i < chatRooms.size(); i++) {
if (currentTime - 604800 >= chatRooms.get(i).dateLastUsed) {
chatRooms.remove(i);
//Also remove the chatroom from clients lists of chatrooms
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
Server server = new Server ();
server.run();
}
}
Client.java
public class Client extends Thread{
private String ip = "127.0.0.1";
private int port = 5000 ;
private Socket socket;
private DataInputStream iStream;
private DataOutputStream oStream;
private String input;
public Client() {
try {
this.socket = new Socket(ip, port);
this.iStream = new DataInputStream(socket.getInputStream());
this.oStream = new DataOutputStream(socket.getOutputStream());
}catch (Exception e) {
e.printStackTrace();
}
}
/*
* Sends a message to the user
* #param message: The message to be sent to the user
*/
protected void send (String message) {
try {
oStream.writeUTF(message);
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Closes the connection to the client
*/
protected void close () {
try {
iStream.close();
oStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Runs a thread for the client to constantly receive the clients input(non-Javadoc)
* #see java.lang.Thread#run()
*/
public void run() {
try {
Scanner reader = new Scanner(System.in);
input = iStream.readUTF();
String userInput;//Check if there is input from the user
while (input != null) {
input = iStream.readUTF();
System.out.println(input);
userInput = reader.next();
oStream.writeUTF(userInput);
}
reader.close();
}catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Client client = new Client();
client.run();
}
}
Objects used by Server.java
User.java
//Each user represents a client that has connected to the server
public class User implements Runnable{
private DataInputStream inputStream;
private DataOutputStream outputStream;
private Socket socket;
private String name;
protected LinkedList<Chatroom> chatRooms;
private String input;
private Server server;
/*
* User Constructor, create a user for each client connecting to the server
* #socket The socket that the user will be communicated through
* The client is prompted to create a name for themself, they are they prompted to do an action.
*/
public User(Socket socket, Server server) {
this.socket = socket;
this.server = server;
try {
inputStream = new DataInputStream(socket.getInputStream());
outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF("Enter a name");
this.name = inputStream.readUTF();
String message = "Create a chatroom: create \nList Chat Rooms: list \n Join Chat Room: join \n Leave Chat Room: Leave";
send(message);
} catch (IOException e) {
}
}
/*
* Returns the current amount of chatrooms this user is in
*/
protected int chatRoomLength () {
return this.chatRooms.size();
}
/*
* Gets the most recent input from the user
*/
protected String getInput() {
return input;
}
/*
* Puts a user/client in a chatroom
* #param cRoom: The chatroom that the user will join
*/
protected void joinRoom (Chatroom cRoom) {
chatRooms.add(cRoom);
}
/*
* Removes a user/client from a chatroom
*/
protected void leaveRoom (Chatroom c) {
chatRooms.removeFirstOccurrence(c);
}
/*
* Sends a message to the user
* #param message: The message to be sent to the user
*/
protected void send (String message) {
try {
outputStream.writeUTF(message);
} catch (IOException e) {
}
}
/*
* Closes the connection to the client
*/
protected void close () {
try {
inputStream.close();
outputStream.close();
socket.close();
} catch (IOException e) {}
}
/*
* Runs a thread for the client to constantly receive the clients input(non-Javadoc)
* #see java.lang.Thread#run()
*/
public void run() {
try {
input = inputStream.readUTF(); //Check if there is input from the user
//if the user has disconnected from the server, remove them from the list
if (input == null) {
this.close();
this.server.userList.remove(this);
}else if (input.equals("create")){ //create a chat room
this.send("Name the Chatroom");
input = this.getInput();
Chatroom c = this.server.createChatRoom(input);
this.joinRoom(c);
}else if (input.equals("list")) { //List the current chatrooms
String rooms = "";
for (int j = 0; j< server.chatRooms.size(); j++) {
rooms = rooms + server.chatRooms.get(j).getName() + "\n";
}
this.send(rooms);
}else if (input.equals("join")) { //Join the user to a chat room
int end = chatRooms.size();
if (end == 0) {
this.send("There's currently no chat rooms");
}else {
this.send("Which room would you like to join");
input = this.getInput();
for (int k = 0; k < end; k++) {
if (chatRooms.get(k).getName().equals(input)) {
Chatroom joinRoom = chatRooms.get(k);
this.joinRoom(joinRoom);
String message = "Chatroom " + input + " messages. \n";
//Print the current messages in the chatroom to the user
for (int j = 0; j < joinRoom.messages.size(); j++ ) {
message = message + joinRoom.messages.get(j) + "\n";
}
this.send(message);
} else if (k == end - 1) {
this.send("There's no chat rooms by that name");
}
}
}
}else if (input.equals("leave")) { //Remove the user from a chatroom
int end = this.chatRoomLength(); //if the chatroom list of the user is empty
if (end == 0) {
this.send("You are not in any Chat Rooms");
}else {
this.send("Which room would you like to leave");
input = this.getInput();
for (int m = 0; m < end; m++) { //find the chatroom by the same name
if (this.chatRooms.get(m).getName().equals(input)) {
this.chatRooms.remove(m);
this.send("Great! You've been removed from" + input);
} else if (m == end - 1) {
this.send("You're not in a chatroom named" + input);
}
}
}
}else { //All other input is interpreted as a message to be posted in the chatrooms that the user is in
int end = this.chatRoomLength();
if (end == 0) {
this.send("You can't write to any chat rooms because you are not in any");
}
for (int m = 0; m < end; m++) { //Add the users message to ALL the chatrooms the user is in
Chatroom c = this.chatRooms.get(m);
c.addMessage(input);
//Send this added message to all the users in this chatroom
for (int n = 0; n < c.users.size(); n++) {
User u = c.users.get(n);
u.send("Chatroom" + c.getName() + ":" + input);
}
}
}
}catch (IOException e) {
}
}
}
Chatroom.java
public class Chatroom {
private String name; //Name of the chatroom
protected LinkedList<String> messages; //List of text messages that have been sent by users to the chatroom and are displayed in the chatroom
protected long dateLastUsed; //The last time the chatroom was joined or had a message sent to it
protected LinkedList<User> users; //The clients/users that are currently in the chatroom
/*
* Chatroom constructor
* #param name The name of the chatroom, as determined by the user creating it
*/
public Chatroom(String name) {
dateLastUsed = System.currentTimeMillis() / 1000; //Sent the time that the chatroom was used last to the current UNIX Epoch time
messages = new LinkedList<String>();
this.name = name;
}
/*
* Adds a message into the chatroom
* #param message The message to be added to the chatroom
*/
protected void addMessage(String message) {
messages.add(message);
dateLastUsed = System.currentTimeMillis() / 1000;
}
/*
* Returns the name of the chatroom
* #return String equal to the name of the chatroom
*/
protected String getName() {
return this.name;
}
}
First of all, there is no call to createUser() which has the code to accept the socket connection.
Next is this code in run() function,
try {
Scanner reader = new Scanner(System.in);
input = iStream.readUTF();
String userInput;//Check if there is input from the user
while (input != null) {
input = iStream.readUTF();
System.out.println(input);
userInput = reader.next();
oStream.writeUTF(userInput);
}
Once the socket is accepted the server prints Enter a name which is stored input,
inside the while loop there is another readUTF() call.
The problem with readUTF() call is that it's blocking, ie. if there isn't a writeUTF() call from the server it waits for data.
I solved the problem by the following snippet,
try {
Scanner reader = new Scanner(System.in);
String userInput;//Check if there is input from the user
do {
input = iStream.readUTF();
System.out.println(input);
userInput = reader.next();
oStream.writeUTF(userInput);
} while (input != null);
reader.close();
}
Even this isn't the optimum solution as it would need Server to write something into the stream every time.
Another solution would be to read and write using different threads that way we would be able to create a full-duplex like chat, unlike this half duplex.
Then there were some NPE due to no initialization of chatRooms LinkedList,
Plus some logical errors like, not adding chatRooms to its List.
Wrong Code:
protected Chatroom createChatRoom(String roomName) {
Chatroom room = new Chatroom(roomName);
return room;
}
Corrected Code:
protected Chatroom createChatRoom(String roomName) {
Chatroom room = new Chatroom(roomName);
this.chatRooms.add(room);
return room;
}
The best way to solve all the bugs would be Github and a few contributors :p

Setting up a Client socket and ServerSocket listener (Java)

I'm trying to set up a peer to peer connection in java.
I'm trying to set up my program to listen for an incoming connection while outwardly being able to connect to a different client.
How can I instantiate my socket connection: socketConnection as whatever is connected to the program. Ideally like so:
if(socketConnection.isConnectedToExternalPeer()){
//do stuff
} else if (socketConnection.hasAnIncomingConnection()){
//do stuff
}
After consulting #L.Spillner 's solution I've put together the following code below, this only issue is that I can't quite grasp how to go about accepting a connection, this is evident from the fact that when I try to set up streams the program ends up in a loop while waiting for the peer's reply:
public class Client implements AutoCloseable {
// Any other ThreadPool can be used as well
private ExecutorService cachedExecutor = null;
private ExecutorService singleThreadExecutor = null;
// port this client shall listen on
private int port = 0;
// Name of the client
private String name = null;
// indicates that a connection is ongoing
private boolean isConnected = false;
// the socket the Client is currently connected with
private Socket activeConenctionSocket = null;
// The ServerSocket which will be listening for any incoming connection
private ServerSocket listener = null;
// The socket which has been accepted by the ServerSocket
private Future<Socket> acceptedSocket;
private ObjectInputStream inputStream = null;
private ObjectOutputStream outputStream = null;
private BloomChain bloomChain = null;
/**
* #param port Port number by which this client shall be accessed.
* #param name The name of this Client.
*/
public Client( int port, String name )
{
this.port = port;
this.name = name;
this.bloomChain = new BloomChain();
this.cachedExecutor = Executors.newCachedThreadPool();
this.singleThreadExecutor = Executors.newSingleThreadExecutor();
this.listener = createListeningSocket();
startListening();
}
private ServerSocket createListeningSocket()
{
ServerSocket temp = null;
try
{
temp = new ServerSocket( this.port );
}
catch ( IOException e )
{
e.printStackTrace();
}
return temp;
}
private void startListening()
{
if ( !this.isConnected )
{
this.listener = createListeningSocket();
this.acceptedSocket = this.cachedExecutor.submit( new ServAccept( this.listener ) );
}
}
/**
* Attempts to connect to any other socket specified by the hostname and the targetport.
*
* #param host The hostname of the target to connect.
* #param targetport The port of the target.
*/
public void connect( String host, int targetport )
{
try
{ System.out.println(host);
System.out.println(targetport);
this.activeConenctionSocket = new Socket( InetAddress.getByName( host ), targetport );
setUpStreams(this.activeConenctionSocket);
this.isConnected = true;
System.out.println(InetAddress.getAllByName(host));
}
catch ( IOException e )
{
e.printStackTrace();
}
try
{
this.listener.close();
}
catch ( IOException e )
{
// this will almost certainly throw an exception but it is intended.
}
}
public void setUpStreams(Socket socket) throws IOException {
this.outputStream = new ObjectOutputStream(socket.getOutputStream());
this.outputStream.flush();
this.inputStream = new ObjectInputStream(socket.getInputStream());
}
#Override
public void close() throws Exception
{
// close logic (can be rather nasty)
}
public void sendMessage(String message){
if(bloomChain.size()<1){
bloomChain.addBlock(new Block(message, "0"));
} else {
bloomChain.addBlock(new Block(message, bloomChain.get(bloomChain.size()-1).getPreviousHash()));
}
try {
this.outputStream.writeObject(bloomChain);
this.outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public String mineMessage(){
final String[] receivedMessage = {null};
final Block tempBlock = this.bloomChain.get(this.bloomChain.size()-1);
this.singleThreadExecutor.submit(()->{
tempBlock.mineBlock(bloomChain.getDifficulty());
receivedMessage[0] = tempBlock.getData();
});
return receivedMessage[0];
}
public String dataListener(){
if(isConnected) {
try {
BloomChain tempChain = (BloomChain) this.inputStream.readObject();
if (tempChain.isChainValid()) {
this.bloomChain = tempChain;
return mineMessage();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
public ServerSocket getListener() {
return this.listener;
}
public boolean isConnected(){
return isConnected;
}
public ObjectOutputStream getOutputStream(){
return this.outputStream;
}
public ObjectInputStream getInputStream(){
return this.inputStream;
}
}
EDIT 2:
I tried to await for acceptedSocket.get() to return a socket in a separate thread as follows:
new Thread(()->{
setupStreams(this.acceptedSocket.get());
//try-catch blocks omitted
}).start();
This successfully wait for acceptedSocket to return a connected socket however when I try to connect to another locally running client i get the following error: java.net.SocketException: socket closed
Okay after some tinkering I finally figured out a neat little solution:
We want to be able to listen and connect at the same time so we need a ServerSocket and issue an ServerSocket#accept call to accept incoming cnnections.
However this method is blocking the thread so in order to being able to proceed with our programm we have to outsource this call into another thread and luckly the default Java API does provide a simple way to do so.
The following codesample is not finished but provides the core functionality:
Client.java:
public class Client
implements AutoCloseable
{
// Any other ThreadPool can be used as well
private ExecutorService es = Executors.newCachedThreadPool();
// port this client shall listen on
private int port;
// Name of the client
private String name;
// indicates that a connection is ongoing
private boolean isConnected = false;
// the socket the Client is currently connected with
private Socket activeConenctionSocket;
// The ServerSocket which will be listening for any incoming connection
private ServerSocket listener;
// The socket which has been accepted by the ServerSocket
private Future<Socket> acceptedSocket;
/**
* #param port Port number by which this client shall be accessed.
* #param name The name of this Client.
*/
public Client( int port, String name )
{
this.port = port;
this.name = name;
this.listener = createListeningSocket();
startListening();
}
private ServerSocket createListeningSocket()
{
ServerSocket temp = null;
try
{
temp = new ServerSocket( port );
}
catch ( IOException e )
{
e.printStackTrace();
}
return temp;
}
private void startListening()
{
if ( !isConnected )
{
listener = createListeningSocket();
acceptedSocket = es.submit( new ServAccept( listener ) );
}
}
/**
* Attempts to connect to any other socket specified by the hostname and the targetport.
*
* #param host The hostname of the target to connect.
* #param targetport The port of the target.
*/
public void connect( String host, int targetport )
{
isConnected = true;
try
{
activeConenctionSocket = new Socket( InetAddress.getByName( host ), targetport );
}
catch ( IOException e )
{
e.printStackTrace();
}
try
{
listener.close();
}
catch ( IOException e )
{
// this will almost certainly throw an exception but it is intended.
}
}
#Override
public void close() throws Exception
{
// close logic (can be rather nasty)
}
}
Let's walk through there step by step on how we instantiate a new Client object:
When we instantiate our object we create a new ServerSocket
We start listenting by creating a new Thread of a Callable<V> Object which I've named ServAccept for example purposes.
Now we have a Future<T> object which will contain a socket if any connection gets accepted.
A positive side effect of the startListening() method is, that you can make it public and call it once more if the connection has dropped.
The conenct(...) method almost works the same way as your setupConnection() method but with a small twist. The ServerSocket, which is still listening in another thread, will be close. The reason for this is, that there is no other way to exit the accept() method the other thread is stuck in.
The last thing (which you have to figure out) is when to check if the Future object is already done.
ServAccept.java
public class ServAccept
implements Callable<Socket>
{
ServerSocket serv;
public ServAccept( ServerSocket sock )
{
this.serv = sock;
}
#Override
public Socket call() throws Exception
{
return serv.accept();
}
}
EDIT:
As a matter of fact I have to admit that my approach might not be a very well rounded approach for the task so I decided to change tweak some things. This time instead of using a Future Object I decided to go with Events / a custom EventListener which is just sitting there and listening for a connection to receive. I tested the connection functionality and it works just fine but I haven't implemented a solution to determine if a Client really conncted to a peer. I just made sure that a client can only hold one connection at a time.
The changes:
ServerAccept.java
import java.io.IOException;
import java.net.ServerSocket;
public class ServAccept implements Runnable
{
private ServerSocket serv;
private ConnectionReceivedListener listener;
public ServAccept( ServerSocket sock,ConnectionReceivedListener con )
{
this.serv = sock;
this.listener = con;
}
#Override
public void run()
{
try
{
listener.onConnectionReceived( new ConnectionReceivedEvent( serv.accept() ) );
} catch (IOException e)
{
// planned exception here.
}
}
}
Does no longer implement Callable<V> but Runnable the only reason for that change is that we do not longer await any return since we will work with a listener and some juicy events. Anyway in order to do so we need to create and pass a listener to this object. But first we should take a look at the listener / event structure:
ConnectionReceivedListener.java
import java.util.EventListener;
#FunctionalInterface
public interface ConnectionReceivedListener extends EventListener
{
public void onConnectionReceived(ConnectionReceivedEvent event);
}
Just a simple interface from what we build some anonymous classes or lambda expressions. Nothing to fancy. It doen't even need to extend the EventListener interface but I love to do that to remind me what the purpose of the class is.
ConnectionReceivedEvent.java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ConnectionReceivedEvent
{
private Socket accepted;
public ConnectionReceivedEvent( Socket sock )
{
this.accepted = sock;
}
public Socket getSocket()
{
return accepted;
}
public OutputStream getOutput() throws IOException
{
return accepted.getOutputStream();
}
public InputStream getInput() throws IOException
{
return accepted.getInputStream();
}
public int getPort()
{
return accepted.getPort();
}
}
Nothing to fancy as well, just passing a Socket as a constructor parameter and defining some getters from which most will not be used in this example.
But how to we use it now?
private void startListening()
{
if (!isConnected)
{
closeIfNotNull();
listener = createListeningSocket();
es.execute( new ServAccept( listener, event -> setAccepted( event.getSocket() ) ) );
}
}
private void setAccepted( Socket socket )
{
if (!isConnected)
{
this.activeConenctionSocket = socket;
setUpStreams( socket );
} else
{
sendError( socket );
}
}
We still make use of our ExecutorService and creating a new Thread with the ServAccept class. However since we do not expect any return I changed from ExecutorService#submit to ExecutorService#execute (just a matter of opinion and taste).
But ServAccept needs two arguments now. The ServerSocket and the Listener to use. Fortunately we can use annonymous classes and since our Listener does only feature one method we can even use a lambda expression. event -> setAccepted(event.getSocket()).
As an answer to your 2nd edit: I did a logical mistake. Not the ServerSocket#close method does throw the exception whe interrupting a ServerSocket#accept call but rather the accept() call itself throws the exception. In other words the exception you got was intended and i suppressed another one by mistake.

Getting response from Netty server in non-Netty client

This is my first question on StackOverflow and I hope I have adhered to the expected standards.
I have been taking over some code from someone else who isn't working here anymore and I'm pretty much stranded here. I searched and asked some colleagues (not too much Java experience unfortunately) but no-one seems to be able to help me. Searching didn't really help me either.
I'm sending Json requests to a Netty server from a client which intentionally is NOT implemented using Netty. For now it is just a simple Java socket, but the intention is to have a Flask client send requests to the Netty server. The requests arrive (both using Java Sockets and using Python Flask), and get properly processed in the pipeline, but I want to send a response to the client and although I suspect where in the code to send the response I'm clearly missing out on something as I don't get any response. Any suggestions?
The Java Socket client (note that the json1 and json2 strings have been omitted from the snippet here as they are rather long, but they are formatted properly). Posting requests using a Socket and the related output stream. The response part (with the input stream for the same socket) is just some test which I have my doubt about, but not sure how to do this otherwise (and that's why I kept it here). I've been seeing plenty of examples with clients implementing Netty interfaces and that seems to work fine, but as said I want a client not using Netty to be able to receive the responses as well (if that's possible at all).
String serverResponse;
for (int j = 0; j < 100; j++) {
for (int i = 0; i < 1000; i++) {
try {
Socket s = new Socket("localhost", 12000);
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
out.write(json1 + i + json2);
out.flush();
// Testing only - trying to get the response back from the server
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
while(true) {
if ((serverResponse = in.readLine()) != null) {
log.info("server says", serverResponse);
break;
}
}
out.close();
s.close();
Thread.sleep(1000);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Thread.sleep(2000);
}
MCTcpServer.java
/**
* Abstract TCP Server class. this class should be implemented in the subclass to implement an actual server.
*
* #param <R> The data to be read from the socket.
* #param <W> data to be written (in case of duplex) from the socket.
*/
public abstract class MFTcpServer<R, W> {
protected final AtomicBoolean started;
protected MFTcpServer() {
this.started = new AtomicBoolean();
}
/**
* Start the server.
*
* #param initializer the channel initializers. they will be called when a new client connects to the server.
* #return instance of tcp server
*/
public final MFTcpServer<R, W> start(ChannelInitializer<Channel> initializer) {
if (!started.compareAndSet(false, true)) {
throw new IllegalStateException("Server already started");
}
doStart(initializer);
return this;
}
/**
* Start the server and wait for all the threads to be finished before shutdown.
* #param initializer the channel initializers. they will be called when a new client connects to the server.
*/
public final void startAndAwait(ChannelInitializer<Channel> initializer) {
start(initializer);
awaitShutdown();
}
/**
* Shutdown the server
* #return true if successfully shutdown.
*/
public final boolean shutdown() {
return !started.compareAndSet(true, false) || doShutdown();
}
/**
* Wait for all the threads to be finished before shutdown.
*/
public abstract void awaitShutdown();
/**
* Do the shutdown now.
* #return true if successfully shutdown
*/
public abstract boolean doShutdown();
/**
* start the server
* #param initializer the channel initializers. they will be called when a new client connetcs to the server.
* #return instance of tcp server
*/
public abstract MFTcpServer<R, W> doStart(ChannelInitializer<Channel> initializer);
/**
*
* #return the port where the server is running.
*/
public abstract int getPort();
MFNetty4TcpServer.java Actual server implementation
public class MFNetty4TcpServer<R, W> extends MFTcpServer<R, W> {
private static final Logger logger = LoggerFactory.getLogger(MFNetty4TcpServer.class);
private static final int BOSS_THREAD_POOL_SIZE = 2;
private int port;
private ServerBootstrap bootstrap;
private ChannelFuture bindFuture;
/**
* The constructor.
*
* #param port port where to listen
*/
protected MFNetty4TcpServer(int port) {
this.port = port;
final NioEventLoopGroup bossGroup = new NioEventLoopGroup(0, new DefaultEventExecutorGroup
(BOSS_THREAD_POOL_SIZE));
final NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, new DefaultEventExecutorGroup
(JsonProducerConfig.THREAD_POOL_SIZE));
bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class);
}
#Override
public MFNetty4TcpServer<R, W> doStart(ChannelInitializer<Channel> initializer) {
bootstrap.childHandler(new ChannelInitializer<Channel>() {
#Override
protected void initChannel(Channel ch) throws Exception {
if (initializer != null) {
ch.pipeline().addLast(initializer);
}
}
});
try {
bindFuture = bootstrap.bind(port).sync();
if (!bindFuture.isSuccess()) {
// Connection not successful
throw new RuntimeException(bindFuture.cause());
}
SocketAddress localAddress = bindFuture.channel().localAddress();
if (localAddress instanceof InetSocketAddress) {
port = ((InetSocketAddress) localAddress).getPort();
logger.info("Started server at port: " + port);
}
} catch (InterruptedException e) {
logger.error("Error waiting for binding server port: " + port, e);
}
return this;
}
#Override
public void awaitShutdown() {
try {
bindFuture.channel().closeFuture().await();
} catch (InterruptedException e) {
Thread.interrupted(); // Reset the interrupted status
logger.error("Interrupted while waiting for the server socket to close.", e);
}
}
#Override
public boolean doShutdown() {
try {
bindFuture.channel().close().sync();
return true;
} catch (InterruptedException e) {
logger.error("Failed to shutdown the server.", e);
return false;
}
}
#Override
public int getPort() {
return port;
}
/**
* Creates a tcp server at the defined port.
*
* #param port port to listen to
* #param <R> data to be read
* #param <W> data to be written back. Only in case of duplex connection.
* #return instance of tcp server.
*/
public static <R, W> MFTcpServer<R, W> create(int port) {
return new MFNetty4TcpServer<>(port);
}
}
JsonProducerConfig.java The pipeline is setup here.
/**
* Spring Configuration class of the application.
*/
#Configuration
#Import({DatabusConfig.class})
public class JsonProducerConfig {
private static final Logger log = LoggerFactory.getLogger(JsonProducerConfig.class);
public static final int THREAD_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
public static final String TCP_SERVER = "tcpServer";
public static final String CHANNEL_PIPELINE_INITIALIZER = "channel_initializer";
public static final String MF_KAFKA_PRODUCER = "mf_kafka_producer";
public static final String JSON_AVRO_CONVERTOR = "jsonAvroConvertor";
#Value("#{systemProperties['tcpserver.port']?:'12000'}")
private String tcpServerPort;
#Bean(name = TCP_SERVER)
#Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public MFTcpServer nettyTCPServer() {
return MFNetty4TcpServer.create(Integer.parseInt(tcpServerPort));
}
#Bean(name = MF_KAFKA_PRODUCER)
#Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public MFKafkaProducer pushToKafka() {
return new MFKafkaProducer();
}
#Bean(name = JSON_AVRO_CONVERTOR)
#Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public JsonAvroConvertor jsonAvroConvertor() {
return new JsonAvroConvertor();
}
/**
* This is where the pipeline is set for processing of events.
*
* #param jsonAvroConvertor converts json to avro
* #param kafkaProducer pushes to kafka
* #return chanenl initializers pipeline.
*/
#Bean(name = CHANNEL_PIPELINE_INITIALIZER)
#Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public ChannelInitializer<Channel> channelInitializers(JsonAvroConvertor jsonAvroConvertor,
MFKafkaProducer kafkaProducer) {
return new ChannelInitializer<Channel>() {
#Override
protected void initChannel(Channel channel) throws Exception {
if (log.isInfoEnabled())
log.info("initChannel - initing channel...");
channel.pipeline().addLast(new NioEventLoopGroup(0, new DefaultEventExecutorGroup(THREAD_POOL_SIZE)));
channel.pipeline().addLast(new JsonObjectDecoder(1048576));
channel.pipeline().addLast(jsonAvroConvertor);
channel.pipeline().addLast(kafkaProducer);
if (log.isInfoEnabled())
log.info("channel = " + channel.toString());
}
};
}
}
JsonProducer.java The main program
public class JsonProducer {
private static final Logger log = LoggerFactory.getLogger(JsonProducer.class);
private static MFTcpServer tcpServer;
/**
* Main startup method
*
* #param args not used
*/
public static void main(String[] args) {
System.setProperty("solschema", "false");
try {
// the shutdown hook.
Runtime.getRuntime().addShutdownHook(new Thread(
() -> {
if (tcpServer != null) {
tcpServer.shutdown();
}
}
));
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(JsonProducerConfig.class);
tcpServer = (MFTcpServer) context.getBean(JsonProducerConfig.TCP_SERVER);
ChannelInitializer<Channel> channelInitializer = (ChannelInitializer<Channel>) context.
getBean(JsonProducerConfig.CHANNEL_PIPELINE_INITIALIZER);
tcpServer.startAndAwait(channelInitializer);
} catch (Exception t) {
log.error("Error while starting JsonProducer ", t);
System.exit(-1);
}
}
}
The MFKafkaProducer.java as the last channel in the pipeline. Note the ctx.writeAndFlush(msg) in the channelRead method which is where I understand the response should be initiated. But what after that. When running this channelFuture.isSuccess() evaluates to false. The response object was an attempt to a String response.
#ChannelHandler.Sharable
public class MFKafkaProducer extends ChannelInboundHandlerAdapter {
private static final Logger log = LoggerFactory.getLogger(MFKafkaProducer.class);
#Resource
ApplicationContext context;
#Resource(name = DatabusConfig.ADMIN)
Admin admin;
private Map<String, IProducer> streams = new HashMap<>();
#PreDestroy
public void stop() {
removeAllStreams(); // then stop writing to producers
}
/**
* #param clickRecord the record to be pushed to kafka
* #throws Exception
*/
public void handle(GenericRecord clickRecord) throws Exception {
Utf8 clientId = null;
try {
clientId = (Utf8) clickRecord.get(SchemaUtil.APP_ID);
stream(producer(clientId.toString()), clickRecord);
} catch (Exception e) {
String message = "Could not push click data for clientId:" + clientId;
log.warn("handle - " + message + "!!!", e);
assert clientId != null;
removeStream(clientId.toString());
}
}
/**
* removes all the streams
*/
private void removeAllStreams() {
Set<String> strings = streams.keySet();
for (String clientId : strings) {
removeStream(clientId);
}
}
/**
* removes a particular stream
*
* #param clientId the stream to be removed
*/
private void removeStream(String clientId) {
Assert.notEmpty(streams);
IProducer producer = streams.get(clientId);
producer.stopProducer();
streams.remove(clientId);
}
/**
* #param producer the producer where data needs to be written
* #param clickRecord teh record to be written
*/
private void stream(IProducer producer, GenericRecord clickRecord) {
producer.send(clickRecord);
}
/**
* This will create a producer in case it is not already created.
* If already created return the already present one
*
* #param clientId stream id
* #return the producer instance
*/
private IProducer producer(String clientId) {
if (streams.containsKey(clientId)) {
return streams.get(clientId);
} else {
IProducer producer = admin.createKeyTopicProducer(SchemaUtil.APP_ID, "test_" + clientId, new ICallback() {
#Override
public void onSuccess(long offset) {
if (log.isInfoEnabled())
log.info("onSuccess - Data at offset:" + offset + " send.");
}
#Override
public void onError(long offset, Exception ex) {
if (log.isInfoEnabled())
log.info("onError - Data at offset:" + offset + " failed. Exception: ", ex);
}
#Override
public void onStreamClosed() {
log.warn("onStreamClosed - Stream:" + clientId + " closed.");
removeStream(clientId);
}
});
producer.startProducer();
streams.put(clientId, producer);
return producer;
}
}
#Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
log.debug("KafkaProducer - channelRead() called with " + "ctx = [" + ctx + "], msg = [" + msg + "]");
if (msg instanceof GenericRecord) {
GenericRecord genericRecord = (GenericRecord) msg;
try {
handle(genericRecord);
log.debug("channelRead sending response");
Charset charset = Charset.defaultCharset();
ByteBuf response = Unpooled.copiedBuffer("Just a response", charset);
ChannelFuture future = ctx.writeAndFlush(msg);
future.addListener(new ChannelFutureListener() {
#Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if (channelFuture.isSuccess())
log.info("channelRead - future.operationComplete - Response has been delivered to all channels");
else
log.info("channelRead - future.operationComplete - Response has NOT been delivered to all channels");
}
});
} catch (Exception ex) {
log.error("Something went wrong processing the generic record: " + msg + "\n ", ex);
}
} else {
log.debug("KafkaProducer - msg not of Type Generic Record !!! " + msg);
}
}
#Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
log.error("Something went wrong writing to Kafka: \n", cause);
ctx.close();
}
}
Using ChannelFuture#cause() I noticed I was not serializing a ByteBuf object, but a GenericRecord instead. Using
ByteBuf response = Unpooled.copiedBuffer(genericRecord.toString(), charset);
ChannelFuture future = ctx.writeAndFlush(response);
the GenericRecord gets converted to a ButeBuf and sends a response using the writeAndFlush method.
The test client using a Socket implementation somehow never really received a response, but by using a SocketChannel this was resolved as well.

Gracefully ending a thread that's waiting on a blocking queue

I'm having an issue with a multi-threaded server I'm building as an academic exercise, more specifically with getting a connection to close down gracefully.
Each connection is managed by a Session class. This class maintains 2 threads for the connection, a DownstreamThread and an UpstreamThread.
The UpstreamThread blocks on the client socket and encodes all incoming strings into messages to be passed up to another layer to deal with. The DownstreamThread blocks on a BlockingQueue into which messages for the client are inserted. When there's a message on the queue, the Downstream thread takes the message off the queue, turns it into a string and sends it to the client. In the final system, an application layer will act on incoming messages and push outgoing messages down to the server to send to the appropriate client, but for now I just have a simple application that sleeps for a second on an incoming message before echoing it back as an outgoing message with a timestamp appended.
The problem I'm having is getting the whole thing to shut down gracefully when the client disconnects. The first issue I'm contending with is a normal disconnect, where the client lets the server know that it's ending the connection with a QUIT command. The basic pseudocode is:
while (!quitting) {
inputString = socket.readLine () // blocks
if (inputString != "QUIT") {
// forward the message upstream
server.acceptMessage (inputString);
} else {
// Do cleanup
quitting = true;
socket.close ();
}
}
The upstream thread's main loop looks at the input string. If it's QUIT the thread sets a flag to say that the client has ended communications and exits the loop. This leads to the upstream thread shutting down nicely.
The downstream thread's main loop waits for messages in the BlockingQueue for as long as the connection closing flag isn't set. When it is, the downstream thread is also supposed to terminate. However, it doesn't, it just sits there waiting. Its psuedocode looks like this:
while (!quitting) {
outputMessage = messageQueue.take (); // blocks
sendMessageToClient (outputMessage);
}
When I tested this, I noticed that when the client quit, the upstream thread shut down, but the downstream thread didn't.
After a bit of head scratching, I realised that the downstream thread is still blocking on the BlockingQueue waiting for an incoming message that will never come. The upstream thread doesn't forward the QUIT message any further up the chain.
How can I make the downstream thread shut down gracefully? The first idea that sprang to mind was setting a timeout on the take() call. I'm not too keen on this idea though, because whatever value you select, it's bound to be not entirely satisfactory. Either it's too long and a zombie thread sits there for a long time before shutting down, or it's too short and connections that have idled for a few minutes but are still valid will be killed. I did think of sending the QUIT message up the chain, but that requires it to make a full round trip to the server, then the application, then back down to the server again and finally to the session. This doesn't seem like an elegant solution either.
I did look at the documentation for Thread.stop() but that's apparently deprecated because it never worked properly anyway, so that looks like it's not really an option either. Another idea I had was to force an exception to be triggered in the downstream thread somehow and let it clean up in its finally block, but this strikes me as a horrible and kludgey idea.
I feel that both threads should be able to gracefully shutdown on their own, but I also suspect that if one thread ends it must also signal the other thread to end in a more proactive way than simply setting a flag for the other thread to check. As I'm still not very experienced with Java, I'm rather out of ideas at this point. If anyone has any advice, it would be greatly appreciated.
For the sake of completeness, I've included the real code for the Session class below, though I believe the pseudocode snippets above cover the relevant parts of the problem. The full class is about 250 lines.
import java.io.*;
import java.net.*;
import java.util.concurrent.*;
import java.util.logging.*;
/**
* Session class
*
* A session manages the individual connection between a client and the server.
* It accepts input from the client and sends output to the client over the
* provided socket.
*
*/
public class Session {
private Socket clientSocket = null;
private Server server = null;
private Integer sessionId = 0;
private DownstreamThread downstream = null;
private UpstreamThread upstream = null;
private boolean sessionEnding = false;
/**
* This thread handles waiting for messages from the server and sending
* them to the client
*/
private class DownstreamThread implements Runnable {
private BlockingQueue<DownstreamMessage> incomingMessages = null;
private OutputStreamWriter streamWriter = null;
private Session outer = null;
#Override
public void run () {
DownstreamMessage message;
Thread.currentThread ().setName ("DownstreamThread_" + outer.getId ());
try {
// Send connect message
this.sendMessageToClient ("Hello, you are client " + outer.getId ());
while (!outer.sessionEnding) {
message = this.incomingMessages.take ();
this.sendMessageToClient (message.getPayload ());
}
// Send disconnect message
this.sendMessageToClient ("Goodbye, client " + getId ());
} catch (InterruptedException | IOException ex) {
Logger.getLogger (DownstreamThread.class.getName ()).log (Level.SEVERE, ex.getMessage (), ex);
} finally {
this.terminate ();
}
}
/**
* Add a message to the downstream queue
*
* #param message
* #return
* #throws InterruptedException
*/
public DownstreamThread acceptMessage (DownstreamMessage message) throws InterruptedException {
if (!outer.sessionEnding) {
this.incomingMessages.put (message);
}
return this;
}
/**
* Send the given message to the client
*
* #param message
* #throws IOException
*/
private DownstreamThread sendMessageToClient (CharSequence message) throws IOException {
OutputStreamWriter osw;
// Output to client
if (null != (osw = this.getStreamWriter ())) {
osw.write ((String) message);
osw.write ("\r\n");
osw.flush ();
}
return this;
}
/**
* Perform session cleanup
*
* #return
*/
private DownstreamThread terminate () {
try {
this.streamWriter.close ();
} catch (IOException ex) {
Logger.getLogger (DownstreamThread.class.getName ()).log (Level.SEVERE, ex.getMessage (), ex);
}
this.streamWriter = null;
return this;
}
/**
* Get an output stream writer, initialize it if it's not active
*
* #return A configured OutputStreamWriter object
* #throws IOException
*/
private OutputStreamWriter getStreamWriter () throws IOException {
if ((null == this.streamWriter)
&& (!outer.sessionEnding)) {
BufferedOutputStream os = new BufferedOutputStream (outer.clientSocket.getOutputStream ());
this.streamWriter = new OutputStreamWriter (os, "UTF8");
}
return this.streamWriter;
}
/**
*
* #param outer
*/
public DownstreamThread (Session outer) {
this.outer = outer;
this.incomingMessages = new LinkedBlockingQueue ();
System.out.println ("Class " + this.getClass () + " created");
}
}
/**
* This thread handles waiting for client input and sending it upstream
*/
private class UpstreamThread implements Runnable {
private Session outer = null;
#Override
public void run () {
StringBuffer inputBuffer = new StringBuffer ();
BufferedReader inReader;
Thread.currentThread ().setName ("UpstreamThread_" + outer.getId ());
try {
inReader = new BufferedReader (new InputStreamReader (outer.clientSocket.getInputStream (), "UTF8"));
while (!outer.sessionEnding) {
// Read whatever was in the input buffer
inputBuffer.delete (0, inputBuffer.length ());
inputBuffer.append (inReader.readLine ());
System.out.println ("Input message was: " + inputBuffer);
if (!inputBuffer.toString ().equals ("QUIT")) {
// Forward the message up the chain to the Server
outer.server.acceptMessage (new UpstreamMessage (sessionId, inputBuffer.toString ()));
} else {
// End the session
outer.sessionEnding = true;
}
}
} catch (IOException | InterruptedException e) {
Logger.getLogger (Session.class.getName ()).log (Level.SEVERE, e.getMessage (), e);
} finally {
outer.terminate ();
outer.server.deleteSession (outer.getId ());
}
}
/**
* Class constructor
*
* The Core Java volume 1 book said that a constructor such as this
* should be implicitly created, but that doesn't seem to be the case!
*
* #param outer
*/
public UpstreamThread (Session outer) {
this.outer = outer;
System.out.println ("Class " + this.getClass () + " created");
}
}
/**
* Start the session threads
*/
public void run () //throws InterruptedException
{
Thread upThread = new Thread (this.upstream);
Thread downThread = new Thread (this.downstream);
upThread.start ();
downThread.start ();
}
/**
* Accept a message to send to the client
*
* #param message
* #return
* #throws InterruptedException
*/
public Session acceptMessage (DownstreamMessage message) throws InterruptedException {
this.downstream.acceptMessage (message);
return this;
}
/**
* Accept a message to send to the client
*
* #param message
* #return
* #throws InterruptedException
*/
public Session acceptMessage (String message) throws InterruptedException {
return this.acceptMessage (new DownstreamMessage (this.getId (), message));
}
/**
* Terminate the client connection
*/
private void terminate () {
try {
this.clientSocket.close ();
} catch (IOException e) {
Logger.getLogger (Session.class.getName ()).log (Level.SEVERE, e.getMessage (), e);
}
}
/**
* Get this Session's ID
*
* #return The ID of this session
*/
public Integer getId () {
return this.sessionId;
}
/**
* Session constructor
*
* #param owner The Server object that owns this session
* #param sessionId The unique ID this session will be given
* #throws IOException
*/
public Session (Server owner, Socket clientSocket, Integer sessionId) throws IOException {
this.server = owner;
this.clientSocket = clientSocket;
this.sessionId = sessionId;
this.upstream = new UpstreamThread (this);
this.downstream = new DownstreamThread (this);
System.out.println ("Class " + this.getClass () + " created");
System.out.println ("Session ID is " + this.sessionId);
}
}
Instead of calling Thread.stop use Thread.interrupt. That will cause the take method to throw an InterruptedException which you can use to know that you should shut down.
Can you just create "fake" quit message instead of setting outer.sessionEnding to true when "QUIT" appears. Putting this fake message in queue will wake the DownstreamThread and you can end it. In that case you can even eliminate this sessionEnding variable.
In pseudo code this could look like this:
while (true) {
outputMessage = messageQueue.take (); // blocks
if (QUIT == outputMessage)
break
sendMessageToClient (outputMessage);
}

zeromq socket recv returned null (java warper)

i have network client / server application that using java zeromq
framework for the communications .
i have the main server and N clients that polls the server . when the server gets online.
the clients connect him and there some short massaging going on between them
until now i done with single client and it worked fine .
but when adding another client ( that's 2 )
i getting in the request null as returned massage :
request = socket.recv (0);
based on the this example :
http://zguide.zeromq.org/java:mtserver
my code (part of it its very long )
all context and the ZeroMq Settings are set and not null
and i allways get this exception :
Exception in thread "Thread-1" org.zeromq.ZMQException: Operation cannot be accomplished in current state(0x9523dfb)
at org.zeromq.ZMQ$Socket.recv(Native Method)
at com.controller.core.Daemon$1.run(Daemon.java:127)
for(int thread_nbr = 0; thread_nbr < m_iThreadPoolCount; thread_nbr++) {
Thread worker_routine = new Thread() {
#Override
public void run() {
//synchronized(OBJ_LOCK) {
ZMQ.Socket socket = m_pNetworkManager.getContext().socket(ZMQ.REP);//context.socket(ZMQ.REP);
socket.connect ("inproc://workers");
while (true) {
/** Wait for next request from client (C string) */
byte[] request = null;
try{
if(m_pNetworkManager.getContext()!=null) // its never null
{
request = socket.recv (0);
}
}catch (Exception e)
{
// it allays gets null exception
}
boolean bFoundInList = false;
if(request!=null)
{
// multi frame sending
socket.send(m_UT.getbyteArray(
m_UT.getReplayStructure(aStateMap_replay)
),ZMQ.SNDMORE);
socket.send(new byte[0], ZMQ.SNDMORE);
byte[] byteFileStruct = null;
byteFileStruct = m_UT.serialize(stateFilesStruct);
boolean send = socket.send(byteFileStruct,0);
} // socket.recv end
}
// }// synchronized block
}
}; //Thread worker_routine
worker_routine.start();
}
// Connect work threads to client threads via a queue
ZMQQueue zMQQueue = new ZMQQueue( m_pNetworkManager.getContext(),
m_pNetworkManager.getClients(),
m_pNetworkManager.getWorkers());
zMQQueue.run();
// We never get here but clean up anyhow
m_pNetworkManager.getClients().close();
m_pNetworkManager.getWorkers().close();
m_pNetworkManager.getContext().term();
}
allso added the NetworkManager class
public class NetworkManager {
/** ZeroMQ context */
private ZMQ.Context m_context = null;
/** ZeroMQ socket */
private ZMQ.Socket m_socket = null;
/** representation of the clients */
ZMQ.Socket m_clients = null;
/** representation of the workers threads */
ZMQ.Socket m_workers = null;
/**
* NetworkManager constructor.
*/
public NetworkManager()
{
;
}
/**
* Setup the network ZeroMQ network layer
* #param sControllerDomain the Controller domain name and port
*/
public void Init(String sControllerDomain)
{
/** Prepare our context and socket */
m_context = ZMQ.context(1);
m_clients = m_context.socket(ZMQ.ROUTER);
// m_clients = m_context.socket(ZMQ.REP);
m_clients.bind (sControllerDomain);
m_workers = m_context.socket(ZMQ.DEALER);
m_workers.bind ("inproc://workers");
}
/**
* Get ZeroMQ context
* #return ZMQ.Context
*/
public ZMQ.Context getContext() {
return m_context;
}
/**
* get ZeroMQ Socket
* #return ZMQ.Socket
*/
public ZMQ.Socket getSocket() {
return m_socket;
}
/**
* get the workers as ZMQ.Socket
* #return ZMQ.Socket
*/
public ZMQ.Socket getWorkers() {
return m_workers;
}
/**
* get the Clients as ZMQ.Socket
* #return ZMQ.Socket
*/
public ZMQ.Socket getClients() {
return m_clients;
}
}
What your OS system? If you are using Windows the operations using: m_workers.bind ("inproc://workers") is not supported. IIRC.

Categories