This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Applets and the fly likes Client Applet- server application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Client Applet- server application" Watch "Client Applet- server application" New topic

Client Applet- server application

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi All,
I have IIS web server in My Machine.The IP address is 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.
Here is code for client Program:
import java.applet.Applet;
import java.awt.*;
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");
t2 = new TextField(20);
b = new Button("Connect");
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b)
//Create a socket
try {
localSocket = new Socket("",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
// get textfield value
// String s = t1.getText();
//Read Texfield value
//flush the buffer if not full!
// read incoming string from socket
String line = in.readLine();
// t2 = in.readLine();
catch(UnknownHostException unc)
System.out.println("Connection why not connected");
catch(IOException ioe)

Another Program i.e. Server program I have created as an application. Here is code:
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
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
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);
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
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 ican 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!
Please let me know
Selva P

Joined: Oct 24, 2000
Posts: 11
First I would like to clarify that this communication might have failed due to two reasons.
1) An applet can create a socket back to the server from which it was called.
2) your application that you are runnning is no way related with the iis server. Your should have written that as a servlet.So that it is possible for the applet to communicate with the servlet.Since the servlet executes within the HTTP space it become possible.Where as the application program that you are running does not share the Http Space.

Sun Certified Java 2 Programmer
I agree. Here's the link:
subject: Client Applet- server application
It's not a secret anymore!