eire_ireland
asked on
simple annoying error
Below are four files, a server a client that calls a file a html page to load the client. This was working perfectly yesturday and i dont think I changed anything, just cant see what im doing wrong, when the client loads it only sends the command once and after this it just seems to hang, could someone compile it and see whats wrong, ive spent a few days tryin to figure this out.
Cheers
/*******************html file********************** ********** ****/
<html>
<applet code="clientz.class" width=300 height=225>
</applet>
</html>
/************************* ********** clientz.ja va******** ********** ********** ********** /
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.applet.*;
public class clientz extends Applet {
private Button showRock;
private Button showScissors;
private Button showPaper;
private Button enter;
private Frame r;
private Frame s;
private Frame p;
private Frame inter;
Label prompt;
TextField input;
String name;
public void init()
{
prompt = new Label("Enter your name: ");
input = new TextField(20);
add(prompt);
add(input);
String inter = "Enter";
enter = new Button(inter);
add(enter);
String r = "Rock";
showRock = new Button(r);
add(showRock);
String s = "Scissors";
showScissors = new Button(s);
add(showScissors);
String p = "Paper";
showPaper = new Button(p);
add(showPaper);
}
public boolean action(Event e, Object o){
name = (input.getText());
if(e.target == showRock)
r = new clientFrame("Rock" , 1);
/* if(e.target == showScissors)
s = new clientFrame("Scissors" , 1);
if(e.target == showPaper)
p = new clientFrame("Paper" ,1);
if(e.target == enter){
inter = new clientFrame(name ,0);
name = (input.getText());
repaint();
}
*/
return true;
}
/*public void paint(Graphics g)
{
super.paint(g);
g.drawString("Welcome" + name , 100 , 100);
}*/
}
/************************* *********c lientFrame .java***** ********** ********** ******/
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.applet.*;
class clientFrame extends Frame{
private Button a,b,c,d;
TextArea display;
Socket client = null;
InputStream input = null;
//input.read();
PrintWriter output = null;
public clientFrame(String message, int num){
super("clientFrame");
display = new TextArea(20 , 10);
add("Center" , display);
resize(300 , 150);
show();
try{
//eiving message from server and connecting to server
client = new Socket(InetAddress.getLoca lHost(),50 04);
input = client.getInputStream();
display.appendText("\nCrea ted input Stream\n");
display.appendText("\nText from server is\n\t");
char c;
while((c = (char) input.read() ) != '\n')
display.appendText(String. valueOf( c ));
// display.appendText("\n");
//sending message to server
// if(num == 0){
//output = new PrintWriter(new OutputStreamWriter(client. getOutputS tream()), true);
//output.println(message+" Player: ");
// }
//else{
//output = new PrintWriter(new OutputStreamWriter(client. getOutputS tream()), true);
// output.println(message);
//}
}
catch(IOException e) {
e.printStackTrace();
}
finally {
try {
input.close();
output.close();
client.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
public boolean handleEvent( Event e )
{
if( e.id == Event.WINDOW_DESTROY ){
hide();
dispose();
System.exit( 0 );
}
return super.handleEvent( e );
}
}
/******************Serverz .java***** ********** ********** ****/
import java.awt.* ;
import java.awt.event.* ;
import java.net.* ;
import java.io.* ;
import javax.swing.* ;
import java.util.*;
public class Serverz extends Frame
{
TextArea display ;
public Serverz()
{
super( "Serverz" ) ;
display = new TextArea( 20, 5 ) ;
add( "Center", display ) ;
resize( 300, 150 ) ;
show() ;
}
public void runserverz()
{
ServerSocket serve = null ;
try
{
serve = new ServerSocket( 5004, 1000 ) ;
new ClientThread(serve.accept( ));
}
catch( IOException e )
{
e.printStackTrace() ;
}
} //end while
class ClientThread extends Thread {
private Socket socket;
public ClientThread(Socket socket) {
this.socket = socket;
start();
}
public void run() {
PrintWriter output = null;
BufferedReader input = null;
//PrintWriter output1 = null;
try {
//Sender s = new Sender("sd");
display.setText("\nConnect ion received");
output = new PrintWriter(new OutputStreamWriter(socket. getOutputS tream()), true);
String s = new String( "Has connected Adro\n" ) ;
output.println(s);
display.appendText("\nSent message");
/* //receiving message from client
input = new BufferedReader(new InputStreamReader(socket.g etInputStr eam()));
String buffer = null;
String store = "";
while ((buffer = input.readLine()) != null) {
store += buffer;
}
String[] s2 = new String[5];
StringTokenizer st = new StringTokenizer(store);
int numElements = 0;
if ((numElements = st.countTokens()) > 0) {
int index = 0;
while (st.hasMoreTokens()) {
s2[index++] = st.nextToken();
}
}
display.appendText( "\n" +store );
output = new PrintWriter(new OutputStreamWriter(socket. getOutputS tream()), true);
s = ("Hello Adro\n");
output.println(s);
display.appendText(s);*/
} catch (IOException e) {
e.printStackTrace();
}
/* finally {
//close socket and streams
try {
//input.close();
// output.close();
// socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/
//Socket socket;
/*OutputStream output = null;
OutputStream output1 = null;
OutputStream output_name = null;
BufferedReader input = null;
display.setText( "\nConnection received" ) ;
output = socket.getOutputStream() ;
String s = new String( "Has connected\n" ) ;
for( int i = 0 ; i < s.length() ; ++i )
output.write( ( int )s.charAt( i ) ) ;
display.appendText(
"\nSent message" );*/
//receiving message from client
// input = new BufferedReader( new InputStreamReader( socket.
// getInputStream() ) ) ;
// String buffer = null ;
//String store = "";
// while( ( buffer = input.readLine() ) != null )
// store += buffer;
/* display.appendText( "\n" + s2[0] );
output_name = socket.getOutputStream() ;
String name = new String(" Adro is cool\n" ) ;
for( int i = 0 ; i < name.length() ; ++i )
output_name.write( ( int )name.charAt( i ) ) ;*/
// }
// catch( IOException e )
//{
//e.printStackTrace() ;
//}
}
}
public static void main( String[] args )
{
Serverz main = new Serverz() ;
main.runserverz();
}
}
Cheers
/*******************html file**********************
<html>
<applet code="clientz.class" width=300 height=225>
</applet>
</html>
/*************************
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.applet.*;
public class clientz extends Applet {
private Button showRock;
private Button showScissors;
private Button showPaper;
private Button enter;
private Frame r;
private Frame s;
private Frame p;
private Frame inter;
Label prompt;
TextField input;
String name;
public void init()
{
prompt = new Label("Enter your name: ");
input = new TextField(20);
add(prompt);
add(input);
String inter = "Enter";
enter = new Button(inter);
add(enter);
String r = "Rock";
showRock = new Button(r);
add(showRock);
String s = "Scissors";
showScissors = new Button(s);
add(showScissors);
String p = "Paper";
showPaper = new Button(p);
add(showPaper);
}
public boolean action(Event e, Object o){
name = (input.getText());
if(e.target == showRock)
r = new clientFrame("Rock" , 1);
/* if(e.target == showScissors)
s = new clientFrame("Scissors" , 1);
if(e.target == showPaper)
p = new clientFrame("Paper" ,1);
if(e.target == enter){
inter = new clientFrame(name ,0);
name = (input.getText());
repaint();
}
*/
return true;
}
/*public void paint(Graphics g)
{
super.paint(g);
g.drawString("Welcome" + name , 100 , 100);
}*/
}
/*************************
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.applet.*;
class clientFrame extends Frame{
private Button a,b,c,d;
TextArea display;
Socket client = null;
InputStream input = null;
//input.read();
PrintWriter output = null;
public clientFrame(String message, int num){
super("clientFrame");
display = new TextArea(20 , 10);
add("Center" , display);
resize(300 , 150);
show();
try{
//eiving message from server and connecting to server
client = new Socket(InetAddress.getLoca
input = client.getInputStream();
display.appendText("\nCrea
display.appendText("\nText
char c;
while((c = (char) input.read() ) != '\n')
display.appendText(String.
// display.appendText("\n");
//sending message to server
// if(num == 0){
//output = new PrintWriter(new OutputStreamWriter(client.
//output.println(message+"
// }
//else{
//output = new PrintWriter(new OutputStreamWriter(client.
// output.println(message);
//}
}
catch(IOException e) {
e.printStackTrace();
}
finally {
try {
input.close();
output.close();
client.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
public boolean handleEvent( Event e )
{
if( e.id == Event.WINDOW_DESTROY ){
hide();
dispose();
System.exit( 0 );
}
return super.handleEvent( e );
}
}
/******************Serverz
import java.awt.* ;
import java.awt.event.* ;
import java.net.* ;
import java.io.* ;
import javax.swing.* ;
import java.util.*;
public class Serverz extends Frame
{
TextArea display ;
public Serverz()
{
super( "Serverz" ) ;
display = new TextArea( 20, 5 ) ;
add( "Center", display ) ;
resize( 300, 150 ) ;
show() ;
}
public void runserverz()
{
ServerSocket serve = null ;
try
{
serve = new ServerSocket( 5004, 1000 ) ;
new ClientThread(serve.accept(
}
catch( IOException e )
{
e.printStackTrace() ;
}
} //end while
class ClientThread extends Thread {
private Socket socket;
public ClientThread(Socket socket) {
this.socket = socket;
start();
}
public void run() {
PrintWriter output = null;
BufferedReader input = null;
//PrintWriter output1 = null;
try {
//Sender s = new Sender("sd");
display.setText("\nConnect
output = new PrintWriter(new OutputStreamWriter(socket.
String s = new String( "Has connected Adro\n" ) ;
output.println(s);
display.appendText("\nSent
/* //receiving message from client
input = new BufferedReader(new InputStreamReader(socket.g
String buffer = null;
String store = "";
while ((buffer = input.readLine()) != null) {
store += buffer;
}
String[] s2 = new String[5];
StringTokenizer st = new StringTokenizer(store);
int numElements = 0;
if ((numElements = st.countTokens()) > 0) {
int index = 0;
while (st.hasMoreTokens()) {
s2[index++] = st.nextToken();
}
}
display.appendText( "\n" +store );
output = new PrintWriter(new OutputStreamWriter(socket.
s = ("Hello Adro\n");
output.println(s);
display.appendText(s);*/
} catch (IOException e) {
e.printStackTrace();
}
/* finally {
//close socket and streams
try {
//input.close();
// output.close();
// socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/
//Socket socket;
/*OutputStream output = null;
OutputStream output1 = null;
OutputStream output_name = null;
BufferedReader input = null;
display.setText( "\nConnection received" ) ;
output = socket.getOutputStream() ;
String s = new String( "Has connected\n" ) ;
for( int i = 0 ; i < s.length() ; ++i )
output.write( ( int )s.charAt( i ) ) ;
display.appendText(
"\nSent message" );*/
//receiving message from client
// input = new BufferedReader( new InputStreamReader( socket.
// getInputStream() ) ) ;
// String buffer = null ;
//String store = "";
// while( ( buffer = input.readLine() ) != null )
// store += buffer;
/* display.appendText( "\n" + s2[0] );
output_name = socket.getOutputStream() ;
String name = new String(" Adro is cool\n" ) ;
for( int i = 0 ; i < name.length() ; ++i )
output_name.write( ( int )name.charAt( i ) ) ;*/
// }
// catch( IOException e )
//{
//e.printStackTrace() ;
//}
}
}
public static void main( String[] args )
{
Serverz main = new Serverz() ;
main.runserverz();
}
}
What do you have to do to reproduce the error?
Are you getting any exceptions?
> client loads it only sends the command once and after this it just seems to hang
Do you mean ti freezes?
Are you sure it is not blocking while waiting to read data?
> client loads it only sends the command once and after this it just seems to hang
Do you mean ti freezes?
Are you sure it is not blocking while waiting to read data?
(assuming that you are using the code below)
s = new clientFrame("Scissors" , 1);
if(e.target == showPaper)
p = new clientFrame("Paper" ,1);
if(e.target == enter){
inter = new clientFrame(name ,0);
you create new frames all the time. Not sure if you need this or you need to have the input in the same frame.
s = new clientFrame("Scissors" , 1);
if(e.target == showPaper)
p = new clientFrame("Paper" ,1);
if(e.target == enter){
inter = new clientFrame(name ,0);
you create new frames all the time. Not sure if you need this or you need to have the input in the same frame.
ASKER
Run the client and server.
When you click on rock, it sends "has connected adro" to the client, if you click on rock again it does nothing, it should send rock again to the client. Im not getting any exceptions, clientFrame gets loaded twice when rock is clicked, but this was always the case and it never affected it.
When you click on rock, it sends "has connected adro" to the client, if you click on rock again it does nothing, it should send rock again to the client. Im not getting any exceptions, clientFrame gets loaded twice when rock is clicked, but this was always the case and it never affected it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I also forgot to add the code that closes the connections and input/output streams on the client...
ASKER
Cheers, ill give that a go
ASKER
That does'nt seem to handle two seperate connections to the server in a seperate thread like the original?
ASKER
Cheers for the reply, how can I modify the code to accept only two connections from different clients?
Create a new thread on the server to accept the second client's connection. Each client should run on a separate thread on the server side.
ASKER
Is there any way I can access the value(rock, scissors, or paper) sent to the server. What I mean is compare each value on the server even though there is a seperate thread for each client?
Store them in an instance variable and then compare/contrast them when you read the other value.
For example lets say that you have on the server a String variable "myValue". When client1 sends a value then you assign it to the "myValue" variable:
myValue = input.readLine();
Then the second time you read something from the second client and you do:
s = input.readLine();
you compare it with the myValue variable:
if (s.equals(myValue))
{
// do something.
}
Make sure to synchronize the variable if more than one threads have access on it.
For example lets say that you have on the server a String variable "myValue". When client1 sends a value then you assign it to the "myValue" variable:
myValue = input.readLine();
Then the second time you read something from the second client and you do:
s = input.readLine();
you compare it with the myValue variable:
if (s.equals(myValue))
{
// do something.
}
Make sure to synchronize the variable if more than one threads have access on it.
ASKER
ok, that seems fine, will this work for accepting two connections?
somethiing like....
thread1 = new ClientThread(serve.accept( ));
thread2 = new ClientThread(serve.accept( ));
public ClientThread(Socket socket) {
this.socket = socket;
System.out.println("start" );
thread1.start();
System.out.println("stop") ;
thread2.start
somethiing like....
thread1 = new ClientThread(serve.accept(
thread2 = new ClientThread(serve.accept(
public ClientThread(Socket socket) {
this.socket = socket;
System.out.println("start"
thread1.start();
System.out.println("stop")
thread2.start
Yes it will :)
.. although you actually create two threads for each client connection... a bit unecessary unless you indeed need two threads.
Take a look here: http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html
You need an algorithm like: while (true) {
accept a connection ;
create a thread to deal with the client ;
end while
Keep looping and wait for connections from client. Something like:
while (true) {
Socket s = serve.accept();
Thread t = new ClientThread(s);
t.start();
}
You need an algorithm like: while (true) {
accept a connection ;
create a thread to deal with the client ;
end while
Keep looping and wait for connections from client. Something like:
while (true) {
Socket s = serve.accept();
Thread t = new ClientThread(s);
t.start();
}