First make sure the server has an open port for receiving messages and can handle them.
Then you need a valid java.net.Socket instance to the server.
Socket s = new Socket (address, port);
PrintWriter out = new PrintWriter (s.getOutputStream(), true/*autoFlush*/);
out.println ("Holaraiduljo"); //jodel
Some years ago I wrote this Class for handling outgoing messages.
public class MsgOut extends Thread {
private Socket s;
private PrintWriter outClient;
private Vector outMessages;
private boolean externalStop;
public MsgOut (Socket Socket) {
this.s = socket;
outMessages = new Vector();
try {
outClient = new PrintWriter (socket.getOutputStream(), true/*autoFlush*/);
} catch (IOException e) {}
}
public void run() {
externalStop = false;
outClient.println( "Welcome to Never-Never-Land!" ); //just to test the connection. This should allready arrive at the server.
while (!externalStop) {
while (!outMessages.isEmpty()) {
outClient.println ((String)outMessages.get(0));
outMessages.removeElementAt(0);
}
letMeSleep();
}
}
public synchronized void stopThread() {
externalStop = true;
this.notify();
}
public synchronized void sendMessage (String s) {
outMessages.add (s);
this.notify();
}
private synchronized void letMeSleep() {
try {
wait();
} catch (InterruptedException e) {
}
}
}