Ajuda com cliente servidor

1 resposta
J

Pessoal, tenho dois servidores e um cliente, explicando, o cliente envia uma mensagem pro servidor através da porta, e o servidor devolve o resultado da operação para o cliente, mas tenho uma duvida, gostaria de saber como eu faço pra que esse resultado que foi devolvido para o cleinte seja enviado por ese mesmo cliente para um segundo server, tipo o resultado do primeiro server servir de parametro para o segundo server como se tivesse enviando automaticamente, vou postar o codigo dos dois server e do cliente e se alguem puder me ajudar eu agradeceria muito:

Server:
import java.io.*;
import java.net.*;
import java.util.Date;
 
public class MyServer extends Thread {
	private ServerSocket sock;
 
	public MyServer(int port) {
		super();
		try {
			sock = new ServerSocket(port);
			System.out.println("MyServer running at port " + port);
		} catch (IOException e) {
			System.out.println("Error: couldn't create socket.");
			System.exit(1);
		}
	}
 
	public void run() {
		Socket client = null;
 
		while (true) {
			if (sock == null)
				return;
			try {
				client = sock.accept();
				// Recebendo identificacao
				BufferedReader in = new BufferedReader(new InputStreamReader(
						client.getInputStream()));
				String c = in.readLine();
				System.out.println(c);
				// Enviando Informacoes
				BufferedOutputStream bos = new BufferedOutputStream(client
						.getOutputStream());
				PrintWriter os = new PrintWriter(bos, false);
				os.println(executar(c));
				os.flush();
				os.close();
				client.close();
			} catch (IOException e) {
				System.out.println("Error: couldn't connect to client.");
				System.exit(1);
			}
		}
	}
	
	private String executar(String conteudo) {
		return conteudo.toUpperCase();
	}
 
	public static void main(String[] arguments) {
		int port = 4000;
		if (arguments.length == 1) {
			port = Integer.parseInt(arguments[0]);
		}
		MyServer server = new MyServer(port);
		server.start();
	}
}
Server2:
import java.io.*;
import java.net.*;
import java.util.Date;
 
public class MyServer2 extends Thread {
	private ServerSocket sock;
 
	public MyServer2(int port) {
		super();
		try {
			sock = new ServerSocket(port);
			System.out.println("MyServer running at port " + port);
		} catch (IOException e) {
			System.out.println("Error: couldn't create socket.");
			System.exit(1);
		}
	}
 
	public void run() {
		Socket client = null;
 
		while (true) {
			if (sock == null)
				return;
			try {
				client = sock.accept();
				// Recebendo identificacao
				BufferedReader in = new BufferedReader(new InputStreamReader(
						client.getInputStream()));
				String c = in.readLine();
				System.out.println(c);
				// Enviando Informacoes
				BufferedOutputStream bos = new BufferedOutputStream(client
						.getOutputStream());
				PrintWriter os = new PrintWriter(bos, false);
				os.println(executar(c));
				os.flush();
				os.close();
				client.close();
			} catch (IOException e) {
				System.out.println("Error: couldn't connect to client.");
				System.exit(1);
			}
		}
	}
	
	private String executar(String conteudo) {
		return conteudo.toLowerCase();
	}
	
	public static void main(String[] arguments) {
		int port = 4001;
		if (arguments.length == 1) {
			port = Integer.parseInt(arguments[0]);
		}
		MyServer2 server = new MyServer2(port);
		server.start();
	}
}
Cliente:
import java.io.*;
import java.net.*;
 
public class MyClient {
	public static void main(String[] arguments) {
		String mesg;
		String host;
		String port;
		if (arguments.length == 3) {
			host = arguments[0];
			port = arguments[1];
			mesg = arguments[2];
		} else {
			System.out.println("Usage: java MyClient host port message");
			return;
		}
		try {
			Socket digit = new Socket(host, Integer.parseInt(port));
			digit.setSoTimeout(20000);
 
			// Envia identificacao
			BufferedOutputStream bos = new BufferedOutputStream(digit
					.getOutputStream());
			PrintWriter os = new PrintWriter(bos, false);
			os.println(mesg);
			os.flush();
 
			// recebe informacoes
			BufferedReader in = new BufferedReader(new InputStreamReader(digit
					.getInputStream()));
			boolean eof = false;
			while (!eof) {
				String line = in.readLine();
				if (line != null)
					System.out.println(line);
				else
					eof = true;
			}
			digit.close();
		} catch (IOException e) {
			System.out.println("IO Error: " + e.getMessage());
		}
	}
}

1 Resposta

J

Como ninguem respondeu vcs acham que eu deveria postar no topico de avançado?

Criado 29 de março de 2013
Ultima resposta 31 de mar. de 2013
Respostas 1
Participantes 1