Ajuda com cliente servidor
31/03/2013
0
Bom dia pessoal, tenho 3 codigos, um ciente e dois servidores, preciso fazer o seguinte, o cliente envia uma mensagem pro server 1, esse server faz a operação e retorna para o cliente, agora vem a minha duvida, gostaria de saber como faço para esse retorno de resultado do server1 servir como parametro de entrada para o server2?
Vou postar os 3 codigos:
Cliente:
Server1:
Server2:
Vou postar os 3 codigos:
Cliente:
import java.io.*; import java.net.*; public class MyClient { public static void main(String[] arguments) throws Exception { 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));//cria o socket digit digit.setSoTimeout(20000); // Envia identificacao BufferedOutputStream bos = new BufferedOutputStream(digit.getOutputStream());//cria um stream de saida PrintWriter os = new PrintWriter(bos, false); os.println(mesg); os.flush(); // recebe informacoes BufferedReader in = new BufferedReader(new InputStreamReader(digit.getInputStream()));//recebe as informações enviadas pelo server boolean eof = false; while (!eof) { String line = in.readLine(); if (line != null) System.out.println(line); else eof = true; } digit.close();//fecha o socket } catch (IOException e) { System.out.println("IO Error: " + e.getMessage()); } } }
Server1:
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(); } }
Janrie Tidres
Curtir tópico
+ 0
Responder
Posts
04/04/2013
Davi Costa
É só iniciar o server2 após o retorno do server1 não é isso?
att Davi
att Davi
Responder
Clique aqui para fazer login e interagir na Comunidade :)