java socket编程对话

时间:2013-12-17 10:28:46

标签: java sockets client-server

大家好我想在服务器和客户端之间进行java套接字对话,它与第一条消息一起工作然后对话停止,在我的情况下我想从客户端发送hi消息并从服务器接收hi消息然后服务器必须还发送另一个按摩“输入一个数字来检查”并等待输入

我的客户代码是:

public class ClientSide {
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception{
        ClientSide clientSideInstance = new ClientSide();
        clientSideInstance.run();
    }

    /**
     * @throws Exception
     */
    public void run() throws Exception{

        Socket soc = new Socket("localhost",4321);
        PrintStream printStream = new PrintStream(soc.getOutputStream());
        printStream.println("hi");
        InputStreamReader Inputreader = new InputStreamReader(soc.getInputStream());
        BufferedReader bufferedReader = new BufferedReader(Inputreader);
        String receivedMsg =  bufferedReader.readLine();
        System.out.println(receivedMsg);
    }
}

我的服务器代码是:

public class ServerSide {
    private PrintStream printStream;

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception{
        ServerSide serverSideInstance = new ServerSide();
        serverSideInstance.run();
    }

    /**
     * @throws Exception
     */
    public void run() throws Exception{

        ServerSocket socket = new ServerSocket(4321);
        Socket soc = socket.accept();
        InputStreamReader Inputreader = new InputStreamReader(soc.getInputStream());
        BufferedReader bufferedReader = new BufferedReader(Inputreader);

        String receivedMsg =  bufferedReader.readLine();
        System.out.println(receivedMsg);

        // parsing client massage 
        if(receivedMsg.trim().equalsIgnoreCase("hi")){

            printStream = new PrintStream(soc.getOutputStream());
            printStream.println("hi");

            printStream.println("enter number to check ");

            InputStreamReader InputreaderForPrimeNumber = new InputStreamReader(soc.getInputStream());
            BufferedReader bufferedReaderForPrimeNumber = new BufferedReader(InputreaderForPrimeNumber);
            String number = bufferedReaderForPrimeNumber.readLine();

            if(isPrime(Integer.parseInt(number))){
                printStream.println("yes - it is Prime number :)");
            }else{
                printStream.println("No - it is not a Prime number :(");
            }

        }
    }

    private boolean isPrime(int n) {
        for(int i=2;i<n;i++) {
            if(n%i==0)
                return false;
        }
        return true;
    }
}

任何人都可以提供任何帮助吗?

1 个答案:

答案 0 :(得分:2)

你有一些错误,实施工作确实没有完成 这是你的代码修复。另外,请注意1不是素数AFAIK。
当客户端发送“再见”而不是号码时,会话结束

服务器:

        import java.io.BufferedReader;
        import java.io.InputStreamReader;
        import java.io.PrintStream;
        import java.net.ServerSocket;
        import java.net.Socket;

        public class ServerSide {
            private PrintStream printStream;

            /**
             * @param args
             * @throws Exception
             */
            public static void main(String[] args) throws Exception{
                ServerSide serverSideInstance = new ServerSide();
                serverSideInstance.run();
            }

            /**
             * @throws Exception
             */
            public void run() throws Exception{

                ServerSocket socket = new ServerSocket(4321);
                Socket soc = socket.accept();
                InputStreamReader Inputreader = new InputStreamReader(soc.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(Inputreader);

                printStream = new PrintStream(soc.getOutputStream());

                while (true){
                    String receivedMsg =  bufferedReader.readLine();
                    System.out.println(receivedMsg);

                    // parsing client massage 
                    if(receivedMsg.trim().equalsIgnoreCase("hi")){
                        printStream.println("hi");
                        printStream.println("enter number to check: ");
                    }else if (receivedMsg.trim().equalsIgnoreCase("bye")){
                        // printStream.println("bye");
                        break;
                    }else{
                        if(isPrime(Integer.parseInt(receivedMsg))){
                            printStream.println("Yes - it is a prime number :)");
                        }else{
                            printStream.println("No - it is not a prime number :(");
                        }
                        printStream.println("enter number to check: ");
                    }
                }
            }

            private boolean isPrime(int n) {
                if (n==1) return false;

                for(int i=2;i<n;i++) {
                    if(n%i==0){
                        return false;
                    }
                }
                return true;
            }

        }



客户端:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.Socket;

    public class ClientSide {
        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception{
            ClientSide clientSideInstance = new ClientSide();
            clientSideInstance.run();
        }

        /**
         * @throws Exception
         */
        public void run() throws Exception{
            BufferedReader brr = new BufferedReader(new InputStreamReader(System.in));
            Socket soc = new Socket("localhost", 4321);
            PrintStream printStream = new PrintStream(soc.getOutputStream());
            printStream.println("hi");

            InputStreamReader Inputreader = new InputStreamReader(soc.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(Inputreader);
            String receivedMsg =  bufferedReader.readLine().trim();
            System.out.println(receivedMsg);

            while (true){
                receivedMsg =  bufferedReader.readLine();
                System.out.println(receivedMsg);

                if (receivedMsg.equalsIgnoreCase("enter number to check: ")){
                    String userMessage = brr.readLine().trim(); // read from stdin
                    printStream.println(userMessage); // send message/number to server
                    if (userMessage.equalsIgnoreCase("bye")){
                        break;
                    }
                    receivedMsg =  bufferedReader.readLine().trim();
                    System.out.println(receivedMsg);
                }
            }
        }
    }