0% found this document useful (0 votes)
5 views3 pages

Cliente Servidor Inrs

The document contains Java code for a simple server-client application. The server listens for connections on port 12345 and receives messages from a connected client, while the client connects to the server, allows user input, and sends messages until the user types 'sair'. Both classes handle input and output streams for communication and include error handling for IO exceptions.

Uploaded by

Mindoh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Cliente Servidor Inrs

The document contains Java code for a simple server-client application. The server listens for connections on port 12345 and receives messages from a connected client, while the client connects to the server, allows user input, and sends messages until the user types 'sair'. Both classes handle input and output streams for communication and include error handling for IO exceptions.

Uploaded by

Mindoh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Import java.io.

*;

Import java.net.*;

Public class Server {

Public static void main(String[] args) {

Int port = 12345; // Porta do servidor

Try (ServerSocket serverSocket = new ServerSocket(port)) {

System.out.println(“Servidor aguardando conexões na porta “


+ port + “…”);

Socket socket = serverSocket.accept();

System.out.println(“Cliente conectado: “ +
socket.getInetAddress());

// Criando fluxo de entrada para receber mensagens do cliente

BufferedReader input = new BufferedReader(new


InputStreamReader(socket.getInputStream()));

String message;

While ((message = input.readLine()) != null) {

System.out.println(“Mensagem recebida: “ + message);

System.out.println(“Cliente desconectado.”);

Socket.close();

} catch (IOException e) {

e.printStackTrace();

}
}

Cliente

Import java.io.*;

Import java.net.*;

Public class Client {

Public static void main(String[] args) {

String serverAddress = “localhost”; // Endereço do servidor

Int port = 12345; // Porta do servidor

Try (Socket socket = new Socket(serverAddress, port);

PrintWriter output = new


PrintWriter(socket.getOutputStream(), true);

BufferedReader userInput = new BufferedReader(new


InputStreamReader(System.in))) {

System.out.println(“Conectado ao servidor. Digite mensagens


para enviar (ou ‘sair’ para encerrar):”);

String message;

While (true) {

System.out.print(“Digite: “);

Message = userInput.readLine();

If (“sair”.equalsIgnoreCase(message)) {

Break;
}

Output.println(message);

System.out.println(“Cliente encerrado.”);

} catch (IOException e) {

e.printStackTrace();

You might also like