i am nwebie in this forum but i think it is useful for me and the member are helpful
my project is about connecting client with the server to start exchanging messages between them.
to be more clear we process this purpose we serve this to the student in the university. how??
student will send a message that contains his name,id and request by format the server want such as zaina-20024008-grade.
the grade is the request and it will be different from one to another becouse it may be subject,pass credit or remain credits an so on. and then click send
when the message recievd to the server the server will superate the message to keep name , id and grade in its location to use it later. then it will access to the database with the name and id it got and start to get the request result and then send it back to the client and ask to the client if he need to get othe info or terminate
i thought to communicate the client with the server by socket connection " stream socket" and i have just finshed the code for one side which is for the server and still working to the client but i have a question occured in my mind and it may stupid question to you
since we diclare the port number in both client and server for listening to connect and declare the ip number in the client which is the local host name, is must the socket connect to network to available access and connect? in another meaning do i need to use modem to be the server connect to the internet becouse since we declare the host name"ip" we need network layer
does it??
i need you help please and sorry for my weakness language but i hope you understand what i want and looking for
i post the code here and note that the double quotation thais for the database i didnt fill still
Expand|Select|Wrap|Line Numbers
- package test;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class PcServer extends Thread {
- // declare unbound server socket
- private ServerSocket ServS =null ;
- //declare unconnected client socket
- private Socket ClientConnection = null;
- // input stream from client
- private ObjectInputStream IS;
- //output stream to client
- private ObjectOutputStream OS;
- //CONSTRUCTOR
- public PcServer (Socket ClientSocket){
- ClientConnection = ClientSocket;
- }
- public PcServer() {}
- // set up run server
- public void runServer(){
- try //creat server socket
- { ServS = new ServerSocket (3131);//3131 is the port number
- }
- catch (IOException e) {
- System.err.println("Could not listen on port:3131");
- System.exit(-1);
- }
- boolean listening = true;
- while (listening)
- { try
- { ClientConnection = ServS.accept();//allow server to accept connection from client
- //open input stream from the socket connection to read the data from the client
- IS=new ObjectInputStream(ClientConnection.getInputStream());
- //open data out stream from the socket connection to write data to the client
- OS=new ObjectOutputStream (ClientConnection.getOutputStream());
- //start the process(sending and recieving data) from/to client
- starExchange();
- }
- catch (Exception Ex){
- Ex.printStackTrace();
- }
- try // terminate all connection and close stream gate
- { OS.close();
- IS.close();
- ClientConnection.close();}
- catch (Exception ex)
- { ex.printStackTrace();}
- }
- }
- //start the process of the program
- public void starExchange() throws Exception{
- String msg =(String)IS.readObject();//read msg coming from client
- //EON=End Of Name
- int EON = msg.indexOf('-');
- //message getting from client = name-id-request
- // extract the name from the msg
- String name = msg.substring(0, EON);
- //extract id from the msg
- long ID = Long.parseLong(msg.substring(EON+1, EON+9));
- //extract request from the msg
- String Request = msg.substring(EON+10, msg.length());
- //JDBC driver name and database URL
- final String JDBC_DRIVER ="";
- final String DATABASE_URL="";
- //manage database connection
- Connection DBconnection = null;
- Statement DBstatement = null; // query statement
- //connect to the student database and query database
- try {
- // load database driver class
- Class.forName (JDBC_DRIVER);
- //establish connection to database
- DBconnection = DriverManager.getConnection (DATABASE_URL,"","");
- //create statement for quering database
- DBstatement = DBconnection.createStatement();
- request(Request,name,ID, DBconnection);
- }
- catch (SQLException sqlException){
- sqlException.printStackTrace();
- }
- finally // close DBstatement and DBconnction
- {
- try
- { DBstatement.close();
- DBconnection.close();
- }
- catch (Exception exception)
- { exception.printStackTrace();
- }
- }
- }
- public void request ( String R,String N,long id, Connection connection) throws Exception
- {
- //query database
- Statement st = connection.createStatement();
- ResultSet resultset = st.executeQuery("SELECT"+R+"FROM Student-Table WHERE ID="
- +id+"AND NAME="+N);//select the result according to the name and id of student
- // return the result according to the request
- OS.writeObject("NAME:"+N+"-ID:"+id+"-"+R+"is"+resultset+"/n Do you need to get othe information?/n" +
- "send (Y/N) with your request if Y");//SEND RESULT OF STUDENT REQUEST
- String newR=(String) IS.readObject();//READ THE STUDENT RESPOSE
- //CHECKING IF CLIENT WANTS TO GET OTHE INFO.
- if (newR.charAt(0)=='y'||newR.charAt(0)=='Y')
- { // extract client request
- String newReq=newR.substring(2,newR.length());
- //recursive function...check other request
- request(newReq,N,id, connection);
- }
- }
- }