Server and Log File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • James Bond
    New Member
    • Jan 2007
    • 12

    #1

    Server and Log File

    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 "SocketRead er", and is coded as follows:

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

    public class socketReader implements Runnable{

    BufferedReader readMessage;
    String message="";

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

    public void run(){
    try{


    //while(true){
    readMessage=new BufferedReader( new InputStreamRead er
    (client.getInpu tStream()));
    message=readMes sage.readLine() ;
    readMessage.clo se();
    //}
    }
    catch (IOException ioex){
    System.out.prin tln("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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by James Bond
    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 "SocketRead er", and is coded as follows:

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

    public class socketReader implements Runnable{

    BufferedReader readMessage;
    String message="";

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

    public void run(){
    try{


    //while(true){
    readMessage=new BufferedReader( new InputStreamRead er
    (client.getInpu tStream()));
    message=readMes sage.readLine() ;
    readMessage.clo se();
    //}
    }
    catch (IOException ioex){
    System.out.prin tln("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?

    Comment

    • James Bond
      New Member
      • Jan 2007
      • 12

      #3
      Originally posted by r035198x
      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.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by James Bond
        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.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          You might want to have a look at this thread too.

          Comment

          • James Bond
            New Member
            • Jan 2007
            • 12

            #6
            Originally posted by r035198x
            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:



            Code:
            import java.io.*;
            import java.net.*;
            
            public class socketReader implements Runnable{
            	
            	BufferedReader readMessage;
            	String message="";
            	Socket client;
            	
            	synchronized String socketReader(Socket client){
            		
            		this.client=client;
            		Thread t=new Thread(this);		
            		t.start();
            					
            		return message;					
            	}
            
            
            
            		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);
            			}
            		}
            		
            }
            and another thing : I couldnt understand wat u were saying abt using tags.
            Last edited by r035198x; Jan 25 '07, 12:59 PM. Reason: removed quote tags and put code tags

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by James Bond
              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.

              Comment

              • James Bond
                New Member
                • Jan 2007
                • 12

                #8
                Originally posted by r035198x
                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.
                Code:
                Thanks I got it.

                Comment

                • James Bond
                  New Member
                  • Jan 2007
                  • 12

                  #9
                  Originally posted by James Bond
                  Code:
                  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

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by James Bond
                    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.

                    Comment

                    • James Bond
                      New Member
                      • Jan 2007
                      • 12

                      #11
                      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.

                      Comment

                      • James Bond
                        New Member
                        • Jan 2007
                        • 12

                        #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.

                        Comment

                        Working...