PrintWriter is null in java Server - java

I have got a problem setting up a server - client connection for a robot in java.
I have got two clients listening to two different ports. When the server is sending an error is occurring the PrintWriter is null.
Perhaps the methods are in two different instances. But how can I fix that?
Server code:
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
// port for the TCP/IP network
private static int port = 30001;
public void setPort(int newPort) {
port = newPort;
}
private ServerSocket serverSocket;
private Socket socket;
private PrintWriter pw;
public void start() throws IOException {
System.out.println("Server: Hi, I am ready to serve you!");
System.out.println("Server: Trying to connect.");
// get a connection
try {
serverSocket = new ServerSocket(port);
socket = serverSocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Server: I got a connection!");
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
}
public void sendString(String msg) {
pw.println(msg);
pw.flush();
}
public void stop() throws IOException {
pw.close();
socket.close();
serverSocket.close();
System.out.println("Server has stopped");
}
}
Code of the coordinating class:
package ServerV4Test;
import java.io.IOException;
public class CoordinateServer {
private Server myServer01 = new Server();
private Server myServer02 = new Server();
public void sendString(String msg) {
myServer01.sendString(msg);
myServer02.sendString(msg);
}
public void startServerMaster () throws IOException {
System.out.println("The server coordinator started!");
Server myServer01 = new Server();
myServer01.setPort(30001);
myServer01.start();
Server myServer02 = new Server();
myServer02.setPort(30002);
myServer02.start();
}
}
Programm code:
import java.io.IOException;
public class ProgramServer {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
CoordinateServer coordServer = new CoordinateServer();
coordServer.startServerMaster();
String sendString = "hello world";
coordServer.sendString(sendString);
coordServer.closeServerMaster();
}
}
Error message:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.PrintWriter.println(String)" because "this.pw" is null
at ServerV4Test.Server.sendString(Server.java:40)
at ServerV4Test.CoordinateServer.sendString(CoordinateServer.java:11)
at ServerV4Test.ProgramServer.main(ProgramServer.java:16)
Client code:
import java.io.*;
import java.net.*;
import java.util.ArrayList;
public class Client {
// set port and IP for the server
private String hostname = "localhost";
private int port;
public void setHostname (String sHost) {
hostname = sHost;
}
public void setPort(int sPort) {
port = sPort;
}
private InetSocketAddress address;
private void createAddress() {
address = new InetSocketAddress(hostname, port);
}
// create a list for the received strings
private ArrayList<String> receivedList= new ArrayList<String>();
public String getReceivedString() {
String temp = receivedList.get(0);
receivedList.remove(0);
return temp;
}
public boolean hasReceivedString() throws IOException {
receiveString();
if (receivedList.size() > 0) {
return true;
}
else {
return false;
}
}
private Socket socket;
private BufferedReader bufReader;
public void start() throws IOException {
System.out.println("Client: I start myself!");
System.out.println("Client: creating connection!");
socket = new Socket();
createAddress();
socket.connect(address);
System.out.println("Client: I got a connection!");
InputStreamReader iStreamReader = new InputStreamReader(socket.getInputStream());
bufReader = new BufferedReader(iStreamReader);
}
private void receiveString() throws IOException {
while (bufReader.ready()) {
if(bufReader.ready()) {
String message = bufReader.readLine();
receivedList.add(message);
}
}
}
public void stop() throws IOException {
bufReader.close();
socket.close();
System.out.println("Client has stopped");
}
}
Client programm:
import java.io.IOException;
public class ProgramUseClient1 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("client testprogram started");
// create an instance of the server
Client myClient = new Client();
myClient.setPort(30001);
// start client
myClient.start();
// repeat receiving and sending
boolean progRunning = true;
while(progRunning) {
// test if something is received
if(myClient.hasReceivedString()) {
String receivedString = myClient.getReceivedString();
System.out.println("The client 1 received: " + receivedString);
// test if client should be stopped
myClient.stop();
progRunning = false;
}
}
System.out.println("Goodbye");
}
}

You have created shadowed variables myServer01 and myServer02 where the global variables do not have PrintWriter pw initialized. Replace
Server myServer01 = new Server();
with
myServer01 = new Server();
and similarly for myServer02

It is all about timing of events...
When you instantiate a Server object, the global variable pw is null. It is not until Server#start() is called that the writer is properly instantiated. Unfortunately for you, the CoordiateServer calls sendString before the Server objects are running (start() is never called).
There is no reason why you need to delay the creation of the PrintWriter. Make sure it is instantiated in the Server constructor.

Related

How to use existing Objects in an overwritten run() Method?

