Why isn't my server Reacting to input until after I disconnect? - java

I have a simple Server-Multi Client program that sends input from a user out to everyone.
The Server gets messages, and then sends them systematically out to everyone.
The problem I'm having is that the server gets the messages, but doesn't send them back until I disconnect from the server. I have one thread for each user, and one thread that sends out the messages.
Here is the Main server:
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(25566);
SendThread SendThread = new SendThread();
new Thread(SendThread).start();
while(true){
Socket clientSocket = serverSocket.accept();
CollectThreads socketThread = new CollectThreads(clientSocket);
SendThread.Clients.add(socketThread);
new Thread(socketThread).start();
}
}
Here is the Input Thread(CollectThreads):
public Socket Client;
public CollectThreads(Socket socket)
{
//Here we set the socket to a local variable so we can use it later
Client = socket;
}
#Override
public void run() {
System.out.println("i");
try{
PrintWriter out = new PrintWriter(Client.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(Client.getInputStream()));
out.println("Connected");
boolean x = true;
while(x){
String s;
if((s = in.readLine()) != null){
System.out.println(s);
SendThread.log.add(s);
}
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
And the Sender(SendThread):
public ArrayList<CollectThreads> Clients = new ArrayList<CollectThreads>();
public static ArrayList<String> log = new ArrayList<String>();
#Override
public void run() {
try{
while(true){
String x;
while(!Clients.isEmpty()){
x = log.get(0);
for(int c = 0; c < Clients.size(); c += 1){
PrintWriter out = new PrintWriter(Clients.get(c).Client.getOutputStream(), true);
out.println(x);
System.out.println(x);
}
log.remove(0);
}
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
If I input x, The server console prints x, I disconnect, and then it prints x the second time.
Can anyone find my mistake? or tell me why this isn't working?

You're never removing anything from SendThread.Clients, so I can't see why it won't just keep resending the same thing indefinitely.
It's a very strange design. Why not have each client thread send its own output, like everybody else?

Related

Java - How do I loop to read the InputStream until the user makes an input to continue?

I have a multithreaded client-server system which works back and forth with the client communicating first and the server replying.
However, for two specific clients, I need them to constantly check if there is data held in the input stream before proceeding when the user makes an input.
The program is a car park management system. When the car park is full(0 spaces available) and a car arrives at an entrance client, the system forms a queue of clients waiting to grant entry. When a car leaves the car park, the first client in the queue is removed and added to a BlockingQueue for that specific entrance client. I have created a direct output output stream for each of the entrance clients. So when a BlockingQueue is not empty, data is taken from this queue and output is sent to the stream of that specific client.
However, the problem is - the entrance client which was queued should automatically read its InputStream and print the data to grant access, but instead it causes an error and crashes. I think what is happening is that when the system first starts, the is the client is stuck waiting to read data which initially doesn't exist because it would require some sort of input at the first stage, causing an error.
How do I fix this so that the client reads and prints the input stream(whether it be specific data such as contains the word "queue") IF there is data available else to continue IF the user makes an input.
I hope this makes sense, I tried to make it as clear as possible.
Server class:
public class Server {
public static void main(String[] args) throws IOException {
//Create the shared objects in the global scope...
int groundFloor = 0; //SET TO 0 FOR TESTING
int firstFloor = 0;
SharedState SharedStateObject = new SharedState(groundFloor,firstFloor);
//Sets up the server socket on port 4444
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(4444);
System.out.println("Car Park Server started." + "\n");
}
catch (IOException e) {
System.err.println("Could not start server on specified port.");
System.exit(-1);
}
//Got to do this in the correct order with only four clients!
ServerThread GroundFloorEntrance = new ServerThread(serverSocket.accept(), "GroundFloorEntrance", SharedStateObject);
ServerThread FirstFloorEntrance = new ServerThread(serverSocket.accept(), "FirstFloorEntrance", SharedStateObject);
ServerThread GroundFloorExit1 = new ServerThread(serverSocket.accept(), "GroundFloorExit1", SharedStateObject);
ServerThread GroundFloorExit2 = new ServerThread(serverSocket.accept(), "GroundFloorExit2", SharedStateObject);
GroundFloorEntrance.start();
FirstFloorEntrance.start();
GroundFloorExit1.start();
GroundFloorExit2.start();
serverSocket.close();
//Loop for granting queued clients access
while(true)
{
BlockingQueue<String> queuedGroundAccess = SharedStateObject.getQueuedGround();
BlockingQueue<String> queuedFirstAccess = SharedStateObject.getQueuedFirst();
if(!queuedGroundAccess.isEmpty())
{
Socket clientSocket = GroundFloorEntrance.clientSocket();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
try
{
out.println(queuedGroundAccess.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(!queuedFirstAccess.isEmpty())
{
Socket clientSocket = FirstFloorEntrance.clientSocket();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
try
{
out.println(queuedFirstAccess.take());
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
Client
public class GroundFloorEntrance {
#SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
// Set up the socket, in and out variables
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
int port = 4444;
String serverName = "localhost";
String clientID = "Ground Floor Entrance";
try {
clientSocket = new Socket(serverName, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: "+ port);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer = null;
String fromUser = null;
System.out.println("Initialised " + clientID + " client and IO connections");
//I THINK THE ISSUE IN THE FOLLOWING STRUCTURE:
while (true) {
fromServer = in.readLine();
if(fromServer != null && fromServer.contains("Queue: "))
{
System.out.println(fromServer);
}
fromUser = stdIn.readLine();
if (fromUser != null) {
out.println(fromUser);
}
fromServer = in.readLine();
System.out.println(fromServer);
}
}
}
A problem is in this loop. When you write fromServer = in.readLine(); it stop execution of your program and waits for data to be entered from server.
while (true) {
fromServer = in.readLine();
if(fromServer != null && fromServer.contains("Queue: "))
{
System.out.println(fromServer);
}
fromUser = stdIn.readLine();
if (fromUser != null) {
out.println(fromUser);
}
fromServer = in.readLine();
System.out.println(fromServer);
}
What you can do with that? You should read data from server in another thread to prevent blocking main thread while waiting for data. Like that:
new Thread(new MyRunnable(fromServer)).start();
And MyRunnable will look like this:
public class MyRunnable implements Runnable {
private Scanner scanner;
public MyRunnable(Scanner scanner) {
this.scanner = scanner;
}
#Override
public void run() {
while (true) {
if (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
}
}
}
If you will have some questions, please ask.

User input does not work within a java thread. What am I doing wrong?

Ok, I am trying to make a simple chat server and client. This program is the chat server.
I have two threads that are running. The first thread handles all of the clients and binds each new client to a new socket using ServerSocket and the second thread for now allows the user to send a message to the connected client. I cannot, however, get user input from within this thread! The statement "String inputvar = br.readLine()" will not execute! What am I doing wrong?
public void relay() throws IOException
{
new Thread(new Runnable()
{
#Override
public void run()
{
try{
try
{
while (true)
{
Socket socket2 = null;
Socket socket1 = socketmethod(socket2,0);
System.out.println(socket1);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (socket1 != null)
{
PrintWriter outputriver = new PrintWriter(socket.getOutputStream(),true);
boolean cfd = true;
while(cfd==true)
{
System.out.println("this worked");
System.out.print("Message: ");
String inputvar = br.readLine();
System.out.println("this worked2");
if (inputvar.equals("disconnect_now"))
{
cfd = false;
}
outputriver.println("Unknown User: " + inputvar);
}
}
}
}
finally
{
System.out.println("error 1");
}
}
catch (IOException e)
{
System.out.println("Unexpected error: IOException in thread2");
}
}
}).start();
I can't tell from your code where you are initiating accepting your client connection. I would expect to see something like this...
public void replay() {
try {
serverSocket = new ServerSocket(port);
while(true) {
try {
final Socket connection = serverSocket.accept();
new Thread(new Runnable() {
#Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String value = br.readLine();
PrintWriter pw = new PrintWriter(connection.getOutputStream(), true);
pw.println(value);
However if you are accepting multiple client requests having many threads, you will have them block on br.readline() as they compete for System.in
Also make sure that your input is sending the correct "new line" character(s) in order to fulfill the readline() requirements.

Cannot communicate back to server from client

Here is my Server class, along with Admin/Client... The admin can recieve the initial message from the server, but cannot respond back to the server. The Server Socket is created, and when the admin connects to the Server, The Server's message is recieved and printed. Although once the Server's menu printout("1. Login, etc..."). I cannot enter an option via the console. It seems to be in a deadlock as soon as the admin prints out the menu sent from the server.
/**
A server that executes the Simple Bank Access Protocol.
*/
public class BankServer extends Thread {
private Socket sAdmin;
private Scanner inServer;
private PrintWriter outServer;
public static void main(String[] args) throws IOException {
Bank bank = new Bank(10);
ServerSocket server = new ServerSocket(8888);
System.out.println("Waiting for clients to connect...");
Thread admin = new BankServer();
admin.start();
while (true) {
Socket s = server.accept();
System.out.println("Client connected.");
BankService service = new BankService(s, bank);
Thread t = new Thread(service);
t.start();
}
}
#SuppressWarnings("resource")
public void run() {
try {
ServerSocket server = new ServerSocket(8889);
try {
System.out.println("Waiting for admin to connect...");
while (true) {
sAdmin = server.accept();
System.out.println("Admin connected.");
InputStream instream = sAdmin.getInputStream();
OutputStream outstream = sAdmin.getOutputStream();
inServer = new Scanner(instream);
outServer = new PrintWriter(outstream);
outServer.print(...+"1. Login\n"+etc...);
outServer.flush();
findCommand();
}
} finally {
sAdmin.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void findCommand() throws IOException {
while (true) {
if (!inServer.hasNext()) {
return;
}
String command = inServer.next();
String chosenCommand;
switch (command) {
case "1":
chosenCommand = "LOGIN";
runCommand(chosenCommand);
break;
case "2":
etc...
}
}
}
...
}
Ive removed the runCommand method and some fields that don't cause problems to allow for an easier read.
Here is the the admin class. It will output the options sent from the server, but will not exit the loop of outputing the intial message, and allow me to enter a choice.
public class BankAdmin {
public static void main(String[] args) throws IOException {
Socket inAdmin = new Socket("localhost", 8889);
InputStream instream = inAdmin.getInputStream();
OutputStream outstream = inAdmin.getOutputStream();
Scanner in = new Scanner(instream);
PrintWriter adminOut = new PrintWriter(outstream);
Scanner console = new Scanner(System.in);
while (true) {
if (!in.hasNextLine()) {
String command = console.next();
System.out.println("Sending: " + command);
adminOut.print("command");
adminOut.flush();
} else {
String response = in.nextLine();
System.out.println(response);
}
}
}
}
This is what is happening:
After the server sends the menu to the client, the client becomes stuck in in.hasNextLine(). This is because the in.hasNextLine() will block (pause) for input, waiting for a new line (otherwise, it cannot determine whether there will be a next line). To fix this problem, you must use threads, with one waiting for input while the other reads from the console.
For more information, see the Javadoc on Scanner.hasNextLine()

"Socket is closed" exception when using a Socket that is passed to a Thread? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am making a simple Web Server where I spawn a new thread each time a client connects. I then need to send a response using a BufferedOutputStream(since the HTTP protocol requires a /r/n at the end of the line).
Here is my code:
class HttpServer{
public static void main(String args[]){
ServerSocket ss = null;
int port = 40000;
try{
if(args.length > 0){
port = Integer.parseInt(args[0]);
}
ss = new ServerSocket(port);
while(true){
Socket client = ss.accept();
User user = new User(client);
user.start();
}
}
catch(Exception e){
System.err.println(e.getMessage());
}
}
}
And my User class:
class User extends Thread{
Socket client;
public User(Socket s){
client = s;
}
public void run(){
try{
BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
println(bos, "Hello World");
bos.close();
client.close();
}
catch(Exception e){
System.err.println(e.getMessage());
}
}
private void println(BufferedOutputStream bos, String s) throws IOException {
String news = s + "\r\n";
byte[] array = news.getBytes();
for(int i=0; i<array.length; i++){
bos.write(array[i]);
}
return;
}
}
The problem occurs when I try and create the BufferedOutputStream; it gives me a "Socket is closed" error and I have no idea why.
Any help would be much appreciated.
Thanks!
This is the code that doesn't work:
class User extends Thread{
Socket client;
public User(Socket s){
client = s;
}
public void run(){
try{
System.out.println("Address: " + client.getInetAddress().toString());
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line;
Boolean first = true;
while(!((line = reader.readLine()).equals(""))){
if(first)
System.out.println(line.split(" ")[1]);
first = false;
}
reader.close();
BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
println(bos, "Hello World");
bos.close();
// Close socket
client.close();
}
catch(Exception e){
System.err.println(e.getMessage());
}
}
private void println(BufferedOutputStream bos, String s) throws IOException {
String news = s + "\r\n";
byte[] array = news.getBytes();
for(int i=0; i<array.length; i++){
bos.write(array[i]);
}
return;
}
}
This exception means that you closed the Socket.
Closing the input stream or the output stream of a socket closes the other two, and closing any FilterInput/OutputStream/Reader/Writer closes the stream/reader/writer it is filtering.
So far as I can tell, there is no problem with your code. It seems more likely that the operating system or browser is restricting access to your server in some way.
Socket client = ss.accept();
User user = new User(client);
user.start();
The accept call blocks waiting for connections. Your User thread is never created. You should create a new User class which creates a new thread for you and writes your messages to the server.
public class User extends Thread{
Socket client;
int port;
public User(int p) throws UnknownHostException, IOException{
port = p;
client = new Socket(InetAddress.getLocalHost(), 40000);
}
public void run(){
try{
BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
println(bos, "Hello World");
bos.close();
client.close();
}
catch(Exception e){
System.err.println(e.getMessage());
}
}
private void println(BufferedOutputStream bos, String s) throws IOException {
String news = s + "\r\n";
byte[] array = news.getBytes();
for(int i=0; i<array.length; i++){
bos.write(array[i]);
}
return;
}
public static void main(String[] args) throws UnknownHostException, IOException {
User user = new User(4000);
user.start();
}
}
The server should only listen to incoming connections.(and perhaps print it)
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
class HttpServer{
public static void main(String args[]){
ServerSocket ss = null;
int port = 40000;
try{
if(args.length > 0){
port = Integer.parseInt(args[0]);
}
ss = new ServerSocket(port);
while(true){
Socket client = ss.accept();
Scanner scanner = new Scanner(client.getInputStream());
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
}
}
catch(Exception e){
System.err.println(e.getMessage());
}
}
}
Run both of them as different java processes and you should see hello world at server side.
If you change your port you may have to change this on the client side too.

A simple java client server program

So I did this client server program in java for my college mini project. Note that this is just a small module of a big project I'm working on. I need a string to be sent from the client to the server. The server will return back the string as it is back to the client. (The code will be modified later such that the string is processed before sending back). The client will send a string whenever needed to the server. Thus it means it is compulsory for the server to be running for indefinite time.
The problem I face here is that my server works perfectly only for the first time when the client sends a string. If I run the client the second time with a different string, I get back the same string I sent to the server previously!
Here is my server program:
public class Server {
public static boolean x = true;
public static String reply;
public static void main(String a[]) throws Exception {
System.out.println("Entered server console..");
Socket echoSocket = null;
ServerSocket serverSocket = null;
PrintWriter out = null;
BufferedReader in = null;
System.out.println("Initializing Connection..");
boolean runFlag = true;
try {
serverSocket = new ServerSocket(77);
while (runFlag) {
echoSocket = serverSocket.accept();
out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
while (x) {
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
reply = in.readLine();
if (reply != null) {
x = false;
}
}
System.out.println("received: " + reply);
out.println(reply);
System.out.println("sent back: " + reply);
stdIn.close();
}
} catch (Exception e) {
System.out.println("Exception in starting server: " + e.getMessage());
} finally {
out.close();
in.close();
echoSocket.close();
}
}
}
Here is my Client program:
public class Client {
public static String reply,temp;
public static boolean x=true;
public Client()
{
temp="lala";
}
public Client(String t)
{
temp=t;
}
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("localhost", 77);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
temp="lala"; //this is the string to be sent
out.println(temp);
while (x) {
reply= in.readLine();
if(reply!=null)
{
x=false;
}
}
System.out.println("reply: "+reply);
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
Can anyone help me find what the problem here is?
while (x) {
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
reply = in.readLine();
if (reply != null) {
x = false;
}
}
Your server enters this loop the first time a client connects, and it sets the reply String to some input from the client. However, it never enters this loop again, as x's value never changes back to true.
When you accept a request, the x will be set false and never become true.
Please initial the x when you enter the loop.
What's more,if you use a socket between client and server, please move the
echoSocket = serverSocket.accept();
out of the first loop.And you can use echoSocket to communicate.Then you will
keep the long connection.

Categories