How to exchange messages with java Socket server and client?

Asked

Viewed 1,039 times

1

I am unable to exchange server messages to the java Socket client.

The server is ready, but the client cannot do according to the server. Which class sends message to the server?

Below are the questions to answer:

1) Sends message to Server. The message must contain the client name.

2) Receives message from Customer.

3) Send a message to the Customer asking the customer to send a randomly generated verification code.

4) Receives message from Server.

5) Sends a message to the Server containing the security code it sent 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();
            }
        }
    }
}

Follows the class Customer:

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(); 
    }
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.