Allora ho due classi che effettuano tutte le operazioni sulle socket, una per il client e una per il server. Ecco il codice di uno (sono molto simili le due classi):
// Implementa il livello di comunicazione CLIENT
import java.net.*;
import java.io.*;
public class SocketUtilClient {
final static int MSG_TEXT = 1;
final static int FILE_SEND = 2;
final static int FILE_RECEIVE = 3;
final static int EXIT = 4;
final static int NULL = 5;
Socket clientSock;
final int PORT = 6794;
final String HOST = "localhost";
DataOutputStream outToOther;
DataInputStream inFromOther;
public Socket openConnection() {
try {
clientSock = new Socket(HOST, PORT);
} catch (UnknownHostException e) {
return null;
} catch (IOException e) {
return null;
}
try {
outToOther = new DataOutputStream(clientSock.getOutputStream());
} catch (IOException e) {
}
try {
inFromOther = new DataInputStream(clientSock.getInputStream());
} catch (IOException e) {
}
return clientSock;
}
public void closeConnecion() {
try {
clientSock.close();
} catch (IOException e) {
}
}
public int sendFile (String fileName) throws IOException {
int res = 0;
File file = new File(fileName);
int numOfBytes = (int) file.length();
FileInputStream inFile = null;
try {
inFile = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
res = 0;
}
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fileInBytes);
outToOther.write(fileInBytes, 0, numOfBytes);
outToOther.flush();
return res;
}
public int sendString(String text) {
int res = 1;
try {
//outToOther.writeChars(text + "\n\r");
outToOther.writeBytes(text + "\n\r");
outToOther.flush();
System.out.println("Chars scritti: " + text);
} catch (IOException e) {
res = 0;
}
return res;
}
public String readString() {
String textRead = null;
try {
textRead = (inFromOther.readLine()).toString();
} catch (IOException e) {
}
return textRead;
}
public int readInt() {
try {
int q = (int)inFromOther.readInt();
closeStreams();
return q;
}
catch (IOException e3) {
return 0;
}
}
public void sendInt(int num) {
try {
outToOther.writeInt((int)num);
outToOther.flush();
} catch (IOException e) {
}
}
public int readService() {
return readInt();
}
public void requestService(int services) {
sendInt(services);
}
}
Poi il client esegue questo:
SocketUtilClient socket = null;
int tmp = 0;
socket = new SocketUtilClient();
sock = socket.openConnection();
System.out.println("Connessione aperta");
if (sock == null) {
System.out.println("Errore nella socket!");
System.exit(0);
}
// Richiesta del servizio:
socket.requestService(socket.FILE_SEND);
try {
tmp = socket.sendFile("c:\\bootfont.bin");
} catch (IOException e) {
}
if (tmp == 0)
System.out.println("Errore nel file");
socket.requestService(socket.MSG_TEXT);
int w;
w = socket.sendString("Ciao gente secondo messaggio scambiato");
if (w == 0)
System.out.println("Errore nello spedire il msg");
e cosi' via.
La prima richiesta viene eseguita correttamente, tutte le successive no! Richiedo la prima funzione con requestService e poi chiamo il metodo appropriato (esempio sendFile) e funziona. ma poi gia' quando invio la seconda requestService il server legge tutto male.
Ah, ecco il codice del server:
while(service != 4){
service = (int)sockS.readService();
System.out.println("SERVER: servizio letto: " + service);
switch(service) {
case 1: // Stringa di testo
String str = sockS.readString();
System.out.println("Stringa letta sul server: " + str);
//sockS.requestService(sockS.NULL);
break;
case 2: // File send from client
File f = sockS.receiveFile("c:\\received.txt");
//sockS.requestService(sockS.NULL);
break;
case 3: // File receive
break;
case 4: // Fine
break;
case 5: // NULL
break;
}
//System.out.println("Numero letto: " + sockS.readInt());
}
Credo di aver detto tutto sul codice.... che ne pensate?!
Ciao Luca!