Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Client Applet- server application

 
Angela Jessi
Ranch Hand
Posts: 428
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
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.
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
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
ANgela
 
Selva P
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic