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.
Siccome ho ripreso da poco a scrivere in java, non ricordo più molte cose e mi sono bloccato non riuscendo più ad andare avanti.
Qualcuno mi potrebbe dare una mano ad impostare l'esercizio?
Io ho provato a farlo però non funziona
codice:
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();
}
}
codice:
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();
}
}
codice:
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class ChatHandler implements ProtocolHandler {
private Socket socket;
privatebytestatus;
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) {
Object msg;
String msg2;
switch(status) {
case 1: // client scrive
System.out.print("Scrive Client: ");
Scanner scriveClient = null;
msg = scriveClient.nextLine();
PrintStream outToServer = null;
outToServer.println(msg); // il messaggio viene mandato al server
status = 4;
break;
case 2: // client legge
Scanner leggeServer = null;
msg2 = leggeServer.nextLine();
System.out.println("Server dice: " + msg2);
status = 1;
break;
case 3: // server scrive
System.out.print("Scrive Server: ");
Scanner scriveServer = null;
msg = scriveServer.nextLine();
PrintStream outToClient = null;
outToClient.println(msg);
status = 2;
break;
case 4: // server legge
Scanner leggiClient = null;
msg2 = leggiClient.nextLine();
System.out.println("Client dice: " + msg2);
status = 3;
break;
}
}}}
codice:
publicinterface ProtocolHandler {
public void handle();
}
Qualcuno mi potrebbe dare una mano a capire dove sbaglio? grazie mille a chiunque mi risponderà