简体   繁体   中英

How to sent a result when query through socket?

I have code about query database through socket but i can not how to receive result, anybody can help me. Here is my code:

Server

private void server() throws IOException, SQLException {

    try {

        ServerSocket server = new ServerSocket(1994);
        System.out.println("Server is ready...");
        socket = server.accept();

        DataInputStream in = new DataInputStream(socket.getInputStream());

         DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        String query = in.readUTF();
        pst = conn.prepareStatement(query);
        rs = pst.executeQuery();

        while(rs.next())
        {

            String ID = rs.getString("ID");
            out.writeUTF(ID);
            String Name= rs.getString("Name");
            out.writeUTF(Name);

        }


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    socket.close();
}

Client

public class Client {

private void connect() throws ClassNotFoundException {
    ResultSet rs = null;
    try {

        Socket socket = new Socket("localhost", 1994);
        DataInputStream in = new DataInputStream(socket.getInputStream());
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        out.writeUTF("Select * from sinhvientest");
        String ID = null;
        String Name = null;
        String s;
        while(!(s = in.readUTF()).equals(null))
        {
            ID = in.readUTF();
            System.out.print(ID + " ");
            Name = in.readUTF();
            System.out.println(Name);
        }

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Connect to database is good, query from database is good, I want send result to clent but i do not how to. It sent only one row. Anyone can help me? Thanks so much!

You write two String values:

        String ID = rs.getString("ID");
        out.writeUTF(ID);
        String Name= rs.getString("Name");
        out.writeUTF(Name);

but you read three:

    while(!(s = in.readUTF()).equals(null))
    {
        ID = in.readUTF();
        System.out.print(ID + " ");
        Name = in.readUTF();
        System.out.println(Name);
    }

Better use

    try {
        while( true )
        {
            String ID = in.readUTF();
            String Name = in.readUTF();
            System.out.println(ID + " " + Name);
        }
     } catch( EOFException eof ){
        // handle regular end of file
     } catch( IOException ioe ){
        // error
     } catch( UTFDataFormatException dfe ){
        // error
     }

Later Here's a very inelegant sample of a client/server connection using readUTF.

public class Client {

public Client( String host, int port ) throws Exception {
    Socket socket = new Socket(host, port);
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    DataInputStream dis = new DataInputStream(socket.getInputStream() );
    dos.writeUTF( "a line from client" );
    try {
         while( true ){
            String line = dis.readUTF();
            System.out.println( "got: " + line );
         }
    } catch( Exception e ){
    }
    dis.close();
    dos.close();
}

public static void main(String args[]) throws Exception {
    String host = args[0];
    int port = Integer.parseInt( args[1] );
    Socket socket = null;
    Client client = new Client( host, port );
}
}

public class Server {
public static void main(String[] args) throws Exception {
    int portNumber = Integer.parseInt(args[0]);

    while (true) {
        try {
            new Server(portNumber);
        } catch (java.net.SocketException se) {
        } catch (IOException ioe) {
        } catch (ClassNotFoundException cnf) {
        }
    }
}

public Server( int port ) throws Exception {
ServerSocket serverSocket = new ServerSocket(port );
    Socket socket = serverSocket.accept();

    DataInputStream dis = new DataInputStream(socket.getInputStream());
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    System.out.println("...connected...waiting for data...");
    String line = dis.readUTF();
    System.out.println( "got:" + line );
    for( int i = 1; i <= 3; i++ ){
        dos.writeUTF( "line " + i + " from server" );
    }
    dis.close();
    dos.close();
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM