Hey guys,
I can't understand why am I not getting anything from the server, it is supposed to return "Hello" to the client ... I have this code implemented for a game I made during the summer and it works perfectly(of course this is just part of the code), but just started a new project and got stucked on it already and I can't find a reasonable explanation for it .. Here's the code ..
Server
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class AuthServer extends Thread{
private ServerSocket serverSocket;
private ArrayList<ClientThread> alThreads = new ArrayList<ClientThread>();
String input1;
public AuthServer(int port)throws IOException{
serverSocket = new ServerSocket(port);
}
public static void main(String[] args){
int port = 9001;
try{
Thread t = new AuthServer(port);
t.start();
}catch(IOException e){
e.printStackTrace();
}
}
public void run(){
while(true){
try{
System.out.println("Starting to run");
Socket server = serverSocket.accept();
ClientThread ct = new ClientThread(server);
alThreads.add(ct);
ct.start();
} catch(IOException e){
e.printStackTrace();
}
}
}
public class ClientThread extends Thread{
Socket clientSocket;
PrintWriter out;
public ClientThread(Socket s){
this.clientSocket = s;
}
public void run(){
try{
out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("waiting");
String input = in.readLine();
System.out.println(input);
input1 = in.readLine();
for(ClientThread t:alThreads){
t.print();
}
} catch(IOException e){
e.printStackTrace();
}
}
public void print() throws IOException{
this.out.print(input1+ "\n");
this.out.flush();
}
}
}
Client
package client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
String defaultHost;
int defaultPort;
String input;
public Client (String host, int port){
defaultHost = host;
defaultPort = port;
}
public void run() throws IOException{
Socket clientSocket = new Socket(defaultHost, defaultPort);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
out.print("Client connected"+"\n");
out.print("Hello");
out.flush();
System.out.println("Connected now!");
input = inFromServer.readLine();
System.out.println(input);
}
public static void main(String[] args) throws IOException{
Client c = new Client("localhost", 9001);
c.run();
}
}
What doesn't seem to be working is the print method in the server, the Client gets stucked waiting for input from the server, any ideas?