I'm a beginner in programming and do strugle a bit.
So I'm building a TCP peer to peer Chat and that requires me to divide the tasks -> Threads.
So I want to built a Thread for the "writing" part of that connection (Scanner, DataOutputStream etc.) In order to do that i implemented Runnable and that forces me to write my Thread in the overwritten run() method.
Now I have a bit of a problem, because in Order to send my messages out to the "other end (another client) I need the "socket.getOutputStream" but I cant use it in the run() method and i dont know how to fix this problem, sitting already a week on this problem. Any ideas ?
public class ClientHorcher implements Runnable {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(11111);
System.out.println("Waiting For Connection:-");
Socket socket = serverSocket.accept();
Scanner scanner = new Scanner(System.in);
DataInputStream datenRein = new DataInputStream(socket.getInputStream());
DataOutputStream datenRaus = new DataOutputStream(socket.getOutputStream());
String nickname;
System.out.print("Gib einen Nickname ein: ");
nickname = scanner.nextLine();
while (true) {
String vonMsg = datenRein.readUTF(in);
System.out.println("Client:-" + vonMsg);
if (vonMsg.equals("exit")) {
System.out.println("Beenden!!!");
datenRein.close();
datenRaus.close();
scanner.close();
socket.close();
serverSocket.close();
System.exit(0);
}
System.out.print(nickname + ":-");
String zuMsg = scanner.nextLine();
datenRaus.writeUTF(zuMsg);
if (zuMsg.equals("exit")) {
System.out.println("Quiting!!!");
datenRein.close();
datenRaus.close();
scanner.close();
socket.close();
serverSocket.close();
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
}
}`
`public class ClientVerbinder implements Runnable {
public static void main(String[] args) {
try {
Socket socket = new Socket("Localhost", 11111);
System.out.println("Connected");
Scanner scanner = new Scanner(System.in);
DataInputStream datenRein = new DataInputStream(socket.getInputStream());
DataOutputStream datenRaus = new DataOutputStream(socket.getOutputStream());
String nickname;
System.out.print("Gib einen Nickname ein: ");
nickname = scanner.nextLine();
while (true) {
System.out.print(nickname+":-");
String zuMsg = scanner.nextLine();
datenRaus.writeUTF(zuMsg);
if (zuMsg.equals("exit")) {
System.out.println("Beenden!!!");
datenRein.close();
datenRaus.close();
scanner.close();
socket.close();
System.exit(0);
}
String vonMsg = datenRein.readUTF();
System.out.println("CLient"+":-" + vonMsg);
if (vonMsg.equals("exit")) {
System.out.println("Quiting!!!");
datenRein.close();
datenRaus.close();
scanner.close();
socket.close();
System.exit(0);
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
}
}
Multiple possibilities.
Use static variables. Normally you would not do that, but because your code is all-static, you could
Use member variables in an OO-style code.
If you wanted to do it properly, you'd split the server into two parts:
The ServerSocket listener that only listens to connections (Socket connectionToClient = ss.accept), and whenever a connection comes in, creates a new
ClientHandler, passes in the Socket, and the ClientHandler then starts its own thread internally
This way, the ClientHandler has all the data it needs (again as member variables) and can work on its own concerns (aka 'separation of concerns).
Update
This is what I created. Really simple.
Start Server
Start 1st client
Start 2nd client
In any client, type message
Other client receives it
Classes:
SimpleServer
package stackoverflow.simplemtserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.LinkedBlockingDeque;
public class SimpleServer { // make it closeable and close sockets if this is not standalone
public static void main(final String[] args) throws IOException {
final int port = 7623;
new SimpleServer(port);
}
final LinkedBlockingDeque<SimpleServerClientHandler> clientHandlers = new LinkedBlockingDeque<>();
private final ServerSocket mServerSocket;
public SimpleServer(final int pPort) throws IOException {
mServerSocket = new ServerSocket(pPort);
final Thread m = new Thread(() -> runLoop(), getClass().getSimpleName() + " Main Loop");
m.start();
}
private void runLoop() {
while (true) {
try {
System.out.println("Server waiting for connection...");
#SuppressWarnings("resource") final Socket cs = mServerSocket.accept(); // do NOT close the socket here, no try-resource, will interrupt threaded communication!
final SimpleServerClientHandler ch = new SimpleServerClientHandler(this, cs);
clientHandlers.add(ch);
System.out.println("Connection accepted, handler started. Handlers active: " + clientHandlers.size());
} catch (final IOException e) {
// handle this how you need it
e.printStackTrace();
}
}
}
public void signOffClientHandler(final SimpleServerClientHandler pClientHandler) {
clientHandlers.remove(pClientHandler); // we could also accommplish this using stack trace to avoid access from outside, but this is the easier solution
}
public void spreadMessageToClients(final String pMessageFromClient, final SimpleServerClientHandler pSimpleServerClientHandler) {
for (final SimpleServerClientHandler ch : clientHandlers) {
if (ch != pSimpleServerClientHandler) ch.relayMessageToClient(pMessageFromClient); // we can work with identity == and != here
}
}
}
SimpleServerClientHandler
package stackoverflow.simplemtserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class SimpleServerClientHandler {
private final SimpleServer mParentServer;
private final Socket mClientSocket;
private final DataInputStream mDIS;
private final DataOutputStream mDOS;
public SimpleServerClientHandler(final SimpleServer pSimpleServer, final Socket pCS) throws IOException {
mParentServer = pSimpleServer;
mClientSocket = pCS;
mDIS = new DataInputStream(mClientSocket.getInputStream());
mDOS = new DataOutputStream(mClientSocket.getOutputStream());
final Thread t = new Thread(() -> runComms(), getClass().getSimpleName() + " Comms Loop");
t.setDaemon(true); // threads now stop once server stops. this is NOT a soft exit
t.start();
}
private void runComms() {
try {
try {
while (true) {
// do all your logic here, work with DIS and DOS
final String messageFromClient = mDIS.readUTF();
if (messageFromClient == null) break;
if (!messageFromClient.startsWith("*")) mParentServer.spreadMessageToClients(messageFromClient, this);
}
} catch (final Exception e) {
// TODO: handle exception
}
} finally {
try {
mClientSocket.close(); // also closes DataIn/Out
} catch (final IOException e) { /* ignore */ }
mParentServer.signOffClientHandler(this);
}
}
public void relayMessageToClient(final String pMessageFromClient) {
try {
mDOS.writeUTF("*" + pMessageFromClient);
} catch (final IOException e) {
// ignore unless needed otherwise
}
}
}
SimpleClient
package stackoverflow.simplemtserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
public class SimpleClient {
public static void main(final String[] args) throws UnknownHostException, IOException {
final String hostname = "localhost";
final int port = 7623;
try (final Socket s = new Socket(hostname, port);
final DataInputStream dis = new DataInputStream(s.getInputStream());
final DataOutputStream dos = new DataOutputStream(s.getOutputStream());
final Scanner scanner = new Scanner(System.in);) {
final Thread t = new Thread(() -> runListenerLoop(dis), SimpleClient.class.getSimpleName() + " Reader Thread");
t.setDaemon(true);
t.start();
while (true) {
System.out.println("Enter message:");
System.out.flush();
final String msg = scanner.nextLine();
if (msg == null) break;
System.out.println("Spreading message: " + msg);
dos.writeUTF(msg);
}
}
}
private static void runListenerLoop(final DataInputStream pDis) {
while (true) {
try {
System.out.println("Waiting for incoming messages...");
final String msg = pDis.readUTF();
System.out.println("Received: " + msg);
} catch (final SocketException e) {
// if ("java.net.SocketException: Connection reset".equals(e.getMessage()))
break;
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
From here on, there's now more ways you can extend this client/server communication:
use opcodes (read/write int constants or enums before data) to distinguish for special operations and/or messages (like this here puts an asterisk in front of the string to prevent re-re-laying the same message indefinitely over the network)
read multiple strings for user, message, additional options
implement this is a user friendly UI so you dont have to use console I/O
Update 2
The pure peer-to-peer solution is this one.
If the app is run without params, it goes into listening mode, waiting for a connection.
If the app is run with one arguments, it interprets it as listening port and also goes into listening mode.
If the app is un with [hostname] [port] arguments, it will try to connect there
Example:
start first app without arguments (listening)
start second app with arguments "localhost 7642" (connecting)
both apps will now
connect,
then set up I/O resources,
then start the listening thread for incoming messages
then go into the read-keyboard-and-write-to-socket loop
now you can type a message in one of the apps, the other one will receive it
p2p code:
package stackoverflow.simplemtserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
public class SimplePeerToPeerClient {
static public final int DEFAULT_PORT = 7642;
public static void main(final String[] args) throws UnknownHostException, IOException {
if (args.length == 0) {
System.out.println("Waiting on default port " + DEFAULT_PORT);
waitForConnection(DEFAULT_PORT);
} else if (args.length == 1) {
final int port = Integer.parseInt(args[0]);
System.out.println("Waiting on port " + port);
waitForConnection(port);
} else if (args.length == 2) {
final String hostName = args[0];
final int port = Integer.parseInt(args[1]);
System.out.println("Connecting to " + hostName + " on port " + port);
connectToOtherSide(hostName, port);
} else throw new IllegalArgumentException("Invalid amount of argument! Need none (listen) or 2: [hostname] [port] (connect)");
}
private static void waitForConnection(final int pPort) throws IOException {
try (final ServerSocket ss = new ServerSocket(pPort);) {
#SuppressWarnings("resource") final Socket socket = ss.accept(); // will get closed later
startComms(socket);
} // closes ServerSocket after 1st connection
}
private static void connectToOtherSide(final String pHostName, final int pPort) throws UnknownHostException, IOException {
#SuppressWarnings("resource") final Socket socket = new Socket(pHostName, pPort); // will get closed later
startComms(socket);
}
private static void startComms(final Socket pSocket) throws IOException {
try (
final DataInputStream dis = new DataInputStream(pSocket.getInputStream());
final DataOutputStream dos = new DataOutputStream(pSocket.getOutputStream());
final Scanner scanner = new Scanner(System.in);) {
// run the listener loop
final Thread t = new Thread(() -> runListenerLoop(dis), SimpleClient.class.getSimpleName() + " Reader Thread");
t.setDaemon(true);
t.start();
// run my keyboard-input-send loop
while (true) {
System.out.println("Enter message:");
System.out.flush();
final String msg = scanner.nextLine();
if (msg == null) break; // empty input ends client
System.out.println("Spreading message: " + msg);
dos.writeUTF(msg);
}
} finally {
try {
pSocket.close();
} catch (final IOException e) { /* ignore */ }
}
}
private static void runListenerLoop(final DataInputStream pDis) {
while (true) {
try {
System.out.println("Waiting for incoming messages...");
final String msg = pDis.readUTF();
System.out.println("Received: " + msg);
} catch (final SocketException e) {
// if ("java.net.SocketException: Connection reset".equals(e.getMessage()))
break;
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

BufferedReader readLine() stuck

I'm writing a TCP multi Client/Server app that works in an infinite loop.
What happens:
Client types "COMMANDS" and expects to get available commands from server.
Server sends lines to client.
Client reads server input line-by-line and is stuck after the last one is written.
Expected result:
Client is prompted for keyboard input.
Input is written to server.
Server sends back text.
Client reads server input line-by-line.
End of loop.
user types commands > server responds > user gets to type commands again
I know that BufferedReader would stop reading lines once I close the socket. However, that's not what we want since it has to work infinitely.
How do I exit the loop?
Client method:
public class Client {
private final Socket clientSocket;
private final BufferedReader clientInput;
private final PrintWriter clientOutput;
private final BufferedReader keyboardInput;
public Client() throws IOException {
this.clientSocket = new Socket(InetAddress.getLocalHost(), 13370);
this.clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
this.clientOutput = new PrintWriter(clientSocket.getOutputStream(), true);
this.keyboardInput = new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) throws IOException {
Client client = new Client();
client.init();
}
private void init() {
try {
while (true) {
System.out.print("> ");
String args = keyboardInput.readLine().trim().replaceAll("\\s+", " ");
clientOutput.println(args.toUpperCase());
// the loops doesn't seem to stop after reading all input lines
String line;
while ((line = clientInput.readLine()) != null) {
System.out.println(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ClientHandler implements Runnable {
private final int id;
private final Socket clientSocket;
private final PrintWriter serverOutput;
private final BufferedReader serverInput;
// private final ClientFiles clientFiles;
public int getId() {
return id;
}
public ClientHandler(int id, Socket clientSocket) throws IOException {
this.id = id;
this.clientSocket = clientSocket;
this.serverInput = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
this.serverOutput = new PrintWriter(this.clientSocket.getOutputStream(), true);
// this.clientFiles = new ClientFiles(id, clientSocket);
}
#Override
public void run() {
try {
String command;
while ((command = serverInput.readLine()) != null) {
executeCommand(command.split(" "));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
closeConnection();
removeFromHosts();
}
}
private void executeCommand(String[] input) throws IOException {
String command = input[0];
// String option = input[1];
switch (command) {
case "COMMANDS": {
getCommands();
break;
}
// case "LIST_LOCAL": {
// listFiles();
// break;
// }
// case "LIST_FILES": {
// listFiles(option);
// }
// case "PULL": {
// pull(clientSocket, option);
// }
// case "PUSH": {
// push(clientSocket, option);
// }
case "DISCONNECT": {
closeConnection();
break;
}
default: {
write("Invalid command.");
getCommands();
break;
}
}
}
private String read() throws IOException {
return serverInput.readLine();
}
private void write(String message) {
serverOutput.println(message);
}
private void getCommands() {
write("AVAILABLE COMMANDS:");
write("\tCOMMANDS");
write("\t\tlists available commands");
write("\tLIST_HOSTS");
write("\t\tlists hosts connected to server");
write("\tLIST_FILES");
write("\t\tlists files from all hosts connected server");
write("\tLIST_LOCAL");
write("\t\tlists local files");
write("\tLIST_FILES [HOSTS]...");
write("\t\tlists files from provided hosts connected server");
write("\tPULL [HOST] [FILE]");
write("\t\tdownloads file from host");
write("\tPUSH [HOST] [FILE]");
write("\t\tuploads file to host");
write("\tDISCONNECT");
write("\t\tdisconnects client from server");
}
private void closeConnection() {
try {
serverInput.close();
serverOutput.close();
clientSocket.close();
removeFromHosts();
} catch (IOException e) {
e.printStackTrace();
}
}
private void removeFromHosts() {
Server.getClients().remove(this);
}
}
Server just accepts new clients and starts new threads.
public class Server {
private static final AtomicInteger count = new AtomicInteger(1);
private static final ArrayList<ClientHandler> clients = new ArrayList<>();
public static void main(String[] args) {
Server.init();
}
public static void init() {
System.out.println("Opening server socket...");
try (
ServerSocket serverSocket = new ServerSocket(13370)
) {
System.out.println("Server socket opened at port: " + serverSocket.getLocalPort());
while (true) {
System.out.println("Waiting for client connection...");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
ClientHandler client = new ClientHandler(count.getAndIncrement(), clientSocket);
clients.add(client);
System.out.println(clients);
new Thread(client, "client-" + client.getId()).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static ArrayList<ClientHandler> getClients() {
return clients;
}
}
your client is waiting for more lines.
In your code
while ((line = clientInput.readLine()) != null) {
System.out.println(line);
}
clientInput.readLine() will never return null if server does not close the connection. The execution stops waiting more data.
I suggest to implements a solution like insert an empty line or a special char to signal the client that the response to the command is finished.

Java Client/Server resource leak after closing

I am trying to implement a java command line chat server and client. I am using 4 classes, the Server.java sets up a ServerSocket and accepts all connections. The Client.java can connect to the Server, then 2 threads for both client and server are created using the SendMessages.java and ReceiveMessages.java. The threads are responsible for taking the input from stdin and sending it to the output stream of the socket, and taking the incoming input stream to print it to stdout. Everything works when launching the server and connecting the client, the chat also works. However, when either client or server is terminated a resource leak is caused. I want the SendMessages class and ReceiveMessages class to be able to detect when the connection of client or server is terminated and close all resources to avoid resource leaks. Here is the code:
Server.java
import java.net.*;
import java.io.*;
import java.util.*;
public class Server{
public static void main(String[] args) throws SocketException,IOException{
ServerSocket s = new ServerSocket(8000);
s.setSoTimeout(10000000);
while(true){
Socket clientSocket = s.accept();
handle(clientSocket);
}
}
static void handle(Socket clientSocket) throws IOException{
System.out.println("connection accepted from " + clientSocket.getRemoteSocketAddress());
receiveMessages(clientSocket);
sendMessages(clientSocket);
}
static void receiveMessages(Socket clientSocket) throws IOException{
(new Thread(new ReceiveMessages(clientSocket))).start();;
}
static void sendMessages(Socket clientSocket)throws IOException{
(new Thread(new SendMessages(clientSocket))).start();
}
}
Client.java
import java.net.*;
import java.io.*;
import java.util.*;
public class Client{
public static void main(String[] args) throws IOException, UnknownHostException, ConnectException{
String hostname = args[0];
int port = Integer.parseInt(args[1]);
Socket s = null;
s = connect(hostname, port);
handle(s);
}
public static Socket connect(String hostname, int port) throws UnknownHostException, IOException, ConnectException{
Socket s = null;
try{
s = new Socket(hostname, port);
} catch(ConnectException e){
System.out.println("Connect Exception caught!");
}
return s;
}
static void handle(Socket clientSocket) throws IOException, UnknownHostException{
receiveMessages(clientSocket);
sendMessages(clientSocket);
}
static void receiveMessages(Socket clientSocket) throws IOException, NullPointerException{
(new Thread(new ReceiveMessages(clientSocket))).start();
}
static void sendMessages(Socket clientSocket)throws IOException, NullPointerException{
(new Thread(new SendMessages(clientSocket))).start();
}
}
SendMessages.java
import java.io.*;
import java.net.*;
import java.util.*;
public class SendMessages implements Runnable{
Socket clientSocket;
public SendMessages(Socket clientSocket){
this.clientSocket = clientSocket;
}
public void run(){
System.out.println("SendMessages thread has started.");
Scanner sc = new Scanner(System.in);
PrintWriter out = null;
try{
out = new PrintWriter(clientSocket.getOutputStream(), true);
}
catch(IOException | NullPointerException e){}
String message;
while(true){
message = sc.nextLine();
out.println(message);
if(out.checkError()){
out.close();
sc.close();
System.out.println("SendMessages closed");
}
}
}
}
ReceiveMessages.java
import java.io.*;
import java.net.*;
import java.util.*;
public class ReceiveMessages implements Runnable{
Socket clientSocket;
public ReceiveMessages(Socket clientSocket){
this.clientSocket = clientSocket;
}
public void run(){
System.out.println("ReceiveMessages thread has started.");
BufferedReader in = null;
try{
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}catch(IOException|NullPointerException e){}
String message;
try{
while(true){
while((message = in.readLine())!= null){
System.out.println(message);
}
}
} catch(IOException|NullPointerException e){
System.out.println("ReceiveMessages resources closed.");
try{
in.close();
clientSocket.close();
}
catch(IOException f){}
}
}
}
Thank you!
So I have recently delt myself with such an issue.
The problem is, that by shutting one side of the connection down, a "half-open" connection remains (which isn't a real connection anymore).
The Socket API does not have an option to check, if the partnered service/device is still alive.
So what to do about that? I myself prefer the concept of heartbeats.
Every 10 seconds (or any other timeframe, it's up to you) a heartbeat is send to the output stream of the socket. This operation throws a IOException, when the stream is no longer avaiable. Therefore you can catch the the exception and handle all "close" operations within the catch block.
Example:
public class Heartbeater
implements Runnable
{
private OutputStream os;
private ServerHandler servhand;
private Logger logger = Logger.getLogger( Logger.GLOBAL_LOGGER_NAME );
//NOTE: ServerHandler is my class which handles the socket of the server after ServerSocket.accept();
public Heartbeater( OutputStream os, ServerHandler servhand )
{
this.servhand = servhand;
this.os = os;
}
#Override
public void run()
{
try
{
Thread.currentThread().setName( "Heartbeater" );
// while the handler's connection is alive
while ( servhand.isConnectionAlive() )
{
Thread.sleep( 10000 );
// dummy write to trigger the exception if the client does not respond properly
os.write( "_HEARTBEAT_".getBytes() );
}
}
catch ( InterruptedException e )
{
logger.log( Level.SEVERE, "HEARTBEATER GOT INTERRUPTED", e );
Thread.currentThread().interrupt();
}
catch ( IOException e )
{
logger.log( Level.INFO, "The connection to the client has been lost." );
// Here you would define the resource close operation.
servhand.setConnectionAlive( false );
}
}
}

java network programming TCP chat full duplex

I'm implementing a simple tcp chat between a server and a client. I'm using multi-threading so as the server and the client can send and receive data at the same time (full duplex). The program works but if the server has a console for both typing the sending message and also displaying receiving message (same case for client), I cannot edit my typed message that should be sent to server or client when a message has been received from the other side. For e.g:
run(server console):
input msg to send client:
you:
client: hi server
client: bye server.
For this example, i've typed a message to send to client while the client has already said hi server bye server. Before receiving from client, i see what i've typed but after receiving, I can't see the msg nor edit it.
I can only use console because im not good with GUI and i want the same console for both sending and receiving data.
The codes for the program are shown below.
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class ThreadServerSend implements Runnable {
String d;
Socket s1 = null;
Scanner sc = new Scanner(System.in);
public ThreadServerSend(Socket s)
{
s1=s;
}
public void run()
{
System.out.println("input msg to send client: ");
while (true){
try{
PrintStream p = new PrintStream(s1.getOutputStream());
System.out.println("you: ");
d=sc.nextLine();
p.println(d);
if (d.charAt(d.length()-1)=='.'){
s1.close();
break;}
}
catch(IOException e){}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class ThreadServerReceive implements Runnable {
String m;
Socket s2 = null;
Scanner sc = new Scanner(System.in);
public ThreadServerReceive(Socket s)
{
s2=s;
}
public void run()
{
while (true){
try{
BufferedReader b = new BufferedReader(new InputStreamReader(s2.getInputStream()));
m = b.readLine();
System.out.println("client: "+m);
if (m.charAt(m.length()-1)=='.'){
s2.close();
break;}}
catch(IOException e){}
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
public static void main(String[] args) throws UnknownHostException, IOException{
// TODO Auto-generated method stub
ServerSocket s = new ServerSocket(444);
Socket s1 = s.accept();
new Thread(new ThreadServerSend(s1)).start();
ServerSocket s4 = new ServerSocket(443);
Socket s2=s4.accept();
new Thread(new ThreadServerReceive(s2)).start();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class ThreadClientSend implements Runnable {
String d;
Socket s1 = null;
Scanner sc = new Scanner(System.in);
public ThreadClientSend(Socket s)
{
s1=s;
}
public void run()
{
System.out.println("Input msg to send server: ");
while (true){
try{
PrintStream p = new PrintStream(s1.getOutputStream());
System.out.println("you: ");
String d = new Scanner(System.in).nextLine();
p.println(d);
if (d.charAt(d.length()-1)=='.'){
s1.close();
break;}
}
catch(IOException e){}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class ThreadClientReceive implements Runnable {
String m;
Socket s1 = null;
Scanner sc = new Scanner(System.in);
public ThreadClientReceive (Socket s)
{
s1=s;
}
public void run()
{
while (true){
try{
BufferedReader b = new BufferedReader(new InputStreamReader(s1.getInputStream()));
m= b.readLine();
System.out.println("Server: "+m);
if (m.charAt(m.length()-1)=='.')
{
s1.close();
break;
}
}
catch(IOException e){}
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException{
// TODO Auto-generated method stub
Socket s1= new Socket("localhost",444);
Socket s2 = new Socket("localhost",443);
new Thread(new ThreadClientReceive(s1)).start();
new Thread(new ThreadClientSend(s2)).start();
}
}
A bit late, but I actually came up with a working version of this chat client for my programming class. I thought I might as well post it here. I used TCP (full duplex) with multithreading, and I got it to work in console. The small problem is that you can't see your own name (because console has only one active line), but otherwise it works pretty well.
Server (client is more or less the same, obviously the Sockets are normal sockets, not ServerSockets):
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
#SuppressWarnings("serial")
public class serverProgII extends Thread
{
private static ObjectOutputStream oos;
private static ObjectInputStream ois;
private static Socket connection;
private static ServerSocket server;
private static String ip, clientIP, textin, exitword ="exit";
private static networkmessage nmessage;
private static boolean connected = false;
private static boolean done = false;
private static String myName = "";
private static int counter = 0;
public static boolean started = false;
public String type;
public static void main(String[] args)
{
try {
BufferedReader brin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name:> "); //send prompt to DOS window
myName = brin.readLine(); //read in user input
setupConnection();
setupStreams();
started = true;
} catch (Exception e) {
System.out.println("Problem setting up streams and connection!");
}
serverProgII sender = new serverProgII("sender");
serverProgII receiver = new serverProgII("receiver");
sender.start();
receiver.start();
}
public serverProgII(String t)
{
super();
type = t;
}
public void run() {
while(started) {
switch(type) {
case "sender":
sender();
break;
case "receiver":
receiver();
break;
}
try {
Thread.sleep(500); //milliseconds
} catch(Exception e){}
}
}
/* runServer()
This is where all the actual work gets done.
*/
public void sender()
{
try {
BufferedReader inn = new BufferedReader(new InputStreamReader(System.in));
textin = inn.readLine();
sendData(textin);
if (textin.equals(exitword)) // if "exit" is typed in shutdown the server.
{
started = false;
serverShutdown();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public void receiver() {
try {
getData();
} catch(Exception e) {
System.out.println("Error getting data");
}
}
//setup connection
public static void setupConnection() throws IOException
{
System.out.println("SERVER MODE ACTIVATED");
server = new ServerSocket (8000); //create the socket at port 8000
System.out.println("Waiting For Connection...");
connection = server.accept(); //wait for a connection
System.out.println("Received connection: "+connection.getInetAddress());
clientIP=""+connection.getInetAddress(); //print out the client IP address
}//setupconnection()
//Setup streams connection
public static void setupStreams() throws IOException
{
//Open up Streams
System.out.println("Streams Setup");
oos=new ObjectOutputStream(connection.getOutputStream()); //construct object output stream
ois=new ObjectInputStream(connection.getInputStream());
oos.flush();
}//setupStreams()
//method to write/send network data
public void sendData(String toSend) throws IOException
{
try
{
nmessage = new networkmessage(myName, toSend);
oos.writeObject(nmessage);
}//try
catch (IOException ioException)
{
System.out.println("IO exception in sendData");
}
}//sendData()
//method to read in network data
public void getData() throws IOException
{
try
{
networkmessage messageIn =(networkmessage)(ois.readObject());
System.out.println(messageIn.ipnum +" << "+messageIn.text);
}//try
catch (Exception exp1)
{
System.out.println("IO exception in sendData");
}
}//getData()
public void serverShutdown()
{
System.out.println("exiting initiated");
try
{
done = true;
textin = "Chat is terminated. Have a nice day.";
oos.close();
server.close();
connection.close();
}
catch (Exception One)
{
System.out.println("bad termination");
}
}
} // class serverProg

java server is receiving from 1 client only and not from the 2nd

Good day, I'm trying to create a Client/Server chat, that the 1st client send message to server and the server send it to client 2 and vice versa, but the server is just receiving from 1st client only and not from 2nd.
and also, how can i send messages from client 1 to 2 & vice versa
package s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client
{
public static void main(String[] args) throws IOException, Throwable
{
Socket s;
BufferedReader in;
PrintWriter out;
Scanner sc = new Scanner(System.in);
s = new Socket(InetAddress.getLocalHost(),9000);
System.out.println("Connection pending");
in = new BufferedReader (new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
String msg = in.readLine();
System.out.println(msg);
while(msg!="")
{
msg = sc.nextLine();
out.println(msg+"\n");
out.flush();
}
s.close();
sc.close();
}
}
=============================================
package s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class TClient extends Thread
{
private int num;
private Socket s;
private BufferedReader in;
private PrintWriter out;
public Socket getS()
{
return s;
}
public BufferedReader getIn()
{
return in;
}
public PrintWriter getOut()
{
return out;
}
public TClient(Socket s,int num) throws IOException
{
this.s = s;
this.setNum(num);
System.out.println("Client"+num);
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader (new InputStreamReader(s.getInputStream()));
out.println("Connected"+num+"\n");
out.flush();
}
public void run()
{
while(true)
{
try
{
String msg="";
msg = in.readLine();
System.out.println(msg);
if (msg.equals(".")) break;
}
catch (IOException e)
{
}
}
try
{
s.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getNum()
{
return num;
}
public void setNum(int num)
{
this.num = num;
}
}
========================================================
package s;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String[] args)
{
ServerSocket ss ;
Socket s = null ;
int nb_clients = 0;
String msg = "";
TClient[] connexions = new TClient[2];
try
{
ss = new ServerSocket(9000);
System.out.println("Server is listening in:"+ss.getLocalPort());
boolean continu = false;
while(!continu)
{
s = ss.accept();
connexions[nb_clients] = new TClient(s,nb_clients+1);
connexions[nb_clients].start();
nb_clients++;
if (nb_clients>=2) continu=true;
}
System.out.println("Clients connected");
s.close();
ss.close();
}
catch(IOException e)
{
}
}
}
========================================
and the output of each terminal is :
Server is listening in:9000
Client1
Client2
Clients connected
and the output of the 2 clients are :
Connection pending
Connected1
Connection pending
Connected2
if i write a message from 1 & 2 to the server, the server output will be like this:
Server is listening in:9000
Client1
Client2
Clients connected
111111111111111111111111111111111111
================================================================
UPDATE :
i changed a condition in server
if (nb_clients>2) continu=true;
and now i can recieve from both clients, now i have to know how i make them communicate between the clients
Solution for two clients.
Sending messages from one client to another client.
The server will have to marshall the messages between the clients.
public class Server
{
static TClient[] connexions = new TClient[2];
public static void send(int clientNum, String message) {
TClient t = connexions[clientNum];
if(t != null) {
t.getOut().println(message);
t.getOut().flush();
}
}
}
In TClient added:
public void sendMessage(String message) {
int client = (getNum()-1)==1 ? 0:1;
Server.send(client, message);
System.out.printf("Sending message(%s) to client:%d from client:%d%n",message,client,getNum());
}
which I added a call to inside your while loop.
In Client I used another thread for receiving message from the server called ReceiveMessageThread:
public class Client
{
public static void main(String[] args) throws IOException, Throwable
{
Socket s;
BufferedReader in;
PrintWriter out;
Scanner sc = new Scanner(System.in);
s = new Socket(InetAddress.getLocalHost(), 9000);
System.out.println("Connection pending");
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
ReceiveMessageThread thread = new ReceiveMessageThread(in);
String msg = in.readLine();
System.out.println(msg);
thread.start();
while (msg != "")
{
msg = sc.nextLine();
out.println(msg + "\n");
out.flush();
}
s.close();
sc.close();
}
public static class ReceiveMessageThread extends Thread
{
private BufferedReader in;
public ReceiveMessageThread(BufferedReader in)
{
this.in = in;
}
public void run()
{
while (true)
{
try
{
String message = readMessage(in);
if (message != null && !message.equals(""))
System.out.println(message);
} catch (IOException ie)
{
ie.printStackTrace();
}
}
}
public String readMessage(BufferedReader in) throws IOException
{
String msgreceived = "";
String readValue = "";
while (in.ready())
{
readValue = in.readLine();
if (readValue != null)
msgreceived += readValue;
}
return msgreceived;
}
}
}
in server i changed :
if (nb_clients>2) continu=true;
and the server reads from different clients.

Categories