0

I am trying to make some sort of login system, but the server and client wont talk to each other. I am not quite sure why they wont talk to each other, but any help is appreciated.

P.S The port is correctly set up on my router.

Client

public class Clients implements Runnable
{
String ip = "localhost";
int port = 25565;
Socket client;
static Thread thread;
boolean setup = false;
BufferedReader br;
PrintWriter pw;
public static void main(String[] args)
{
    thread = new Thread(new Clients());
    thread.start();
}
public void run()
{
    while(!setup)
    {
        try {
            client = new Socket(ip,port);
            setup = true;
        } catch (IOException e) {
            setup = false;
        }
    }
    try {
        br = new BufferedReader(new InputStreamReader(client.getInputStream()));
        pw = new PrintWriter(client.getOutputStream(),true);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    pw.flush();
    pw.write("client");
    while(true);
}
}

Server

public class Server implements Runnable
{
int port = 25565;
String input;
ServerSocket server;
Socket clients;
BufferedReader br;
PrintWriter pw;
boolean setup = false;
static Thread thread;
public static void main(String[] args)
{
    thread = new Thread(new Server());
    thread.start();
}
public void run()
{
    try {
        server = new ServerSocket(port);
        clients = server.accept();
        br = new BufferedReader(new InputStreamReader(clients.getInputStream()));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        System.out.println("getting input");
        while((input = br.readLine()) != null)
        {
            System.out.println(input);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
Mikhail
  • 31
  • 6
  • you mean the server was not able to read "client"? – Rod_Algonquin Aug 13 '14 at 02:08
  • they seem to connect fine neither of them give me an exception but when I send something through the stream the "server" wont read it – Mikhail Aug 13 '14 at 02:11
  • Theres a lot of questionable code here. First, `while(true)` doesn't do anything; you didnt give it a body. Second, you only initialized the inputstream on the server, but the input AND output stream on the client. I'm guessing that the client is waiting to initialize the stream that you didn't initialize on the server. – Vince Aug 13 '14 at 02:17
  • Just wondering why you need a router if your connecting to "localhost"? Also are you running the server application on your server and the client application on your client computer? – Jonny Henly Aug 13 '14 at 02:17
  • the "localhost" is so i can change it and give it to my friend to test it later and the while true is just to hold the program and I cant get the server to read the inputstream in the first place so why would it matter if it doesnt have an output stream? – Mikhail Aug 13 '14 at 02:21
  • Vince I tested your idea and it didnt work. Just to let you know. – Mikhail Aug 13 '14 at 02:23

2 Answers2

0

I see two problems. The first is that pw.flush should be invoked after pw.write. The second is that the server is waiting on readLine(), which will only return when it encounters a line ending. You should change Clients to invoke pw.write("clients\n"), adding the newline.

laz
  • 28,320
  • 5
  • 53
  • 50
0

You should first do write and then flush

pw.write("client\n");
pw.flush();

Also place \n in the line that you are writing since in the client you are doing br.readline(). So it will wait till a new line is available.

rdp
  • 2,072
  • 4
  • 31
  • 55