-1

I have problem with my client-server. When i run server, after client, server write that client is connected with server but i can write nothing in console. Anyone can check where is problem? Because i'm new in java. I try also Scanner but it doesn't work too.

server:

package ts_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;  

public class Server  
{  
    private static final int PORT = 50000;  
    static boolean flaga = true;  

    private static ServerSocket serverSocket;  
    private static Socket clientSocket;  

    public static void main(String[] args) throws IOException  
    {  
        serverSocket = null;  
        try  
        {  
            serverSocket = new ServerSocket(PORT);  
        }  
        catch(IOException e)  
        {  
            System.err.println("Could not listen on port: "+PORT);  
            System.exit(1);  
        }  

        System.out.print("Waiting for connection...");  

        Thread t = new Thread(new Runnable()  
        {  
            public void run()  
            {  
                try  
                {  
                    while(flaga)  
                    {  
                        System.out.print(".");  
                        Thread.sleep(1000);  
                    }  
                }  
                catch(InterruptedException ie)  
                {  
                    //  
                }  

                System.out.println("\nClient connected on port "+PORT);  
            }  
        });  
        t.start();  

        clientSocket = null;  
        try  
        {  
            clientSocket = serverSocket.accept();  
            flaga = false;  
        }  
        catch(IOException e)  
        {  
            System.err.println("Accept failed.");  
            t.interrupt();  
            System.exit(1);  
        }  

        final PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true);  
        final BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));  

        t = new Thread(new Runnable()  
        {  
            public void run()  
            {  
                try  
                {  
                    Thread.sleep(5000);  

                    while(true)  
                    {  
                        out.println("Ping");  
                        System.out.println(System.currentTimeMillis()+" Ping sent");  

                        String input = in.readLine();  

                        if(input.equals("Pong"))  
                        {  
                            System.out.println(System.currentTimeMillis()+" Pong received");  
                        }  
                        else  
                        {  
                            System.out.println(System.currentTimeMillis()+" Wrong answer");  

                            out.close();  
                            in.close();  
                            clientSocket.close();  
                            serverSocket.close();  
                            break;  
                        }  


                        Thread.sleep(5000);  
                    }  
                }  
                catch(Exception e)  
                {  
                    System.err.println(System.currentTimeMillis()+" Unexpected Error");  
                }  
            }  
        });  
        t.start();  
    }  
} 

client:

package ts_client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client
{
    private static final int PORT = 50000;
    private static final String HOST = "localhost";

    public static void main(String[] args) throws IOException
    {
        Socket socket = null;

        try
        {
            socket = new Socket(HOST, PORT);
        }
        catch(Exception e)
        {
            System.err.println("Could not connect to "+HOST+":"+PORT);
            System.exit(1);
        }

        final PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
        final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        Thread t = new Thread(new Runnable()
        {
            public void run()
            {
                long start = System.currentTimeMillis();

                while (true)
                {
                    try
                    {
                        String input = in.readLine();

                        if (input != null)
                        {
                            System.out.println(System.currentTimeMillis() + " Server: " + input);
                        }

                        if (input.equals("Ping"))
                        {
                            if(System.currentTimeMillis()-start>30000)
                            {
                                out.println("Pon g");
                                System.out.println(System.currentTimeMillis() + " Client: Pon g");
                                break;
                            }

                            out.println("Pong");
                            System.out.println(System.currentTimeMillis() + " Client: Pong");
                        }
                    }
                    catch (IOException ioe)
                    {
                        //
                    }
                }
            }
        });
        t.start();

        out.close();
        in.close();
        socket.close();
    }
}
2
  • What exactly is the problem? What do you expect this code to do and what does it do instead?
    – JB Nizet
    May 17, 2014 at 22:14
  • You are ignoring many exceptions -- don't do that. It's kind of like riding a motorcycle with blinders on -- not a very safe thing to do. May 17, 2014 at 22:15

1 Answer 1

2

Don't close the streams and socket until Client is running.

out.close();
in.close();
socket.close();
3
  • use volatile for static boolean flaga = true; that is accessed in multi threading.
    – Braj
    May 17, 2014 at 22:21
  • Not at all. You are most Welcome
    – Braj
    May 17, 2014 at 22:23
  • @HovercraftFullOfEels Thanks a lot. My target is achieved for the day. Time to sleep. Good night.
    – Braj
    May 17, 2014 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.