Click here to Skip to main content
15,436,489 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey guys, I'm trying to create an application that is similar to the game Who wants to be a millionaire with multiplayer.

--SERVER--
Java
public class ServerThread extends Thread {
    Socket socket = null;
    int num;
    public ServerThread(Socket socket){
        this.socket = socket;
    }
    public void run(){
        try {
            switch(num){
                case 1:
                    DataInputStream dataInputStream= new DataInputStream(socket.getInputStream()); 
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                    String id = dataInputStream.readUTF();
                    objectOutputStream.writeObject(login(id));
                    socket.close();
                    break;
                case 2:
                    ObjectInputStream objectInputStream= new ObjectInputStream(socket.getInputStream()); 
                    DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                    register((Account) objectInputStream.readObject());
                    dataOutputStream.writeUTF("Account Added.");
                    socket.close();
                    break;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    
    
    private Account login(String id){
        AccountDao accountDao = new AccountDaoImpl();
        Account login = accountDao.getAccount(id);
        return login;
    }
    
    private void register(Account account){
        AccountDao accountDao = new AccountDaoImpl();
        accountDao.addAccount(account);
    }

--Client--
Java
public class Client {
    
    private Account account;

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
    
    public Boolean connect(String studNo){
        Boolean flag= false;
        try{
            Socket socket = new Socket("localhost",7723);
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            dataOutputStream.writeUTF(studNo);
            account = (Account)objectInputStream.readObject();
            System.out.println(account.toString());
            socket.close();
            
            if(account == null){
                flag = false;
            }else{
                flag = true;
            }
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        return flag;
    }
    
    public void register(Account regAcc){
        try{
            Socket socket = new Socket("localhost",7723);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            objectOutputStream.writeObject(regAcc);
            account = (Account)objectInputStream.readObject();
            System.out.println(account.toString());
            socket.close();
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
    }

I'm having a hard time thinking the approach I should do when there are already many functions on the Client. How will the Server class know which function of the Client class is to be used?
Posted
Updated 21-Mar-15 22:27pm
v3
Comments
Sergey Alexandrovich Kryukov 22-Mar-15 14:05pm     CRLF
Not clear; what function? The code fragment with "case 1" and "case 2" does not allow me to consider your code seriously. Do you want to implement some kind of remote method calls with callbacks based on row sockets? Then short answer would be: the way you design it. There is no a predefined way to uniquely identify a function. Everything should be serialized. In case of direct call, the serialized form could be some function name, but this is not the case if this is a client-side function. However, I don't know what do you mean by that. —SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900