Java 通过套接字将多个变量从客户端发送到服务器
我最近学习了Java中的套接字,以及通过套接字在客户端和服务器之间来回发送信息。 我想要实现的是将“用户名”和“密码”从客户端发送到服务器,然后根据数据库中的数据检查这些变量 将这两个独立值的值发送到服务器以便在服务器端验证的最佳方法是什么 客户端Java 通过套接字将多个变量从客户端发送到服务器,java,sockets,Java,Sockets,我最近学习了Java中的套接字,以及通过套接字在客户端和服务器之间来回发送信息。 我想要实现的是将“用户名”和“密码”从客户端发送到服务器,然后根据数据库中的数据检查这些变量 将这两个独立值的值发送到服务器以便在服务器端验证的最佳方法是什么 客户端 clientSocket = new Socket("192.168.56.1", 7777); in = new BufferedReader(new InputStreamReader(clientSocket.getInput
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();
如果你一边写两行,你显然应该读另一边的两行:
String name = is.readLine();
String password = is.readLine();
您可以拥有一个以用户名和密码作为属性的对象 序列化对象,然后发送它
class User implements Serializable {
String userName ;
String Password ;
...
}
现在使用ObjectInput/OutputStream读取/写入对象
有关更多信息,请参阅本文档-您可以拥有一个将用户名和密码作为其属性的对象 序列化对象,然后发送它
class User implements Serializable {
String userName ;
String Password ;
...
}
现在使用ObjectInput/OutputStream读取/写入对象
有关更多信息,请参阅本节-如果使用ObjectOutputStream,则可以写入整个对象
这些对象必须是可序列化的如果使用ObjectOutputStream,则可以写入整个对象 这些对象必须是可序列化的