wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes What's wrong here! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "What Watch "What New topic
Author

What's wrong here!

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi,
I wrote on applet which is as follows in which I am trying to use socket to connect to server. In TextField1 whatever i add , i want to write this string to server and want to get response back in string 2.
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
public class MyApplet extends Applet implements ActionListener{
Socket localSocket;
PrintWriter out;
BufferedReader in;
BufferedReader kbdInput;
String s;
String t1,t2;
private Button b;
public void init(){
setLayout(new FlowLayout());
TextField t1 = new TextField(20);
t1.setText("Please Enter the value here");
add(t1);
TextField t2 = new TextField(20);
add(t2);
b = new Button("Connect");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b)
//Create a socket
try {
localSocket = new Socket("10.3.158.222",8192);
//Setup data stream in and out of socket and from KeyBoard
in = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
out= new PrintWriter(localSocket.getOutputStream());
//While we have a connection
while(true)
{
//Read Texfield value
out.println(t1);
//flush the buffer if not full!
out.flush();
// read incoming string from socket
//System.out.println(in.readLine());
t2 = in.readLine();
}
}
catch(UnknownHostException unc)
{
System.out.println("Connection why not connected");
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
}
}
Please let me know what is wrong here in this code. I can compiled but when I run it, when i entered the value and click the button, i am not getting response in text2.
Thanks in advance,
ANgela
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
What exception are you getting in your Java Console? I have a feeling that you are getting a security exception but, you need to let me know if that's the case.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi Carl,
Thanks a lot for ur response.
I am getting following in JAva console:
com.ms.security.SecurityExceptionEx[MyApplet.actionPerformed]: cannot access "10.3.158.222":8192
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.chkex (StandardSecurityManager.java)
at com/ms/security/StandardSecurityManager.checkConnect (StandardSecurityManager.java)
at java/net/Socket.<init> (Socket.java)
at java/net/Socket.<init> (Socket.java)
at MyApplet.actionPerformed (MyApplet.java:34)
at java/awt/Button.processActionEvent (Button.java)
at java/awt/Button.processEvent (Button.java)
at java/awt/Component.dispatchEventImpl (Component.java)
at java/awt/Component.dispatchEvent (Component.java)
at java/awt/EventDispatchThread.run (EventDispatchThread.java)
Please let me know,
Thanks,
Angela
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
I thought so, one of the sandbox restrictions on an Applet is that it can only communicate back to the same server that it originated from. Check the code barn for Hello Server Applet which reads a file off the server. Establishing your connection would be basically the same.
Hope this helps

[This message has been edited by Carl Trusiak (edited January 08, 2001).]
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi carl,
Thanks again for your reply.
I checked the code in Code Barn. I have following questions in my mind:
1) I have to use only URL method.Can I use socket methods
2) I don't want to read the file from server, but I want to pass string to the server and i want to get back the string from server.In URL you can't do that Am I right?
3) What is sand box restriction?
Please let me know,
Thanks again
ANgela

Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
1) No, you can use a socket as long as it connects back to the same server
2)You can do anything with the socket that you can do with any socket
3) The boundrys you have to 'play in' Applet Security FAQ
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi Carl,
I really appreciate ur response.
Now I checked the code in Code Barn. There they have used URL. In my code I have used Socket. SO would please let me know where i can implement sandbox security in my code?
Please help me out ..
Thanks a bunch again,
Angela
sunil choudhary
Ranch Hand

Joined: Nov 10, 2000
Posts: 141

Hello Carl I am modifying Angela's code a bit to get it working on the local host .The error is "Connection Refused:NO further information "
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
//*applet code=MyApplet.class height=400 width=400 ></applet//>
public class MyApplet extends Applet implements ActionListener{
Socket localSocket;
PrintWriter out;
BufferedReader in;
BufferedReader kbdInput;
String s;
String t1,t2;
private Button b;
public void init(){
setLayout(new FlowLayout());
TextField t1 = new TextField(20);
t1.setText("Please Enter the value here");
add(t1);
TextField t2 = new TextField(20);
add(t2);
b = new Button("Connect");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b)
//Create a socket
try
{
localSocket = new Socket("127.0.0.1",8182);
//Setup data stream in and out of socket and from KeyBoard
in = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
out= new PrintWriter(localSocket.getOutputStream());
//While we have a connection
while(true)
{
//Read Texfield value
out.println(t1);
//flush the buffer if not full!
out.flush();
// read incoming string from socket
//System.out.println(in.readLine());
t2 = in.readLine();
}
}
catch(IOException ioe)
{
System.out.println("I am HERE"+ioe.getMessage());
}
}
}

[This message has been edited by sunil choudhary (edited January 09, 2001).]
[This message has been edited by sunil choudhary (edited January 09, 2001).]


"Learning is weightless, a treasure you can always carry easily." -Chinese Proverb
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
That exception occurs when to try to make a socket connection and therre isn't a server running on that server listening to that port. A socket program requires 2 components, a server and a client. Take a look at the Custom networking tutorial
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi Sunil & Carl,
Thanks a lot for ur reply.
Carl, Let me more specific here:
I already have server sitting on serverside. We created that server program with C code. SO that is why I have created only this above client side program.
I embedded MyApplet.class applet in Html file called Advftrs.html. Then I copied MyApplet.class,MyApplet.java andadvftrs.Html(in which I have MyApplet.class) file in server.
Now from Client side when I typed the address ,10.3.158.222/Advftrs.htm, i can't have any error in my applet but as well I can't pass string to server and can't get response back from server
SO IS there anything wrong in my code OR I have to set something else?
Please let me know
It's very urgent for me.
Thanks
Angela
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The server is running? What is it's requirement for a request?
The thing I see with this code is your use of the textfield.
Try these changes

