This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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)
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
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
posted
0
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
posted
0
Hi friends,
can you please help me by posting a simple client and server example to unittest socket connection from my applet and server in linux(apache).Very urgent.
Thanks in advance
rakesh
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35249
7
posted
0
What happened when you ran the code posted above? Or is the problem turning that into a unit test?
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);
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");
}
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: 35249
7
posted
0
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.