Applet Socketing

Im trying to make an applet that will connect to a server program.
I know that the port im using is being forward because i compiled the QuoteServer from the examples and it worked. When I try in an applet it doesnt want to compile.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class QuoteApplet extends Applet {
  DatagramSocket socket;
  DatagramPacket packet;
  InetAddress address;
  byte[] buf;
  String received;
  public void init() {
    socket = new DatagramSocket();
    address = getCodeBase().getHost();
    System.out.println(address);
  public void sendy() {
    // send request
    buf = new byte[256];
    packet = new DatagramPacket(buf, buf.length, address, 4445);
    socket.send(packet);
    packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);
    received = new String(packet.getData());
    System.out.println("Quote of the Moment: " + received);
    socket.close();
}Its an adaption of the QuoteClient I know if compiled it will do nothing im trying to make sure that the basic structure works.
javac -target 1.5 QuoteApplet.java
QuoteApplet.java:20: incompatible types
found : java.lang.String
required: java.net.InetAddress
address = getCodeBase().getHost();
^
1 error

Ive used the
address = getByName("ganbree.no-ip.info");
it churs out Try Catch errors that ive fixed now.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class QuoteApplet extends Applet {
  DatagramSocket socket;
  DatagramPacket packet;
  InetAddress address;
  byte[] buf;
  String received;
  public void init() {
    try {
      socket = new DatagramSocket();
    } catch(java.net.SocketException e) {
      e.printStackTrace();
    try {
    address = InetAddress.getByName("ganbree.no-ip.info");
    } catch(java.net.UnknownHostException e) {
      e.printStackTrace();
//    address = InetAddress.getByName(getCodeBase().getHost() );
//    address = InetAddress.getHostByName(getCodeBase().getHost() );
//  address = InetAddress.getHostByName("ganbree.no-ip.info");
//  address = getCodeBase().getHost();
    System.out.println(address);
  public void sendy() {
    // send request
    buf = new byte[256];
    packet = new DatagramPacket(buf, buf.length, address, 4445);
    try {
      socket.send(packet);
    } catch(java.io.IOException e) {
      e.printStackTrace();
    packet = new DatagramPacket(buf, buf.length);
    try {
      socket.receive(packet);
    } catch(java.io.IOException e) {
      e.printStackTrace();
    received = new String(packet.getData());
    System.out.println("Quote of the Moment: " + received);
    socket.close();
have 2 dollars?

Similar Messages

  • Applet socket server

    Is it possible to have applet socket server? if so, pls tell me the method. Thanks.
    regards,
    Bala

    applet socket server?hmm...i dont think so...this will produce an exception... but maybe you can try signing the applet....

  • Applet socket connection

    Hi,
    I have an applet that connects to a server java console program through a socket connection which works fine from the command line using appletviewer but doesn't work when running in a html page. The applet is not signed however because the applet and server are running on the same machine it shouldn't need to be. I have used socket connections in applets before and have managed to make the connection to server without having it signed but this one is not working for some reason. I have checked the port is opened and again its the same one I have used before so I know its open.
    I am getting the message java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:5000 connect,resolve) when calling:
      Socket s = new Socket(ip,port);I also have a java.policy.applet file in the project root directory which has the following in:
    grant {
      permission java.security.AllPermission;
    };Does anyone know a reason why access could be restricted?

    Night.Monkey wrote:
    Hi,
    I have an applet that connects to a server java console program through a socket connection which works fine from the command line using appletviewer but doesn't work when running in a html page. The applet is not signed however because the applet and server are running on the same machine it shouldn't need to be.Wrong. An unsigned applet can only connect back to the server from where it was downloaded. Your applet comes from www.javawebgames.co.uk and tries to connect to 127.0.0.1.
    Would your server be at www.javawebgames.co.uk there should be no problem.
    I also have a java.policy.applet file in the project root directory which has the following in:
    grant {
    permission java.security.AllPermission;
    };I think this should have zero effect when you are running in the actual browser.

  • Applet socket communication

    Hello,
    I've successfully created a simple client-server application that communicate through the socket,
    and now converting to applet, but the following error occured on the java console
    java.security.AccessControlException: access denied (java.net.SocketPermission 203.xxx.xxx.xxx:9800 connect,resolve)
    that's also the web server where the applet is downloaded
    when i try my computer to run the applet (changed the socket ip to 127.0.0.1 or 192.168.0.111 and run the server app) it works just fine..
    but when i changed it to the public IP and put it on the webserver that's what happen..
    is there any way beside change the policy?
    can anyone please help?
    I'm really in a big need of help
    thank you..
    Edited by: bigpappa on Mar 17, 2009 4:05 AM

    1) There will be no remote server applet. One of the clients will have to run it. Applets are always executed on the client only.
    2) For applets communicating with anything else but the server they came from, they need to be signed.

  • Applet socket problem in client-server, urgent!

    Dear All:
    I have implemented a client server teamwork environment. I have managered to get the server running fine. The server is responsible for passing messages between clients and accessing Oracle database using JDBC. The client is a Java Applet. The code is like the following. The problem is when I try to register the client, the socket connection get java.security.AccessControlException: access denied(java.net.SocketPermission mugca.its.monash.edu.
    au resolve)
    However, I have written a Java application with the same socket connection method to connect to the same server, it connects to the server without any problem. Is it because of the applet security problem? How should I solve it? Very appreciate for any ideas.
    public class User extends java.applet.Applet implements ActionListener, ItemListener
    public void init()
    Authentication auth = new Authentication((Frame)anchorpoint);
    if(auth.getConnectionStreams() != null) {
    ConnectionStreams server_conn = auth.getConnectionStreams();
    // Authenticates the user and establishes a connection to the server
    class Authentication extends Dialog implements ActionListener, KeyListener {
    // Object holding information relevant to the server connection
    ConnectionStreams server_conn;
    final static int port = 6012;
    // Authenticates the user and establishes connection to the server
    public Authentication(Frame parent) {
    // call the class Dialog's constructor
    super(parent, "User Authentication", true);
    setLocation(parent.getSize().width/3, parent.getSize().height/2);
    setupDialog();
    // sets up the components of the dialog box
    private void setupDialog() {
    // create and set the layout manager
    //Create a username text field here
    //Create a password text field here
    //Create a OK button here
    public void actionPerformed(ActionEvent e) {
    authenticateUser();
    dispose();
    // returns the ConnectionStreams object
    public ConnectionStreams getConnectionStreams() {
    return(server_conn);
    // authenticates the user
    private void authenticateUser() {
    Socket socket;
    InetAddress address;
    try {
    // open socket to server
    System.out.println("Ready to connect to server on: " + port);
    socket = new Socket("mugca.its.monash.edu.au", port);
    address = socket.getInetAddress();
    System.out.println("The hostname,address,hostaddress,localhost is:" + address.getHostName() + ";\n" +
    address.getAddress() + ";\n" +
    address.getHostAddress() + ";\n" +
    address.getLocalHost());
    catch(Exception e) {
    displayMessage("Error occured. See console");
    System.err.println(e);
                                  e.printStackTrace();
    }

    Hi, there:
    Thanks for the help. But I don't have to configure the security policy. Instead, inspired by a message in this forum, I simply upload the applet to the HTTP server (mugca.its.monash.edu.au) where my won server is running. Then the applet is download from the server and running fine in my local machine.
    Dengsheng

  • Applet to Applet socketeering

    Hi,
    I'm almost totally new to networking programming so I have a really simple question to ask: How can I have an applet running in a browser on one computer open a socket to another instance of that applet running on another computer?
    I guess I just need a tutorial or example or something. I'm fairly proficient with Java so don't worry about dumbing down the syntax. I already went through the Sun tutorial that teaches you how to download yahoo.com/index.html if that's any help.
    Thanks
    Neal

    Okay well I know next to nothing about servers.
    I have a subdomain on my brother's site that supports ftp. I believe it's hosted by these guys: http://www.1asphost.com/
    It seems like the easiest thing is to run a Java program on the server that orchestrates everything.
    Is my subdomain enough or do I need to look for something else? Also, how do I get the server to execute it?

  • Applet & socket problem

    I want to request Socket port (23)and exchange string from Applet program
    When I try this program , java security exception occur
    how I can use port 23 from Applet
    thanks

    you either have to sign your applet or change you java.policy file to allow the applet to use the socket. to edit the policy file you can use policytool or a text editor.
    more info can be found at http://java.sun.com/docs/books/tutorial/security1.2/summary/tools.html

  • Chat Applet - Sockets - Server

    Hello Fellows!
    I hope u all r fine..
    I am facing a problem in my client/sever application. The communication is Socket Based. i.e the server is using sockets and the client which is an Applet, uses sockets to communicate with the sever.
    The application is working fine in the appletviewer. i.e the applet server communication, both can send and recieve messages.
    BUT
    When I use IE5 the client applet can send messages to the sever but is not able to read from the specified socket...
    If Any one can help me out, i will be greatfull. If u need some code segment then let me know.
    Looking forward for any suggestions.
    Thanking You!
    Ahmad.

    Thanks!
    for your contribution....
    but now i realised that the problem is that the client socket thread is not running.
    //////////////////////// Client Applet
    public class Client extends Applet implements ActionListener
    Thread thRead;
    ClientSocket cs;
    private Panel pnlHead = new Panel();
    private Panel pnlMain = new Panel();
    public static Vector vtrMembers;
    public void init()
    thRead = (Thread) new ClientSocket();
         cs = (ClientSocket) thRead;
         vtrMembers = new Vector();
    thRead.start();     
    cs.out.println("INI ? INI");
    ////////////////////////////////////////Client scoket class
    public class ClientSocket extends Thread
    // public static Socket sClient;
    public Socket sClient;
    // A character-input stream to read from the socket.
    public static BufferedReader in;
    // A text-output stream to write to the socket.
    public PrintWriter out ;
    // String to store the input from the client
    String strText;
    // Boolean Flag to Check the read value
    public boolean bFlag = true;
    public ClientSocket ()
         try
         sClient = new Socket ("Ahmed" , 4000);
         // Initializing the Input straem used to read the socket.
    in = new BufferedReader(new InputStreamReader(sClient.getInputStream()));
         // Initializing the Output straem used to write on the socket.
         out = new PrintWriter(sClient.getOutputStream(), true);
         catch (UnknownHostException uhe)
    //     System.out.println (" Unknown Host Exception : " + uhe);
         catch (IOException ioe)
    //     System.out.println (" I/O Exception : " + ioe);
    public void run()
         for (;;)
         if (bFlag == true)
         Client.txtTry.setText("TRUE");
         else
         Client.txtTry.setText("False");
    thRead.start() statment is executed but the thread does not stat running
    because the text value is not set on the Client Applet.

  • Servlet/applet/socket problem

    Hi there,
    my problem is this...
    i create a page with an servlet whicht shows an applet. this applet connects to a server through an socket. it works fine, but, when i change the page and the applet is not longer on the screen, the socket connection will be not killed! i dont know why. but i must kill it when the applet is not longer on the screen...whats wrong? can you help me?
    matthias

    Are you overriding the destroy() method in your Applet? This will be called when the applet is about to be unloaded from the browser. Or, as a slightly different alternative, you can override the stop() method, which is called when the applet is scrolled off the screen. Usually when stop() is used, start() is overriden as well to allow the applet to resume when it is scrolled back into view. For more info, read up on the applet lifecycle.

  • Re:Applet socket communication

    Hi guys,
    Is it possible to get two applet windows one on the client side and other on the remote using only the Socket() class.
    Actually we have a game in which it is connecting to a server and write some commands to that server.based on the commands applet windows should appear both in client as well as in remote.
    Can anybody tell me about this.

    1) There will be no remote server applet. One of the clients will have to run it. Applets are always executed on the client only.
    2) For applets communicating with anything else but the server they came from, they need to be signed.

  • Applet socket

    I am trying to make a socket to a server. The code below works in a regular program, but when I paste it into the init method of my applet, it has an error.
    try
    Socket Sockets = new Socket (server, port);
    PrintWriter Out = new PrintWriter (Sockets.getOutputStream (), true);
    BufferedReader In = new BufferedReader (new InputStreamReader (Sockets.getInputStream ()));
    Out.println (stuffitssending);
    while (true)
    System.out.println(In.readLine());
    Message was edited by:
    hnghcgcgmghc

    Oh woops. I should have known that.
    Applets are locked down pretty severely. You cant even
    read/write a file and you certainly cant open a socket.
    You have to sign your applet.
    How do you sign an applet?
    http://www.google.com/search?hl=en&q=how+sign+applet
    Search the forums (theres even a whole section).

  • Applet socket connect behaves wrong.

    Hi,
    I have a very strange behavior from the Socket connect method. When i run the code attached as an applet the connect returns as if the connection is established (no machine on my network with the address 12.12.12.12 and ping to 12.12.12.12 fails as expected).
    When i run it from a command line (in a stadard main) the connect behaves as expected an i get an exception.
    Any ideas? what am i doing wrong? (or is it a known applet behavior???)
    thanks...
    ===========================================================
    the code:
    ===========================================================
    public void start() {
    SocketAddress sockaddr = new InetSocketAddress("12.12.12.12", 16000);
    Socket sock = new Socket();
    try {
    System.out.println("Start conn 1");
    sock.connect(sockaddr, 3000);
    } catch (IOException e) {
    System.out.println("Connect failed");
    e.printStackTrace();
    return;
    if (sock.isConnected()) {
    System.out.println("Connected");
    System.out.println("Connect succ 1");
    Edited by: ohad_atia on Jun 28, 2009 12:25 AM

    here is the applet start code - the rest of the methods are empty:
    (no exception here - that is the problem)
          public void start() {
              SocketAddress sockaddr = new InetSocketAddress("12.12.12.12", 16000);
                     Socket sock = new Socket();
              try {
                   System.out.println("Start conn 1");
                   sock.connect(sockaddr, 3000);
              } catch (IOException e) {
                   System.out.println("Connect failed");
                  e.printStackTrace();
                  return;
              if (sock.isConnected()) {
                 System.out.println("Connected");
              System.out.println("Connect succ 1");
          }the code and output i got when i run it using a command line:
    import java.net.Socket;
    import java.net.SocketAddress;
    import java.net.InetSocketAddress;
    import java.io.IOException;
    public class networktest {
            public static void main(String[] argv) {
                    SocketAddress sockaddr = new InetSocketAddress("12.12.12.12", 16000);
                    Socket sock = new Socket();
            try {
                    sock.connect(sockaddr, 3000);
                } catch (IOException e) {
                    System.out.println("Connect failed");
                    e.printStackTrace();
                    return;
                if (sock.isConnected()) {
                       System.out.println("Connected");
                System.out.print("Connect succ");
    }output:
    Connect failed
    java.net.SocketTimeoutException: connect timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at networktest.main(networktest.java:17)
    thanks for your help

  • Applet socket exception

    An applet connects to a socket on the server and works fine. It establishes Object input and Output streams.
    When the web page is closed the server catchs a SocketException which is thrown due to the readObject() method being called on an ObjectInputStream which obviously no longer exists.
    Is there any other way of telling the server that the applet has terminated or due i just use this exception to clean things up (i.e. close the streams and coonection and de-reference the thread).
    thanks.

    The applet could send a message to the server in the method Applet#stop()

  • Applet Socket permission problem

    Heloo I made an applet which gets the content of web . like if i pass the www.yahoo.com to applet it must read all the html coding of yahoo page .my code is perfect with the desktop application but when i run it with applet then it gives the socket permission error i search all where but i havent found any solution i tried to edit policy file but still found the same error kindly help me how to get rid from this error thanks and below is my code sample
    Applet code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.*;
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.security.Permission;
    public class Apple extends JApplet implements ActionListener{
              Container c;
         JTextField uf=new JTextField(30);
         JTextArea tarea=new JTextArea(600,600);
         JButton b=new JButton("Get Content");
         JButton bb=new JButton("Clear");
         URL u;
         JScrollPane sp=new JScrollPane();
         InputStream is = null;
    DataInputStream dis;
    String s;
         public void init(){
              JOptionPane.showMessageDialog(null,"hello world");
              c=new Container();
                   JOptionPane.showMessageDialog(null,"Demo Product Developed By sarc");
              c=this.getContentPane();
              uf.setText("http://www.yahoo.com");
              c.setLayout(new FlowLayout(FlowLayout.LEFT));
              sp.add(tarea);
              c.add(new JLabel("Enter Your Url"));
              c.add(uf);
              c.add(b);     c.add(bb);
              c.add(new Label("EXAMPLE: http://www.rentacoder.com"));
              b.addActionListener(this);bb.addActionListener(this);
              c.add(tarea);     
         public void actionPerformed(ActionEvent ae){
              if( ae.getSource()==b){
                        JOptionPane.showMessageDialog(null,"Demo Product Developed By sarc");
              down();
              if( ae.getSource()==bb){
    tarea.setText("");
              public void down(){
                   try{
                   System.out.println("1");
                   String uu=uf.getText()+":80";
                   u = new URL(uf.getText());
                   is = u.openStream();
                   dis = new DataInputStream(new BufferedInputStream(is));
                   String str;int k=0;int l=50;
                   while ((s = dis.readLine()) != null) {
    tarea.append(s+"\n");
    k++;
    if(k==l){
         JOptionPane.showMessageDialog(null,"Demo Product Developed By sarc");
    l=2*k;
    }is.close();
    } catch (MalformedURLException e) {JOptionPane.showMessageDialog(null,"e"+e);
    } catch (IOException e) {JOptionPane.showMessageDialog(null,e);
    HTML CODE :
    <applet code="Apple.class" width="300" height="300">
    </applet>

    The applet should be able to connect to the host it came from but I think you
    have to connect to the same port as well (not really sure about that).
    You can either have the consumers of your applet set up a policy for your applet
    or you sign the applet.
    Signing applets:
    http://forum.java.sun.com/thread.jsp?forum=63&thread=524815
    second post and last post for the java class file
    http://forum.java.sun.com/thread.jsp?forum=63&thread=409341
    4th post explaining how to set up your own policy with your own keystore
    Java security configuration (according to me):
    1. Signing, good for Internet published applets. The user will be asked if he or she trusts
    the applet and when yes or allways is clicked the applet can do whatever it wants. This
    is the default setting of the SUN jre and can be compared with IE setting that askes the
    user about downloading and running ActiveX controls from the Internet security zone.
    2. Setting up a policy. Good for people who disabled asking the user about signed
    applets (like companies that are worried this could cause a problem). it is possible
    to provide multiple java.policy files in the java.security, a company could put a .policy file
    on the Intanet and have all jre's use this by adding this URL to the java.security.
    When a policy needs to be changed the admin only has to do this is the file on the
    Intranet.
    A specific user can have a policy in their user.home to set up personal policies (to be
    done by Administrators).
    A policy file can use a keystore to be used in a signed by policy. For example "applets
    that are signed by SUN can access some files on my machine). It can allso be used
    to identify yourselve, when making an SSL connection the keystore can be used as
    the source of your public key.

  • Win socket with applet socket by byte type data network

    Server is windock
    Client is applet
    How do i receive and send byte type data..
    What i do using sock type?
    UDP? just i use Inputstream, Outputstream?
    and i some warring..
    Packet Buffering...
    how do i?
    plz help me...

    Hi
    Search the Forum for "socket", start reading other people questions and answers, i am sure that after spending few hours you will find some answers.
    Noah

Maybe you are looking for

  • Creative Zen Micro and Podcast

    I want to subscribe to a series of National Public Radio podcasts on a tool (maybe iTunes but I don't think the Creative Zen Micro works with iTunes) and be able to easily sync these onto my player. I have a Creative Zen Micro about a year old (or mo

  • Problem with accessing database

    Hi everybody I try to execute some wars included with the J2EE tutorial final version and I follow all the steps in building and deploying the specified wars, some of those wars is a punch of JSP pages and Servlet that make access to database. When I

  • DataSource or Crosstab -- List of all Members in the current selection

    Hey Design Studio Experts, i need a collection of the  >>current<< member list of a datasource, Example: i have the Datasource DS_1, which is filtered by 2-3 filters, so the number of lines, that is delivered by the DS_1 is less than the number of li

  • Email alerts question

    when i get an email i only get a star on the envelope icon and not the yahoo mail icon. this is a warranty replacement phone, on my previous phone(same model) showed a star on both the envelope and the yahoo mail icons. i have the blackberry 8530 v5.

  • Modifying permissions crashes Finder

    I have found that using the "Get Info" window to modify permissions for files and folders in Finder often result in Finder crashing (and automatically restarting). This seems to be a problem specific to Leopard, as I had not noticed this prior to upg