Reputation: 9043
I recently learned about sockets in Java and sending information back and forth from client to server through sockets. What I want to achieve is sending 'username' and 'password' from client to server and then have these variables checked against the data in a database.
What would the best way be of sending the values of these two separate values to the server so that it can be verified server side?
client side
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);
}
sever side
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());
}
Kind regards
Arian
Upvotes: 1
Views: 7843
Reputation: 691715
If you're writing two lines on one side, you should obviously read two lines on the other side:
String name = is.readLine();
String password = is.readLine();
Upvotes: 2
Reputation: 4180
if you use an ObjectOutputStream you can write entire objects.
Those objects have to be serializable
Upvotes: 2
Reputation: 951
You can have an object which has got username and password as its attributes.
Serialize the object and then send it .
class User implements Serializable {
String userName ;
String Password ;
...
}
Now use ObjectInput/OutputStream to read / write objects
Refer to this for more info - http://www.coderanch.com/t/205325/sockets/java/send-any-java-Object-through
Upvotes: 2