//--- TCPServer1.java ---------------------------------
import java.io.*;
import java.net.*;
import java.util.*;
/**
* Ilustra estrutura de um servidor TCP.
*/
public class TCPServer1 {
public static void main(String[] args) {
ServerSocket ss = null;
Socket cliente = null;
OutputStream os = null;
try {
// cria servidor de sockets na porta escolhida
ss = new ServerSocket(0);
// aguarda solicitacao
System.out.println("Server: Aguardando na porta " +
ss.getLocalPort());
while (true) {
// inicializacao da conexao
cliente = ss.accept();
os = cliente.getOutputStream();
// retornou de accept(), solicitacao recebida
System.out.println("Server: Processando solicitacao de " +
cliente.getInetAddress().getHostName());
// obtem informaçao solicitada como sequencia de bytes
String data = "http://www.dca.fee.unicamp.br/courses/PooJava/";
byte[] buffer = data.getBytes();
// escreve data para cliente
System.out.println("Server: Enviando \"" + new String(buffer) + "\"");
os.write(buffer);
os.flush();
}
}
catch (Exception e) {
System.err.println(e);
}
finally {
// fechando servicos...
try {
os.close();
cliente.close();
ss.close();
}
catch (Exception e) {
}
}
}
}
Last modified: Mon Sep 18 15:56:24 EST 2000