• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Successful Applet Socket Connection

 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you ran the code posted above? Or is the problem turning that into a unit test?
 
Rakesh Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic