Advertisement

[java] Sockets over the net

Started by October 26, 2000 02:07 PM
6 comments, last by Smoo 24 years, 2 months ago
Well I'm a little confused to what I should do. this link: http://www.javasoft.com/docs/books/tutorial/networking/sockets/clientServer.html has an example on how sockets work which is great except that it has both client and server as applications. Now if I wanted to have the client as an applet, it's giving me security errors. After a short debugging session when creating a new socket it throws errors on me and since i'm new to sockets I'm lost. If anyone could point out what I need to do to get it fixed I would really appreciate it. Thanks, Smoo Ps. Blah, title seems a bit redundant, of course sockets work over the net, I meant through applets. Edited by - Smoo on 10/26/00 2:09:55 PM
I believe that the standard applet security sandbox prevents the applet from connecting to servers other than the on the IP address that the applet originated from.
Advertisement
Ya, you should look into signed cab files for allowing socket stuff to go down in an applet, oh ok, here is how it''s done.. I think its basically right, well it worked for me before.

I suggest putting the below stuff in a batch file, that way you don''t have to type a million things everytime.

REM Start of .bat file
cabarc n yourcabfilename.cab *.class
setreg 1 true
makecert -sk MyKeyName -n "CN=WhateverName Here" MyTestCert.cer
cert2spc MyTestCert.cer MyTestCert.spc
signcode -j javasign.dll -jp low -spc MyTestCert.spc -k MyKeyName yourcabfilename.cab
REM End of .bat file

now, I had to move the following files into the directory of the
class files where I wanted to create the signed cab file, probably cause I had my paths setup wrong. Jeez, I cant remember where I found them... I think they came with J2SE from www.java.sun.com

cert2spc.exe
javasign.dll
makecert.exe
setreg.exe


That brings me to a question actually, someone told me that cab files could only be used in IE, but not in netscape. Is there a way to create signed JAR files so sockets can be opened?

ao
Play free Java games at: www.infinitepixels.com
I was under the impression however that I wouldn''t need signed stuff if the applet was communicating to the same host, only if it were a different host.

Am I wrong to believe so or do I still have to create a signed cab/jar?
it would be best if you just printed your error and relevant parts of your source code.
Here is the client part of the code, and I get a com.ms.security.SecurityExceptions[Host]: cannot access "127.0.0.1":12358

I tried with a few things, like localhost, and with the actual IP of my machine, but same result...

public class ChatClient extends Applet
{
Socket socket1;
PrintWriter out;
BufferedReader in;
String serverName;
String line;

public void init()
{
socket1 = null;
out = null;
in = null;
serverName = getCodeBase().getHost();
line = "";

initComm();
}

public void initComm ()
{
try
{
socket1 = new Socket(serverName, 12583);
out = new PrintWriter(socket1.getOutputStream(), true);
in = new BufferedReader (new InputStreamReader (socket1.getInputStream()));
} catch (UnknownHostException e)
{
line = "Don''t know about host: " + serverName;
errorEnd = true;
} catch (IOException e)
{
line = "Couldn''t get I/O for connection to: " + serverName;
errorEnd = true;
}
}


Thats not all of it, but its what I need to fix for now. I get the error when it its the "socket1 = new Socket(serverName, 12583);" part

Thanks
DrG
Advertisement
.
quote: Original post by Anonymous Poster

Here is the client part of the code, and I get a com.ms.security.SecurityExceptions[Host]: cannot access "127.0.0.1":12358

I tried with a few things, like localhost, and with the actual IP of my machine, but same result...


Yes, well. 127.0.0.1 _is_ localhost and this won''t work cuss the sand box won''t allow it. Use you real IP address and make sure your Server uses it too (not localhost). Check your firewall and hosts files (if you have them), and make sure ''yourServerName'' is pointing @ the correct IP address. Also sometimes firewalls won''t allow high port numbers....

This topic is closed to new replies.

Advertisement