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.. :o
Thanks for your time
Yours sincerely Leo
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:
Code java:
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
Best of Luck also google for "Java socket programming tutorial"
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!
Re: Help with JAVA Server-Client Program
Quote:
Originally Posted by
GodLeoBouki
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.