How to exchange messages with client and server java Socket?

1

I am not able to do the exchange of server messages for the client java Socket.

The server is ready, but the client can not do according to the server. What is the class that sends message to the server?

Here are the questions to answer:

1) Sends message to the Server. The message should contain the client's name.

2) You receive a message from the Customer.

3) Sends a message to the Client, asking the customer to send a randomly generated verification code.

4) Receive message from Server.

5) Sends a message to the server containing the security code sent by it in the previous message.

Server Class:

package SD_Server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException; 
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class MessageServer {

    public static int DEFAULT_PORT = 9999; 

    public static void main(String args[]) {
        int serverPort = args.length > 0 ? Integer.parseInt(args[0])
                : DEFAULT_PORT;
        try {
            ServerSocket serverSocket = new ServerSocket(serverPort);
            while (true) {
                System.out.println("Aguardando conexao no endereco: "
                        + InetAddress.getLocalHost().getHostAddress() + ":"
                        + serverPort);
                Socket clientSocket = serverSocket.accept();
                Connection connection = new Connection(clientSocket);
                System.out.println("Conexao feita com: "
                        + clientSocket.getInetAddress() + ":"
                        + clientSocket.getPort());
            } 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class Connection extends Thread {
    private DataInputStream in;
    private DataOutputStream out; 
    private Socket clientSocket;

    public Connection(Socket aClientSocket) {
        try {
            clientSocket = aClientSocket;
            in = new DataInputStream(clientSocket.getInputStream());
            out = new DataOutputStream(clientSocket.getOutputStream());
            this.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String currentTime() {
        return "[" + System.currentTimeMillis() + "]";
    }

    public void run() {
        try {
            // 1: Recebe mensagem com nome se nome
            String msg = in.readUTF();
            System.out.println(currentTime() + " Mensagem recebida de " 
                    + clientSocket.getInetAddress() + ": " + msg);

            // 2: Envia mensagem com codigo de verificacao
            int random4digits = 1000 + (int) (Math.random() * 9999);
            out.writeUTF("Retorne mensagem com o codigo de verificacao: "
                    + random4digits);
            System.out.println(currentTime() + " Enviado para "
                    + clientSocket.getInetAddress() + " o codigo: "
                    + random4digits);

            // 3: Recebe mensagem com codigo de verificacao
            msg = in.readUTF();
            System.out.println(currentTime() + " Mensagem recebida de "
                    + clientSocket.getInetAddress() + ": " + msg);
        } catch (EOFException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

The following is the class Client:

package SD_Server;

import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class MessageCliente {

    public static void main(String[] args) throws UnknownHostException, Exception {
        // Estabelecendo conexao na porta 9999 do servidor! 
        //System.out.println("Estabelecendo conexao com o servidor..");
        Socket clientSocket = new Socket();  




        // recupera as mensagens recebidas do servidor
        Scanner saida = new Scanner(clientSocket.getInputStream());  
        // Envia mensagem para o servidor e o servidor traz a mensagem para o cliente
        // Ler a mensagem do servidor 
        System.out.println("Cliente recebe a mensagem do servidor:  "+saida.next());  
        saida.close(); 
    }
}
    
asked by anonymous 22.03.2016 / 19:23

0 answers