È possibile scrivere dati su un canale presa come così:
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class SocketWrite {
public static void main(String[] args) throws Exception{
// create encoder
CharsetEncoder enc = Charset.forName("US-ASCII").newEncoder();
// create socket channel
ServerSocketChannel srv = ServerSocketChannel.open();
// bind channel to port 9001
srv.socket().bind(new java.net.InetSocketAddress(9001));
// make connection
SocketChannel client = srv.accept();
// UNIX line endings
String response = "Hello!\n";
// write encoded data to SocketChannel
client.write(enc.encode(CharBuffer.wrap(response)));
// close connection
client.close();
}
}
Il InetSocketAddress può variare a seconda di cosa si sta collegando.
fonte
2009-05-16 07:21:02
Grazie John, Grazie per il vostro supporto, questo è uno dei programmi eccellenti. Il mio errore è stato che non ho usato "\ n" alla fine della mia stringa ... – Deepak
Attenzione, dipende dalla piattaforma. –
Ciao John Puoi menzionare su quale canale di socket della piattaforma non funziona. – Deepak