The Artima Developer Community
Sponsored Link

Java Answers Forum
how to post message to server in an applet

2 replies on 1 page. Most recent reply: Mar 9, 2005 9:35 AM by nick yang

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
nick yang

Posts: 2
Nickname: tsfy
Registered: Mar, 2005

how to post message to server in an applet Posted: Mar 3, 2005 12:31 AM
Reply to this message Reply
Advertisement
I want to use an applet to post message to the server.
What should I do in the ActionPerformed method of the
"submit" button's action?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: how to post message to server in an applet Posted: Mar 3, 2005 11:58 PM
Reply to this message Reply
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) {
            }
        }
    }

nick yang

Posts: 2
Nickname: tsfy
Registered: Mar, 2005

Re: how to post message to server in an applet Posted: Mar 9, 2005 9:35 AM
Reply to this message Reply
Thanks for your help

I complete my login module

Flat View: This topic has 2 replies on 1 page
Topic: Web Translator - to translate the web page from English to Chinese Previous Topic   Next Topic Topic: Why an abstract method cannot be neither final nor static?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use