Questa è la funzione che prenderà i byte dal "br" e li manderà alla seriale.
codice:
class Send extends Thread{
private byte[] pak;
public Send(byte[] pak) {
this.pak= pak;
}
public void run(){
try {
reader.writePacket(pak);
}
catch (IOException e) {
System.exit(2);
}
stampa(System.out, pak);
System.out.println();
jTextArea2.setCaretPosition(jTextArea2.getText().length());// autoscroll
jTextArea2.append("scrivo pacchetto"+(i-1)+": "+hexString(pak)+" \n");//stampa su schermo
}
}
Ti incollo tutto il metodo del server, abbiamo già affrontato questo problema per mandare i byte al client, adesso dobbiamo anche riceverli dal client e mandarli tramite la funzione reder.writePacket(pak)
codice:
try
{
// Ricava lo stream di output associate al socket
// e definisce una classe wrapper di tipo
// BufferedWriter per semplificare le operazioni
// di scrittura
OutputStream s1out = cliente.getOutputStream();
InputStream s1in = cliente.getInputStream();
BufferedOutputStream bw = new BufferedOutputStream(s1out);
BufferedInputStream br = new BufferedInputStream(s1in);
// ByteArrayOutputStream bos = new ByteArrayOutputStream(new OutputStreamWriter(s1out));
// Il server invia la risposta al client
inattivo=false;
bw.write(VERSION);
while(!inattivo){
if(available==false){ // gestione concorrente con la scrittura dei pacchetti
//bw.write(hexString(packet)+"\n");
if (packet.length > 255)
throw new IOException("pacchetto troppo lungo\n");
if (packet.length == 0)
throw new IOException("pacchetto troppo corto\n");
bw.write((byte)packet.length);
bw.write(packet);
bw.flush();
br.read(pak);
System.out.print(pak);
available=true;
}
}