By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Server and Log File

James Bond
P: 12
Hi there
I am developing a client/server app in Java. I have created a client and a server
via the java.net package. But now i want that ach time the client logs on to the server, a log file is created.

For this i created another class that is used only to read data from the cilent socket and implemented runnable in this class.

Now this reading class is called "SocketReader", and is coded as follows:

import java.io.*;
import java.net.*;

public class socketReader implements Runnable{

BufferedReader readMessage;
String message="";

synchronized String socketReader(Socket client){
Thread t=new Thread(this);
t.start();

public void run(){
try{


//while(true){
readMessage=new BufferedReader(new InputStreamReader
(client.getInputStream()));
message=readMessage.readLine();
readMessage.close();
//}
}
catch (IOException ioex){
System.out.println("Exception: "+ioex);
}
} // run () ends here
return message;
} // constructor ends here
} // class ends here

The problem is that it gives an error message.
Wats going wrong. i've even tried to declare the run() outside the constructor.
Kindly reply
Jan 24 '07 #1
Share this Question
Share on Google+
11 Replies


10K+
P: 13,264
Hi there
I am developing a client/server app in Java. I have created a client and a server
via the java.net package. But now i want that ach time the client logs on to the server, a log file is created.

For this i created another class that is used only to read data from the cilent socket and implemented runnable in this class.

Now this reading class is called "SocketReader", and is coded as follows:

import java.io.*;
import java.net.*;

public class socketReader implements Runnable{

BufferedReader readMessage;
String message="";

synchronized String socketReader(Socket client){
Thread t=new Thread(this);
t.start();

public void run(){
try{


//while(true){
readMessage=new BufferedReader(new InputStreamReader
(client.getInputStream()));
message=readMessage.readLine();
readMessage.close();
//}
}
catch (IOException ioex){
System.out.println("Exception: "+ioex);
}
} // run () ends here
return message;
} // constructor ends here
} // class ends here

The problem is that it gives an error message.
Wats going wrong. i've even tried to declare the run() outside the constructor.
Kindly reply
Please use code tags next time when posting code.
Do not put the run method inside the constructor.
What error message do you get when you have the run method outside the constructor?
Jan 24 '07 #2

James Bond
P: 12
Please use code tags next time when posting code.
Do not put the run method inside the constructor.
What error message do you get when you have the run method outside the constructor?
Thanks for replying!!
The error msg that i recieve is that:
illegal start of expression in public void run(). It points on the public.
Jan 24 '07 #3

10K+
P: 13,264
Thanks for replying!!
The error msg that i recieve is that:
illegal start of expression in public void run(). It points on the public.
Sorry for the delay, I'd got disconnected. Post the code that you have that has the run method outside the constructor

Remember the code tags.
Jan 24 '07 #4

10K+
P: 13,264
You might want to have a look at this thread too.
Jan 24 '07 #5

James Bond
P: 12
You might want to have a look at this thread too.
Thanks for replying again.
GOOD NEWS : I got the class compiled.
The mistake i was doing was that i wasnt passing the value of the argument of the constructor to a local instance variable. and it was giving me an error msg "Go Hang Yourself".
But now i have made that change. The New code looks like this:



Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class socketReader implements Runnable{
  5.  
  6.     BufferedReader readMessage;
  7.     String message="";
  8.     Socket client;
  9.  
  10.     synchronized String socketReader(Socket client){
  11.  
  12.         this.client=client;
  13.         Thread t=new Thread(this);        
  14.         t.start();
  15.  
  16.         return message;                    
  17.     }
  18.  
  19.  
  20.  
  21.         public void run(){
  22.             try{
  23.  
  24.  
  25.                 //while(true){
  26.                 readMessage=new BufferedReader(new InputStreamReader(client.getInputStream()));
  27.                 message=readMessage.readLine();
  28.                 readMessage.close();
  29.                 //}                        
  30.             }
  31.             catch (IOException ioex){
  32.                 System.out.println("Exception: "+ioex);
  33.             }
  34.         }
  35.  
  36. }
and another thing : I couldnt understand wat u were saying abt using tags.
Jan 25 '07 #6

10K+
P: 13,264
Thanks for replying again.
GOOD NEWS : I got the class compiled.
The mistake i was doing was that i wasnt passing the value of the argument of the constructor to a local instance variable. and it was giving me an error msg "Go Hang Yourself".
But now i have made that change. The New code looks like this:





and another thing : I couldnt understand wat u were saying abt using tags.
When making a post look at the right side of the page where it says reply guidelines and the third point there is talking about wrapping the code you post in code tags. I will now edit the code you just posted to include these tags and you can see what I'm talking about.
Jan 25 '07 #7

James Bond
P: 12
When making a post look at the right side of the page where it says reply guidelines and the third point there is talking about wrapping the code you post in code tags. I will now edit the code you just posted to include these tags and you can see what I'm talking about.
Expand|Select|Wrap|Line Numbers
  1. Thanks I got it.
Jan 25 '07 #8

James Bond
P: 12
Expand|Select|Wrap|Line Numbers
  1. Thanks I got it.
Did u notice that i have declared the constructor of the class as Synchronized.
This was because i want either the server or the log file to access the data from the client at one time.

Can u suggest me wat would i have to do when multiple clients log on to the sever.
Regards
Jan 25 '07 #9

10K+
P: 13,264
Did u notice that i have declared the constructor of the class as Synchronized.
This was because i want either the server or the log file to access the data from the client at one time.

Can u suggest me wat would i have to do when multiple clients log on to the sever.
Regards
Oh dear.
1)That is not a constructor according to the compiler. Constructors do not return anything. Your method there returns a string.
2.)For the use of synchronized, you should have a look at a threads tutorial before you start using that.
Jan 25 '07 #10

James Bond
P: 12
Thanks for really bright suggestions.

I want to ask u a very important question. I dont have an idea regarding how i can detect a hardware attached to my system through a java programe.

Acctually i want to detect a GSM modem through my programme so that my system becomes a server and the mobile cell phones can access its database
by sending a message to my server.

Please reply
Rgds.
Jan 27 '07 #11

James Bond
P: 12
Thanks for really bright suggestions.

I want to ask u a very important question. I dont have an idea regarding how i can detect a hardware attached to my system through a java programe.

Acctually i want to detect a GSM modem through my programme so that my system becomes a server and the mobile cell phones can access its database
by sending a message to my server.

Please reply
Rgds.
Jan 28 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.