[4530] in java-interest
Socket connection in an applet
daemon@ATHENA.MIT.EDU (trk@bilbo.wipsys.soft.net)
Fri Jan 5 03:00:14 1996
From: trk@bilbo.wipsys.soft.net
To: java-interest@java.sun.com (jig)
Date: Fri, 5 Jan 1996 11:30:27 -0500 (GMT)
**********
I am reposting this..
please help me out..
**********
Hi !
I have a trivial applet which has to contact the SMTP
service on its originating host and do the following:
0. create a socket to talk to port 25 on the originating host
1. read the initial message given by the server
2. Say 'HELO' to the server
3. read the server's response
4. Say 'QUIT' to the server
5. finally read the last message from the server.
6. close the socket
Things are going fine when I use appletviewer. When I use
Netscape ( 2b3 on Solaris 2.4) the applet is not able to
read any message given by the SMTP server. It finds that
the first two messages are null.
I use readLine on the DataOutputStream created using the
getOutputStream() on the connected socket.
I am not getting any Exceptions..
BTW, if I run it as an application ( after modifying the code
below) things are quite ok.
Here is the code :
================================================
import java.net.*;
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.applet.*;
class SmtpServ {
Socket smtper;
DataInputStream readable;
DataOutputStream writable;
String out;
public SmtpServ(String host, int port)
throws IOException
{
smtper = new Socket(host, port);
readable = new DataInputStream(smtper.getInputStream());
writable = new DataOutputStream(smtper.getOutputStream());
}
public void ComposeAndWriteMsg(String msg)
throws IOException
{
char buf[] = msg.toCharArray();
int len = msg.length();
for(int i =0; i < len ; ++i) {
writable.write(buf[i]);
}
writable.write('\015');
writable.write('\012');
writable.flush();
}
public String[] InitDialog()
throws IOException
{
String results[] = new String[2];
results[0] = readable.readLine();
ComposeAndWriteMsg(new String("HELO"));
results[1] = readable.readLine();
return results;
}
public String QuitDialog()
throws IOException
{
ComposeAndWriteMsg(new String("QUIT"));
String result = readable.readLine();
smtper.close();
return result;
}
}
public class netest extends Applet {
SmtpServ a;
String res1[];
String res2;
public void init()
{
res1 = new String[2];
res2 = new String();
try {
a = new SmtpServ("bilbo", 25);
res1 = a.InitDialog();
res2 = a.QuitDialog();
} catch(Exception e){
// res2 = new String(e.getMessage());
// If I uncomment the above, applet not initailized
// error occurs.
}
}
public void paint(Graphics g)
{
if(res1[0] != null)
g.drawString(res1[0], 30, 40);
else
g.drawString("One is null", 30, 40);
if(res1[1] != null)
g.drawString(res1[1], 30, 80);
else
g.drawString("Two is null", 30, 80);
if(res2 != null)
g.drawString(res2, 30, 120);
else
g.drawString("Three is null", 30, 120);
}
}
==========
Netscape shows
One is null
Two is null
That's all.. nothing about res2
========
I suppose I made a silly mistake somewhere. Can someone tell
me how to make this work?
Thanks in adv..
Cheers
TRK
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com