Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ie.gmit.sw.server;
- import java.io.*;
- import java.net.*;
- import java.util.Scanner;
- public class LoginRequester {
- Socket requestSocket;
- ObjectOutputStream out;
- ObjectInputStream in;
- String message;
- Scanner userInput;
- public LoginRequester(){
- userInput = new Scanner(System.in);
- }
- public void runLogin(){
- int cont = 0;
- try {
- requestSocket = new Socket ("127.0.0.1", 2010);
- System.out.println("Connected to localhost in port 2004");
- out = new ObjectOutputStream(requestSocket.getOutputStream());
- out.flush();
- in = new ObjectInputStream(requestSocket.getInputStream());
- do{
- message = (String)in.readObject();
- System.out.println(message);
- message = userInput.next();
- sendMessage(message);
- message = (String)in.readObject();
- System.out.println(message);
- message = userInput.next();
- sendMessage(message);
- cont = 1;
- }while(cont != 1);
- } catch (IOException e) {
- System.out.println("FAILED TO CONNECT");
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- System.out.println("Data recieved in unknown format");
- e.printStackTrace();
- }
- }
- void sendMessage(String msg) {
- try{
- out.writeObject(msg);
- out.flush();
- System.out.println("Client > " + msg);
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- LoginRequester client = new LoginRequester();
- client.runLogin();
- }
- }
- package ie.gmit.sw.server;
- import java.io.*;
- import java.net.*;
- import java.util.Scanner;
- public class LoginProvide {
- ServerSocket providerSocket;
- Socket connection = null;
- ObjectOutputStream out;
- ObjectInputStream in;
- Scanner input;
- String message;
- String userInput;
- String name;
- String address;
- String ban;
- String user;
- String pass;
- int choice;
- int result;
- LoginProvide(){
- input = new Scanner(System.in);
- }
- void login(){
- int cont = 0;
- try {
- providerSocket = new ServerSocket(2010, 10);
- System.out.println("Waiting for Connection");
- connection = providerSocket.accept();
- System.out.println("Connection recieved from " + connection.getInetAddress().getHostAddress());
- out = new ObjectOutputStream(connection.getOutputStream());
- out.flush();
- in = new ObjectInputStream(connection.getInputStream());
- do{
- sendMessage("Please Enter your Username ");
- try {
- message = (String)in.readObject();
- user = new String (message);
- sendMessage("Please Enter your Password ");
- message = (String)in.readObject();
- pass = new String (message);
- cont = 1;
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }while(cont != 1);
- } catch (IOException e) {
- e.printStackTrace();
- }
- finally{
- try {
- in.close();
- out.close();
- providerSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- private void sendMessage(String msg) {
- try {
- out.writeObject(msg);
- out.flush();
- System.out.println("Server > " + msg);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- LoginProvide server = new LoginProvide();
- while(true){
- server.login();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement