Java套接字并维护一个持续的连接

时间:2018-05-02 01:44:52

标签: java sockets

我目前在维护连接方面遇到问题(我觉得这个措辞不合适,并且#34;维持连接")在我的客户端和服务器之间(不是连接,而是留在同样的方法)。这是我的代码:(在之后更多地解释问题)

客户端:

try{ 
            Scanner scan = new Scanner(System.in);
            System.out.print("Type *R for registration, " );
            String line = scan.nextLine();

            int port = 13333;
            Socket mySock = new Socket("127.0.0.1", port);
            Scanner in = new Scanner(
                    new InputStreamReader(mySock.getInputStream()));
            PrintStream out = new PrintStream( mySock.getOutputStream());
            out.println(line);

            while(in.hasNextLine()){    
                System.out.println(in.nextLine());
                String s = scan.nextLine();
                out.println(s);
            }

            mySock.close();

        }catch (Exception e){
            e.printStackTrace();
        }

服务器:

public static void main(String[] args) throws Exception {

        int port = 13333;
        ServerSocket socket = new ServerSocket(port);

        while(true){
            Socket connection = socket.accept();

            User request = new User(connection);

            Thread thread = new Thread(request);

            thread.start();

        }

Serverhelper(用户类):

final class User implements Runnable {
    private static FileWriter fw;
    private static boolean flag = false;
    private  Socket socket;

    public User (Socket socket) throws Exception{
        this.socket = socket;

    }
    private Scanner scan;
    private PrintStream out;

    public void run() {
        try{
            UserInput();
        }catch (Exception e){
            System.out.println(e);
        }
    }

    private  void UserInput() throws Exception{
        scan = new Scanner(
                new InputStreamReader(socket.getInputStream()));
        out = new PrintStream(socket.getOutputStream());

        String input = scan.next();

        if(input.equals("*R")){
            Register();
    }

    }


    private void Register() throws FileNotFoundException{
        try{            
            scan = new Scanner(new InputStreamReader(socket.getInputStream()));
            out = new PrintStream(socket.getOutputStream());
            fw = new FileWriter("info.txt",true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter txtfile = new PrintWriter(bw);  



            //Scanner scan = new Scanner(System.in);
            out.println("Enter a username: ");
            String username = scan.nextLine();
            out.println("Enter a password: ");
            String password = scan.nextLine();
            txtfile.println(username + " " + password);
            txtfile.flush();
            txtfile.close();


        }catch(IOException e){
            e.printStackTrace();
        }
    }

**这就是发生的事情。最初我启动我的服务器,然后启动客户端并输入* R进入控制台,然后返回字符串"输入用户名:"。所以我的服务器和客户端正在工作,但在此之后我失去了因为我然后继续输入用户名发送到服务器。我的userInput方法在用户类中没有获取它,因为它只扫描* R而我没有留在用户类的register方法中。有没有办法保持我的位置(留在注册方法)?这将是理想的但我迷失了。

0 个答案:

没有答案