This week I Started to work with java sockets, and the first idea was to create a login form, and then some forms that will receive data from a DB.
The login is working, but it only checks once because the server closes. I think i need to create a while statement so the server is always listening, but I actually don't know where and how to put it.
This is my code so far: Server:
package pkgnew.sockets;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
ServerSocket serversocket;
Socket client;
BufferedReader input;
PrintWriter output;
public void start() throws IOException{
serversocket = new ServerSocket(9090);
System.out.println("Connection Starting on port:" + serversocket.getLocalPort() );
//make connection to client on port specified
//accept connection from client
client = serversocket.accept();
System.out.println("Waiting for connection from client");
try {
logInfo();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void logInfo() throws Exception{
//open buffered reader for reading data from client
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
String username = input.readLine();
System.out.println("username" + username);
String password = input.readLine();
System.out.println("password" + password);
//open printwriter for writing data to client
output = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
if(username.equals("User") &&password.equals("Password")){
output.println("Welcome, " + username);
}else{
output.println("Login Failed");
}
output.flush();
output.close();
}
public static void main(String[] args){
Server server = new Server();
try {
server.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Client:
package pkgnew.sockets;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
public class Client {
Socket socket;
BufferedReader read;
PrintWriter output;
public void startClient() throws UnknownHostException, IOException{
//Create socket connection
socket = new Socket("localhos", 9090);
//create printwriter for sending login to server
output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
//prompt for user name
String username = JOptionPane.showInputDialog(null, "Enter User Name:");
//send user name to server
output.println(username);
//prompt for password
String password = JOptionPane.showInputDialog(null, "Enter Password");
//send password to server
output.println(password);
output.flush();
//create Buffered reader for reading response from server
read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//read response from server
String response = read.readLine();
System.out.println("This is the response: " + response);
//display response
JOptionPane.showMessageDialog(null, response);
}
public static void main(String args[]){
Client client = new Client();
try {
client.startClient();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Tank you in advance.
use something like this,
while(true){
Socket clientSocket = serverSocket.accept();
new Thread(new Runnable() {
@Override
public void run() {
login();
}
});
}
// somewhere down , kind of shutdownhook to your server serverSocket.close();
well this code is just for your understanding purpose , actually in realtime server will use threadpools to manage number of request at time processed by server and maximum request can be queued ,maximum number of connections can be opened with ssl/tls security and so many factors ....
as usual for more understanding you can go through http://tomcat.apache.org/tomcat-9.0-doc/ best place to learn how https server works :) enjoy