Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Applet-Sockets(Plz help me out)

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I want to know following:
I have an applet which has socket listener and socket sender. Here is my code:

import java.awt.*;
import java.net.*;
import java.io.*;
import java.lang.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
public class K_ClientSocket1 extends Applet implements ActionListener{
public TextArea ta;
private Button send;
private Button rcv;
public String g;
Socket localSocket;
PrintWriter out;
int Port = 11200;
//String IpAddr = "10.3.158.172";
int localPort;
//char []buffer = new char[4000];

public void init() {
//create GUI's
Panel p = new Panel();
p.setLayout(new FlowLayout());
ta = new TextArea();
p.add(ta);
add(p,"North");
send = new Button("Send Message");
send.addActionListener(this);
rcv = new Button("Listener");
rcv.addActionListener(this);
Panel p1 = new Panel();
p1.setLayout(new FlowLayout());
p1.add(send);
p1.add(rcv);
add(p1,"Center");
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == send)
{
//Send Data
try {
localSocket = new Socket(InetAddress.getLocalHost(),Port);
localPort = localSocket.getLocalPort();
String s = "Message:Konica Client";
//Setup data stream in and out of socket
out= new PrintWriter(localSocket.getOutputStream());
//send signature to server
out.println(s);
out.flush();
out.close();
localSocket.close();
}
catch(UnknownHostException uh)
{
System.out.println("Unknown Host Exception");
}
catch (IOException ioe)
{
System.out.println("Input/output Exception");
}
}
if(e.getSource() == rcv)
{
//declaration
ServerSocket echoServer = null;
Socket clientSocket = null;
BufferedReader in;
String g;
int sPort;
sPort = localPort;
try
{
echoServer = new ServerSocket(sPort);
clientSocket = echoServer.accept();
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream( )));
// while socket open
while ((in.readLine())!= null)
{
g = in.readLine();
ta.appendText(g);
}
clientSocket.close();
}
catch (IOException ioe)
{
System.out.println("Input/output error");
}
}
}
}
When I try to send to server, i can able to send. But when i try to listen or receive from the server i am getting following errors:
com.ms.security.SecurityExceptionEx[K_ClientSocket1.init]: cannot access 7689
at com/ms/security/permissions/NetIOPermission.check (NetIOPermission.java)
at com/ms/security/PolicyEngine.deepCheck (PolicyEngine.java)
at com/ms/security/PolicyEngine.checkPermission (PolicyEngine.java)
at com/ms/security/StandardSecurityManager.chk (StandardSecurityManager.java)
at com/ms/security/StandardSecurityManager.checkListen (StandardSecurityManager.java)
at java/net/ServerSocket.<init> (ServerSocket.java)
at java/net/ServerSocket.<init> (ServerSocket.java)
at K_ClientSocket1.init (K_ClientSocket1.java:73)
at com/ms/applet/AppletPanel.securedCall0 (AppletPanel.java)
at com/ms/applet/AppletPanel.securedCall (AppletPanel.java)
at com/ms/applet/AppletPanel.processSentEvent (AppletPanel.java)
at com/ms/applet/AppletPanel.processSentEvent (AppletPanel.java)
at com/ms/applet/AppletPanel.run (AppletPanel.java)
at java/lang/Thread.run (Thread.java)
Please let me know,
Thanks,
angela
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, Angela Jessi
now u r getting these exceptions because the VM of browser have security policy which doesn't let applet to write on the socket
so u have two solutions
either u have to make ur applet signed
or
to change the security policy of VM
tell me in detail what u actually want
ok
bye
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maaten,
Actually Ihave project. I have to develop client side.I have to open the socket and get the value from Printer and update it in my applet.
So I opened the socket with applet. As you know I am getting above errors. Which is the easiest way to make it either to go for signed applet and to change the security policy of VM
I really appreciate your help,
Thanks again,
Plz let me know
Angela
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic