aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Successful Applet Socket Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Successful Applet Socket Connection" Watch "Successful Applet Socket Connection" New topic
Author

Successful Applet Socket Connection

Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
I ran the following, The server code ran on a Linux machine with Apache and the Browser was on my Win2000 machine on the network
Server (Most of the code is Angela's)

The Applet

The Html

This worked successfully here. Angela, if you still have problems, it could be IIS interferring
Hope this helps


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
Hi! Well I d like to know how printwriter scores over data output / Input Streams.. Becoz I use de Data Streams..
Tanx.
Regds
Gautham Kasinath
U R Not Alone


"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at : http://www.geocities.com/gkasinath
Ben Roy
Ranch Hand

Joined: Nov 01, 2000
Posts: 70
I can't tell you how happy I am that you posted this! I was just cruising over to this forum to ask for someone to post a simple example of a server that launches and then waits for incoming connections, as well as a simple example of a client to connect. This is perfect! I hope you know a lot about this stuff because I'm sure I'll soon have many questions. Thanks!
inigo recio
Greenhorn

Joined: Jul 13, 2001
Posts: 1
Hi All,
I ran your code but i got the famous securiry exceptioin.
i ran both the server and client on the same machine.
i thought there wasn't any problem to do this.
i've tried more examples and all of them raise the exception.
do you know which is the problem?
do i have to run the example with 2 machines?
thanks,
Inigo.
Rakesh Kumar
Ranch Hand

Joined: May 30, 2006
Posts: 63
Hi friends,

can you please help me by posting a simple client and server example to unit test socket connection from my applet and server in linux(apache).Very urgent.
Thanks in advance
rakesh
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
What happened when you ran the code posted above? Or is the problem turning that into a unit test?


Ping & DNS - my free Android networking tools app
Rakesh Kumar
Ranch Hand

Joined: May 30, 2006
Posts: 63
no actually ,i tried to run server programme and client applet with tomcat server.But i am not able to connect to server socket programme.I will give the code as below.

client
========
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;
private PrintWriter m_pw;
private Label m_labelDebug;
public void init(){
m_labelDebug=new Label("debug message:");
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);
add(m_labelDebug);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b)
//Create a socket
try
{
System.out.println("MyClient.actionPerformed() 1> "+InetAddress.getByName(this.getCodeBase().getHost()));
//localSocket = new Socket(this.getCodeBase().getHost(), 8080);//2000);
localSocket = new Socket(this.getCodeBase().getHost(), 2000);
//Setup data stream in and out of socket and from KeyBoard
in = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
out= new PrintWriter(localSocket.getOutputStream());
System.out.println("MyClient.actionPerformed() 2 "+new String(t1.getText().getBytes()));
//While we have a connection
// get textfield value
// String s = t1.getText();
//Read Texfield value
out.println(t1.getText().getBytes());
System.out.println("MyClient.actionPerformed() 2.1 in===="+in);
//flush the buffer if not full!
out.flush();
System.out.println("MyClient.actionPerformed() 3 ");
// read incoming string from socket
//System.out.println(in.readLine());
// String line = in.readLine();//readLine();
char[] buffer=new char[100];
if(in.ready() == true)
{
System.out.println("MyClient.actionPerformed()3.1");
// Read length
in.read(buffer,0,2);

t2.setText(buffer.toString());//line);
System.out.println("MyClient.actionPerformed() 4 ");
}
else
{
System.out.println("MyClient.actionPerformed() 5");
}
}
catch(UnknownHostException unc)
{
System.out.println("Connection why not connected: unknown host exp..");
m_labelDebug.setText("m11:"+unc);
}
catch(IOException ioe)
{
System.out.println("IO exp.:"+ioe.getMessage());
m_labelDebug.setText("m22:"+ioe);
}
}
public static void main( String[] args )
{
MyClient applet = new MyClient();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle( "Socket Test" );
frame.getContentPane().add( applet , BorderLayout.CENTER );
applet.init();
applet.start();
frame.setSize( 460 , 360 );
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation( ( d.width - frame.getSize().width ) / 2 ,
( d.height - frame.getSize().height ) / 2 );
frame.setVisible( true );
}
}



server
==============
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
{
System.out.println("MyServer.MyServer() xxxxxxxxxxxxxxxxxxxxxx");
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
{
System.out.println("MyServer.MyServer() 1");
clientSocket = echoServer.accept( );
System.out.println("MyServer.MyServer() 2 accepted ");
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream( )));

out = new PrintWriter(clientSocket.getOutputStream());
System.out.println("MyServer.MyServer() 3 in/out is created");
// 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();
System.out.println("MyServer.MyServer() after read write");
}

}
catch (IOException e)
{
System.out.println("server exp:"+e.getMessage( ));
}
}

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


I tried to run the Myserver.java class with eclipse in tomcat server?But not able to write ir read.1st i want to test in my machine and later in linux machine also.Please advice me since i am new to all thses.

Thanks in advance
rakesh
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
Please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is, making it less likely that people will do so. Please edit your post accordingly.

Also, if there are any error messages, post them along with the stack trace.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Successful Applet Socket Connection