Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with JAVA Server-Client Program

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with JAVA Server-Client Program

    Using the Server-Client model I have to make a program that shows grades info of a student.By giving the lesson name the program returns that lesson grade.Moreover it should provide the overall grade per (6 months session) and general overall grade.
    The Client program can send these:
    Grade request for a lesson
    Overall grade per (6 months session) request
    General overall grade request
    Request for joining new lesson

    After getting back response from the Server,the Client program prints the “answers” to the default “exit” (System.Out)
    The Server program accepts Client requests and returns grade info,which are in a .txt file.Every lesson corresponds to a line on that text file,with that form:
    <lesson name> , <number of 6 months session that the lesson belongs> , <grade>
    Example: Maths , 2 , 10

    Finally in case of an error request Server program should return an error message to the Clients program.

    I am using eclipse and I know I have to make 3 files:1) the protocol (thinking of tcp for safety in data transfer) 2)The Client program and 3)the Server program.
    Can anyone give me a similar example or suggest me anything useful for my task?I am newbie and the only program I have managed to get work is a simple date-time Server-Client program..
    Thanks for your time
    Yours sincerely Leo


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Help with JAVA Server-Client Program

    Should be simple enough. U need multithreading if u desire to service multiple client. I am giving a layout not actual source.
    Server:
    class server{
     main(){
         ServerSocket ss = new ServerSocket(port) ;
             while (true){
                 Socket clinet = ss.accept() ;
                 new Thread(new ClientWorker(client)).start() ;
             }
         }
    }
    class ClientWorker implements Runnable{
        Socket s ;
        BufferedReader in = null ;
        PrintWriter out = null ;
        ClientWorker( Socket s){
            this.s = s ;
        }
        public void run(){
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            out = new PrintWriter(s.getOutputStream(), true);
     
            while (true){
                  String cmd = in.readLine() ; // or something like that
                  if (cmd.equals("SOMETHING"){
                     String str = doSomething() ; // its ur grade evaluation function or whatever....
                    out.println(ur result string) ; out.flush() ;
                 } else if (cmd.equals("SOME OTHER COMMAND"){
                     //server other wise
                 } //one else if will contain condition for termination of this inf loop :D
            }
        }
     
    }

    I hope u can write the client side in similiar fashion it will be single threaded and while getting socket streams... mind this
    out = new PrintWriter(s.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    // yes reverse the order from server side, trust me.
    Best of Luck also google for "Java socket programming tutorial"

  3. The Following User Says Thank You to dumb_terminal For This Useful Post:

    GodLeoBouki (April 12th, 2011)

  4. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with JAVA Server-Client Program

    Hi dumb_terminal,
    I made it somehow through that task.Firstly made the server side by creating functions and classes and made it interact with a "private" data base file..then made a selection menu for the client and finally the tcp protocol.Now I am gonna try the multithreading you said above (1st attempt ever :$)!In addition THANK you for the source layout,it is really useful to me,your socket programming seems better and less complicated to me than the other i used for the date-time project..I ll have to study more .pdfs and practise a lot!
    I proceed slow on that project but steady,so I ll post my source if I ever made it work fine!
    Again thanks for help!

  5. #4
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Help with JAVA Server-Client Program

    Quote Originally Posted by GodLeoBouki View Post
    Hi dumb_terminal,
    I made it somehow through that task.Firstly made the server side by creating functions and classes and made it interact with a "private" data base file..then made a selection menu for the client and finally the tcp protocol.Now I am gonna try the multithreading you said above (1st attempt ever :$)!In addition THANK you for the source layout,it is really useful to me,your socket programming seems better and less complicated to me than the other i used for the date-time project..I ll have to study more .pdfs and practise a lot!
    I proceed slow on that project but steady,so I ll post my source if I ever made it work fine!
    Again thanks for help!
    Thank U, wud u blv i am also a noob ? Anyway if u plan a GUI, pay extra attention, u need to seperate the GUI classes in Event dispatching thread (google it), but first try out some multithreading in console , dnt 4gt to close the streams and try catches, and u might also enjoy this
    Best of Luck pal.

Similar Threads

  1. TCP/IP java client, c++ server
    By akboyd88 in forum Java Networking
    Replies: 0
    Last Post: March 24th, 2011, 11:46 AM
  2. [Java] Client - server, example
    By Grabar in forum Java Networking
    Replies: 6
    Last Post: January 22nd, 2011, 01:56 PM
  3. Client Server Program Not Working
    By ROHIT C. in forum Java Networking
    Replies: 3
    Last Post: September 3rd, 2010, 03:30 PM
  4. Client-Server program
    By Reztem in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 12th, 2010, 06:36 PM
  5. client server program on a single machine
    By subhopam in forum Java Networking
    Replies: 1
    Last Post: December 16th, 2009, 02:02 PM

Tags for this Thread