Okay, so Like the title says, I'm stumped on how to send, specifically, strings through input and output streams. I'm making a basic log in server and want to be able to hit "log in" and have the log in credentials to save on the server side. Here's what I have.
This is the try block inside the action event for my log in button:
try {
Socket socket = Client.s;
OutputStream dOut = Client.s.getOutputStream();
PrintWriter socketOut = new PrintWriter(dOut);
username = jTextField1.getText();
password = jPasswordField1.getText();
//System.out.println("Username: " + username);
//System.out.println("Password: " + password);
socketOut.println(username);
socketOut.flush();
socketOut.println(password);
socketOut.flush();
socketOut.println("Click");
socketOut.flush();
} catch (Exception e){
e.printStackTrace();
}
And here's my code that's server sided. I attempted to receive and store the data but it isn't working and I'm not too sure why. I also have a couple of useless loops that I will eventually remove(ignore those):
BufferedReader socketIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = socketIn.readLine();
while (buttonClicked == false){
int done = 0;
String messageType = socketIn.toString();
while(done == 0) {
switch(messageType)
{
case "username": // Type A
username = socketIn.toString();
System.out.println("username: " + username);
break;
case "password": // Type B
password = socketIn.toString();
System.out.println("Password: " + password);
break;
case "Click":
buttonClicked = true;
break;
}
Please note that I know that I'm missing closing brackets, I only copied part of my code.
BufferedReader socketIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = socketIn.readLine();
while (buttonClicked == false){
int done = 0;
String messageType = socketIn.toString();
while(done == 0) {
switch(messageType)
{
case "username": // Type A
username = socketIn.toString();
System.out.println("username: " + username);
break;
case "password": // Type B
password = socketIn.toString();
System.out.println("Password: " + password);
break;
case "Click":
buttonClicked = true;
break;
}
You take the .toString()
of the stream ( socketIn.toString();
), you should use str
.
BufferedReader socketIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = socketIn.readLine();
while (buttonClicked == false){
int done = 0;
while(done == 0) {
switch(str)
{
case "username": // Type A
username = str;
System.out.println("username: " + username);
break;
case "password": // Type B
password = str;
System.out.println("Password: " + password);
break;
case "Click":
buttonClicked = true;
break;
}
But, anyway i think it's not what you want... i mean when him pass "password", password will be password.
Maybe you want
BufferedReader socketIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = socketIn.readLine();
while (buttonClicked == false){
int done = 0;
while(done == 0) {
switch(str)
{
case "username": // Type A
username = socketIn.readLine();
System.out.println("username: " + username);
break;
case "password": // Type B
password = socketIn.readLine();
System.out.println("Password: " + password);
break;
case "Click":
buttonClicked = true;
break;
}
To read a new line from input.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.