PDA

View Full Version : Chat half duplex java


moskino85
29-10-2013, 18:59
Salve a tutti, vi scrivo perché ho un problema: ho realizzato una chat half duplex utilizzando il protocollo TCP, creando un main per il client e uno per il server .
Ora dovrei complicare l'esercizio in modo da scrivere il codice di gestione del protocollo applicativo in un metodo di una classe .
Dopo devo individuare le variabili di istanza da prevedere nella classe e devo scrivere l’opportuno costruttore per inizializzarla. La classe potrebbe essere una classe che implementa una generica interfaccia.

Ho provato a farla ma da quello che ho capito sbaglio ad utilizzare il PrintStream.Vi allego le varie classi , spero che qualcuno mi possa aiutare. Grazie

import java.io.IOException;

public interface ProtocolHandler {

public void handle() ;

}





import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class ChatHandler implements ProtocolHandler {

private Socket socket;
private byte status;

public ChatHandler(Socket s, byte status) {
this.socket = s;
this.status = status;
}


@Override
// ha bisogno di una socket come parametro e ha bisogno dello stato
public void handle() {
while(status != 0) {
String msg;
String msg2;
switch(status) {
case 1: // client scrive
System.out.print("Scrive Client: ");
Scanner scriveClient = new Scanner(System.in);
msg = scriveClient.nextLine();
PrintStream outToServer = new PrintStream(socket.getOutputStream());
outToServer.println(msg); // il messaggio viene mandato al server
status = 4;
break;

case 2: // client legge
Scanner leggeServer = new Scanner (socket.getInputStream());
msg2 = leggeServer.nextLine();
System.out.println("Server dice: " + msg2);
status = 1;
break;

case 3: // server scrive
System.out.print("Scrive Server: ");
Scanner scriveServer = new Scanner(System.in);
msg = scriveServer.nextLine();
PrintStream outToClient = new PrintStream(socket.getOutputStream());
outToClient.println(msg);
status = 2;
break;

case 4: // server legge
Scanner leggiClient = new Scanner(socket.getInputStream());
msg2 = leggiClient.nextLine();
System.out.println("Client dice: " + msg2);
status = 3;
break;
}




}}}






import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Chat_ServerTCP {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

byte status = 3;

ServerSocket welcomeSocket = new ServerSocket(6789);

Scanner scriveServer = new Scanner(System.in);

Socket connectionSocket = welcomeSocket.accept();

Scanner leggiClient = new Scanner(connectionSocket.getInputStream());

PrintStream outToClient = new PrintStream(connectionSocket.getOutputStream());

ChatHandler c = new ChatHandler(connectionSocket, status);
c.handle();

connectionSocket.close();
}

}



import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Chat_ClientTCP {

/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub

String msg, msg2;

byte status = 1;

// creazione del canale
Socket clientSocket = new Socket("127.0.0.1", 6789);

Scanner scriveClient = new Scanner(System.in);

Scanner leggeServer = new Scanner (clientSocket.getInputStream());

PrintStream outToServer = new PrintStream(clientSocket.getOutputStream());

ChatHandler c = new ChatHandler(clientSocket, status);
c.handle();

clientSocket.close();
}

}


L'errore mi viene dato nella classe ChatHandler, mi chiede di utilizzare le eccezioni ma comunque dopo non mi funziona.Grazie in anticipo a chiunque mi risponderà