Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.*;
- import java.io.*;
- import java.net.*;
- import java.util.HashMap;
- import java.util.Map;
- public class SocketServer {
- private static final Map<String, String> USER_PASSWORD_MAP = new HashMap<String, String>();
- private static final String USER_PROMPT = "enter username";
- private static final String PASSWORD_PROMPT = "enter password";
- private static final String WELCOME_MESSAGE = "Welcome to My Server";
- private static final int MAX_LOGIN_FAILS = 3;
- static {
- USER_PASSWORD_MAP.put("1", "2");
- USER_PASSWORD_MAP.put("user", "password");
- }
- private static boolean authenticateUser(BufferedReader in, PrintWriter out) throws IOException {
- for (int i = 0; i < MAX_LOGIN_FAILS; i++) {
- out.println(USER_PROMPT);
- String userName = in.readLine();
- out.println(PASSWORD_PROMPT);
- String password = in.readLine();
- if (USER_PASSWORD_MAP.containsKey(userName) == true) {
- String userPassword = USER_PASSWORD_MAP.get(userName);
- if (password.equals(userPassword) == true) {
- return true;
- }
- }
- }
- return false;
- }
- private static void handshakeWithClient(BufferedReader in, PrintWriter out) throws IOException {
- System.out.println("Sending string: '" + WELCOME_MESSAGE);
- out.println(WELCOME_MESSAGE);
- String clientHostname = in.readLine();
- System.out.println("Received Client Hostname: " + clientHostname);
- String clientIPAddress = in.readLine();
- System.out.println("Received Client IPAddress: " + clientIPAddress);
- }
- public static void main(String args[]) throws IOException {
- String message;
- ServerSocket srvr = null;
- Socket skt = null;
- PrintWriter out = null;
- try {
- //Detecting the localhost's ip address
- InetAddress localaddr = InetAddress.getLocalHost();
- System.out.println("Local IP Address : " + localaddr);
- System.out.println("Local hostname : " + localaddr.getHostAddress());
- // Creating a server socket for connection
- srvr = new ServerSocket(1234);
- System.out.println("Waiting for connection on " + localaddr);
- // Accept incoming connection
- skt = srvr.accept();
- System.out.print("Server has connected!\n"); // get Input and Output streams
- out = new PrintWriter(skt.getOutputStream(), true);
- out.flush();
- BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
- //sends welcome message
- handshakeWithClient(in, out);
- boolean authenticated = authenticateUser(in, out);
- if (authenticated == true) {
- do {
- out.println("enter a message");
- message = in.readLine();
- System.out.println("client>" + message);
- } while (!message.equals("bye"));
- } else {
- System.out.println("Authentication Failed!");
- }
- out.println("server>Server closing");
- System.out.println("server>Server closing");
- } catch (BindException e) {
- e.printStackTrace();
- System.out.print("A server is already running on the same port.");
- } catch (SocketException e) {
- e.printStackTrace();
- System.out.print("Client has disconnected rudely.");
- } catch (Exception e) {
- e.printStackTrace();
- System.out.print(e);
- } finally {
- if (out != null) {
- out.close();
- }
- if (skt != null) {
- skt.close();
- }
- if (srvr != null) {
- srvr.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement