See the InetAddress API in the javadocs.
Basically, you would create a new Socket:
Socket socket;
OutputStream out;
try{
InetAddress address = InetAddress.getByName("xxx.xxx.xxx.xxx") <--- IP address as String
int port = xxxx; <--- client port
socket = new Socket(address, port);
out = socket.getOutputStream(); <---- obtain outputstream for writing to client(s)
String msg = "..."; <--- message to client(s)
byte [] output = msg.getBytes(); <---- convert msg to bytes; optionally specify a charset as an argument
out.write(output); <--------- write output to client(s)
out.flush(); <--- flush output so we are sure that it has been sent (failure to do so can cause client to hang)
} catch (IOException iox){
}finally {
try{
if (out != null) <-- first check whether we have an outputstream at all
out.close(); <---- if so, close outputstream
}catch (IOException iox){
}
Last edited by jay73; 12-11-2008 at 01:15 PM.
|