This works if the server is simply a tcp/ip and sends a single string in response to a single string request. If the server requires anything special for communication(like a carrage return line feed to indicate the end of the request) ensure your program sends this. If your server returns more than a single string, loop and add this to a component that can handle it like textarea. You will loop until the server returns what it uses to indicate the end of the response.
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Sunil,
Put all class files and html files in one directory , then you will not get Connection Refused ERROR!
Thanks angela
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Carl,
Thanks again for your response.
I will definitely try the code changes you have sent.
Now I have other issue here:
I have IIS web server in My Machine.The IP address is 10.3.158.172 and port I have assinged 2000.FOr this I have created client program(Indirectly server program) as an applet.I have stored Applet and Html file in directory fo IIS server.
Here is code for client Program:
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.TextField;
import java.lang.*;
public class MyClient extends Applet implements ActionListener{
Socket localSocket;
PrintWriter out;
BufferedReader in;
String s;
private Button b;
private TextField t1,t2;
public void init(){
setLayout(new FlowLayout());
t1 = new TextField(20);
t1.setText("Please Enter the value here");
add(t1);
t2 = new TextField(20);
add(t2);
b = new Button("Connect");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b)
//Create a socket
try {
localSocket = new Socket("10.3.158.172",2000);
//Setup data stream in and out of socket and from KeyBoard
in = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
out= new PrintWriter(localSocket.getOutputStream());
//While we have a connection
while(true)
{
// get textfield value
// String s = t1.getText();
//Read Texfield value
out.println(t1.getText());
//flush the buffer if not full!
out.flush();
// read incoming string from socket
//System.out.println(in.readLine());
String line = in.readLine();
t2.setText(line);
// t2 = in.readLine();
}
}
catch(UnknownHostException unc)
{
System.out.println("Connection why not connected");
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
}
}

Another Program i.e. Server program I have created as an application. Here is code:
import java.io.*;
import java.net.*;
public class MyServer
{
//declare local variables
ServerSocket echoServer = null;
Socket clientSocket = null;
BufferedReader in;
PrintWriter out;
String s;
public MyServer(){
// fire up the server, catching IOException in case of problems
try
{
echoServer = new ServerSocket(2000);
}
catch (IOException e)
{
System.out.println(e.getMessage( ));
}
if (echoServer != null)
{
System.out.println("Server listening on port 2000");
}
// create a new socket for incoming transactions and Streams
// to handle data
try
{
clientSocket = echoServer.accept( );
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream( )));
out = new PrintWriter(clientSocket.getOutputStream());
// if socket created, print out details
while (true)
{
//System.out.println("Message Received From:" + clientSocket.getInetAddress( ) + "\nFrom Port: " + clientSocket.getPort( ));
// send input to output!
s = in.readLine();
System.out.println("I Hava Read the line:" + s);
out.println(s);
out.flush();
}
}
catch (IOException e)
{
System.out.println(e.getMessage( ));
}
}

public static void main(String[] args){
MyServer ms = new MyServer();
}

}
I have IIS web server and client both in my machine.So this like server to server (indirectly client to server)
They both compiled successfully. After that I typed in my IIS IP address : 10.3 158.172/MyClient.html file in browser.
I got an applet in browser window. Then on MS prompt I run MyServer program. It's prints Port 2000 is listening.
Now In browser I add the string in TextBox1 and click Connect.Nothing happens means i can not get that string in server program and can't pass string back to client program in Textbox2.When I checked Java console: It's says connection refused!(though all files in same directory)
Please let me know
ANgela

[This message has been edited by Angela Jessi (edited January 09, 2001).]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
IIS could be what is refusing the connection to that port. Try running the server on a machine that doesn't have IIS on it. Put your applet code on that machine and in your html, change the code base to point to that machine. Hopefully this will help. If your still having problems try running your applet as an application. That will remove any problem with the sandbox restriction on applets.

From there you can easily work out the bugs that may be preventing connections and then move it to the more restrictive environment.
maateen ashraf
Ranch Hand

Joined: Jan 08, 2001
Posts: 122
no way to get connected through an applet with server using socket in between them....
if any method is there so plz reply...
thanx in advance
Neeraj Thakkar
Ranch Hand

Joined: Dec 06, 2000
Posts: 80
Hi Angela,
Working on ur problem.will reply soon. In between if u get something kindly mail me at:
neeraj_thakkar@rediffmail.com
neeraj-thakkar@usa.net
NEERAJ THAKKAR


Neeraj Thakkar<br />"Try to get what you like,Else you will be forced to like what you get."
 
 
subject: What's wrong here!
 
Similar Threads
Successful Applet Socket Connection
What is wrong here!
Applets & Sockets .The Security Issue Of JAVA.
Client Applet- server application
What's wrong here!