繁体   English   中英

通过套接字从客户端向服务器发送多个变量

[英]Sending more than one variables from client to server through socket

我最近了解了Java中的套接字,并通过套接字从客户端到服务器来回发送信息。 我要实现的是从客户端向服务器发送“用户名”和“密码”,然后根据数据库中的数据检查这些变量。

将这两个单独值的值发送到服务器以便可以在服务器端进行验证的最佳方法是什么?

客户端

clientSocket = new Socket("192.168.56.1", 7777);

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


    //starting the thread
    while(runner == null)
    {
        runner = new Thread(this);

        runner.start();
    }
}
public void run()
{
    String userNameAdminLogin;
    String passwordAdminLogin;
    while(runner == Thread.currentThread())
    {
        userNameAdminLogin = txtUserName.getText();
        passwordAdminLogin = txtPassword.getText();

        out.println(userNameAdminLogin);
        out.println(passwordAdminLogin);
    }

断面

while(listening)
            {
                clientSocket = ServerSoc.accept();


                in = new DataInputStream(clientSocket.getInputStream());

                BufferedReader is = new BufferedReader(new InputStreamReader(in));

                os = new PrintStream(clientSocket.getOutputStream());

                //How can I save the two seperate cases of data in variables on server side?
                System.out.println(is.readLine());

            }

亲切的问候

阿里安

如果您要在一侧写两行,显然您应该在另一侧读两行:

String name = is.readLine();
String password = is.readLine();

您可以拥有一个以用户名和密码作为属性的对象。

序列化对象,然后发送它。

class User implements Serializable {  
String userName ;
String Password ;
... 
}  

现在使用ObjectInput / OutputStream读取/写入对象

请参阅此以获取更多信息-http: //www.coderanch.com/t/205325/sockets/java/send-any-java-Object-through

如果使用ObjectOutputStream,则可以编写整个对象。

这些对象必须可序列化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM