java Simple Server Client程序

时间:2014-08-07 04:18:42

标签: java sockets network-programming

我正在尝试构建一个简单的服务器客户端程序,我试图找到一种方法,如果服务器关闭,或者如果服务器启动并失去连接,则如何提示客户端

问题:如何提示客户端因为服务器关闭或断开连接而断开连接

服务器

public class Server{

private static Socket socket;

public static void main(String[] args)
{
    try
    {
        int port = 25000;
        ServerSocket serverSocket = new ServerSocket(port);
        //Server is running always. This is done using this while(true) loop
        while(true)
        {
            //Reading the message from the client
            socket = serverSocket.accept();
            System.out.println("Client has connected!");
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String number = br.readLine();
            System.out.println("Message received from client is "+number);

            //Multiplying the number by 2 and forming the return message
            String returnMessage;
            try
            {
                int numberInIntFormat = Integer.parseInt(number);
                int returnValue = numberInIntFormat*2;
                returnMessage = String.valueOf(returnValue) + "\n";
            }
            catch(NumberFormatException e)
            {
                //Input was not a number. Sending proper message back to client.
                returnMessage = "Please send a proper number\n";
            }

            //Sending the response back to the client.
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(returnMessage);
            System.out.println("Message sent to the client is "+returnMessage);
            bw.flush();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

客户端

public class Client{

private static Socket socket;

public static void main(String args[])
{
    Scanner input=new Scanner(System.in);
    while(true)
    {
        try
        {
            String host = "localhost";
            int port = 25000;
            InetAddress address = InetAddress.getByName(host);
            socket = new Socket(address, port);
            //System.out.println("You're now connected to the Server"); /*this should only print once */
            //Send the message to the server
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);

            String number;
            number=input.next();
            String sendMessage = number + "\n";
            bw.write(sendMessage);
            bw.flush();
            System.out.println("Message sent to the server : "+sendMessage);

            //Get the return message from the server
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String message = br.readLine();
            System.out.println("Message received from the server : " +message);
        }
        catch (IOException exception)
        {
            //System.out.println("Server is still offline");/*This should only print once*/
        }

1 个答案:

答案 0 :(得分:3)

How can I prompt the client that he's disconnected because the Server shuts down or loses connection?

您可以使用catch block在Client类中提示客户端,该客户端将在IOException发生时执行

} catch (ConnectException e) { //When the connection is refused upon connecting to the server
      //promt the user here
      System.out.println("Connection refused");
      break; //to quit the infinite loop
} catch (IOException e) { //when connection drops, server closed, loses connection
     //promt the user here
     System.out.println("Disconnected from server");
     break; //to quit the infinite loop
}