Salve ho necessità di scrivere e leggere stringhe da socket esempio java :
public class Connessione {
private Socket connessione;
private PrintStream alServer;
public Connessione(String host_ip,int host_port) throws InterruptedException {
try {
connessione = new Socket(host_ip,host_port);
alServer = new PrintStream(connessione.getOutputStream());
} catch (IOException e) { }
}
public String RxTx(String messaggio) throws IOException, InterruptedException {
String DatiLetti="";
alServer.println(messaggio);
sleep(1700);
BufferedInputStream DalServer = new BufferedInputStream(connessione.getInputStream());
for (int i = 0; i < 10; i++) {
alServer.write(messaggio.charAt(i));
}
char Start =(char)DalServer.read();
while( DalServer.available() > 0 )
{
char ch = (char)DalServer.read();
DatiLetti += ch;
}
return Start+DatiLetti;
}
}
public class Connessione {
private Socket connessione;
private PrintStream alServer;
public Connessione(String host_ip,int host_port) throws InterruptedException {
try {
connessione = new Socket(host_ip,host_port);
alServer = new PrintStream(connessione.getOutputStream());
} catch (IOException e) { }
}
public String RxTx(String messaggio) throws IOException, InterruptedException {
String DatiLetti="";
alServer.println(messaggio);
sleep(1700);
BufferedInputStream DalServer = new BufferedInputStream(connessione.getInputStream());
for (int i = 0; i < 10; i++) {
alServer.write(messaggio.charAt(i));
}
char Start =(char)DalServer.read();
while( DalServer.available() > 0 )
{
char ch = (char)DalServer.read();
DatiLetti += ch;
}
return Start+DatiLetti;
}
}