Ajuda para desenvolver um Cliente Servidor

2 respostas
C

Boa noite pessoal,
sou novo aqui no forum e ja venho pedindo ajuda para vocês.

Estou tentando desenvolver um cliente servidor, por enquanto o que eu tenho são dois clientes que mandam mensagens para um servidor.
mas o que eu tenho que fazer são dois clientes se comunicando entre si.

por favor, peço a ajuda de vocês o mais rapido possivel!

Cliente

import java.net.*;
import java.io.*;
public class Cliente {
    public static void main(String arg[]) throws IOException {
	int defaultport = 1000;
	String defaultserver = "localhost";
	int porta;
	String server;
	if (arg.length>0) {
	    server = arg[0];
	    porta = (Integer.valueOf(arg[1])).intValue();	    
	} else {
	    server = defaultserver;
	    porta = defaultport;	    
	}
	Socket sock = new Socket(server, porta);
	DataInputStream dis = new DataInputStream(sock.getInputStream());
	PrintStream dat = new PrintStream(sock.getOutputStream());
	String strcliid = dis.readLine();
	System.out.println("Sou o cliente no." + strcliid + " do servidor.");
	String dadosConsole;
	String dadosServidor;
	DataInputStream inConsole = new DataInputStream(System.in);
	try {
	    System.out.print("Cliente[" + strcliid + "]: ");
	    dadosConsole = inConsole.readLine();
	    // ...Continuacao
	    while (!dadosConsole.equals("fim")) {
		dadosConsole = strcliid + "#" + dadosConsole;
		dat.println(dadosConsole);
		dat.flush();
		dadosServidor = dis.readLine();
		System.out.println("Servidor: " + dadosServidor);
		System.out.print("Cliente[" + strcliid + "]: ");
		dadosConsole = inConsole.readLine();
	    }
	    dadosConsole = strcliid + "#" + dadosConsole;
	    dat.println(dadosConsole);
	    dat.flush();
	    sock.close();
	} catch (Exception e) {
	    System.out.println(e);
	}
    }
}
Servidor
import java.io.*;
import java.net.*;

public class Servidor implements Runnable {
    Socket sock;
    static int nclient = 0, nclients = 0;
    static ServerSocket servsock;

    public static void main(String arg[]) throws IOException {
	int defaultport = 1000;
	int porta;
	if (arg.length>0) {
	    porta = (Integer.valueOf(arg[0])).intValue();	    
	} else {
	    porta = defaultport;
	}
	servsock = new ServerSocket(porta);
	Thread servDaemon = new Thread(new Servidor());
	servDaemon.start();
	System.out.println("Servidor ativo em " + InetAddress.getLocalHost()
		+ " na porta " + porta + "!");
    }

    public void run() {
	for (;;) {
	    // ...Continuacao
	    try {
		sock = servsock.accept();
		nclient += 1;
		nclients += 1;
		System.out.println("CONECTANDO " + sock.getInetAddress()
			+ " da porta " + sock.getPort() + ".");
	    } catch (IOException ioe) {
		System.out.println(ioe);
	    }
	    Worked w = new Worked(sock, nclient);
	    w.start();
	}
    }

    class Worked extends Thread {
	Socket sock;
	int clientid, poscerq;
	String strcliid;

	public Worked(Socket s, int nclient) {
	    sock = s;
	    clientid = nclient;
	}

	public void run() {
	    String dadosCliente;
	    PrintStream out = null;
	    DataInputStream in = null;
	    try {
		out = new PrintStream(sock.getOutputStream());
		in = new DataInputStream(sock.getInputStream());
		out.println(clientid);
		dadosCliente = in.readLine();
		poscerq = dadosCliente.indexOf("#");
		strcliid = dadosCliente.substring(0, poscerq);
		dadosCliente = dadosCliente.substring(poscerq + 1, dadosCliente
			.length());
		while (!dadosCliente.equals("fim")) {
		    System.out
			    .println("Cli[" + strcliid + "]: " + dadosCliente);
		    out.println("String '" + dadosCliente + "' recebida.");
		    out.flush();
		    dadosCliente = in.readLine();
		    poscerq = dadosCliente.indexOf("#");
		    strcliid = dadosCliente.substring(0, poscerq);
		    dadosCliente = dadosCliente.substring(poscerq + 1,
			    dadosCliente.length());
		}
		System.out.println("DESCONECTANDO " + sock.getInetAddress()
			+ "!");
		nclients -= 1;
		System.out.println("No. de clientes atuais: " + nclients);
		if (nclients == 0) {
		    System.out.println("Fechando servidor...");
		    sock.close();
		    System.exit(0);
		}
	    } catch (IOException ioe) {
		System.out.println(ioe);
	    }
	}
    }
}

2 Respostas

C

up! :roll:

C

up.

Criado 13 de junho de 2008
Ultima resposta 15 de jun. de 2008
Respostas 2
Participantes 1