通过java套接字返回多个值

时间:2014-02-04 14:15:30

标签: java mysql sockets

我有这两个函数,一个在客户端,它向服务器脚本发送值,然后应该返回多个值,这样我就可以填充文本框。

以下是我所做的:

客户端可以向服务器端发送多个值,服务器端成功执行mysql,但我无法弄清楚如何从服务器端向客户端返回多个值。

客户端:

private void formWindowActivated(java.awt.event.WindowEvent evt) {                                     
String identifier = "getbasicinfo";
    String hostname = "localhost";
int port = 6789;



    Socket clientSocket = null;  
    DataOutputStream os = null;
    BufferedReader is = null;


    try {
        clientSocket = new Socket(hostname, port);
        os = new DataOutputStream(clientSocket.getOutputStream());
        is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: " + hostname);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: " + hostname);
    }

// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on the given port

if (clientSocket == null || os == null || is == null) {
    System.err.println( "Something is wrong. One variable is null." );
    return;
}

try {


    os.writeBytes( identifier + "\n" );
    os.writeBytes( userdd + "\n" );
          os.writeBytes( shop + "\n" );
        String name = is.readLine();
        String surname = is.readLine();

        System.out.println( name + "\n");
        System.out.println( surname + "\n");
         txtname.setText(name); //it returns only this value
         txtsurname.setText(surname); 


    os.close();
    is.close();
    clientSocket.close();   
} catch (UnknownHostException e) {
    System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
    System.err.println("IOException:  " + e);
}           
 }      

代码的服务器端:

public void getBasicInfo(String username, String shop)
{   
       //....database code...
       stmt = conn.prepareStatement("SELECT * FROM workers inner join shops s where idworkers=? and s.idshops=?");
       stmt.setString(1, username);
       stmt.setString(2, shop);
       ResultSet rs = stmt.executeQuery();
       while(rs.next())
       {

           dname=rs.getString("name");
           dsurname=rs.getString("surname");
        }
      os.println(dname + "\n" );
      os.println(dsurname + "\n" );

        rs.close();
  }catch(SQLException se){
  //Handle errors for JDBC
  se.printStackTrace();
  }catch(Exception e){
  //Handle errors for Class.forName
  e.printStackTrace();
  }finally{
  //finally block used to close resources
  try{
     if(stmt!=null)
        conn.close();
  }catch(SQLException se){
  }// do nothing
  try{
     if(conn!=null)
        conn.close();
  }catch(SQLException se){
     se.printStackTrace();
  }//end finally try
  }//end try



 }

2 个答案:

答案 0 :(得分:1)

在服务器端,您需要某种集合来收集所有结果。例如

List<String> list = new LinkedList<String>();
//...
while(rs.next())
{
    list.add(dname + ", " + dsurname);
}

然后将此列表返回给客户端。如果您使用字符串列表来实现我的小例子,您可能希望继续处理您自己的类的对象列表(例如Name)。

答案 1 :(得分:1)

我认为你需要在客户端做一些改变。例如,尝试以这种方式制作一个块:

  public static void sendToAll()
{
    Iterator<Socket> sockIt = sockets.iterator();
    while(sockIt.hasNext())
    {
        Socket temp = sockIt.next();
        DataOutputStream tempOut = null;
        try
        {
            tempOut = new DataOutputStream(temp.getOutputStream());
        } catch (IOException e1)
        {

            e1.printStackTrace();
        }

    }
}

此块“send to all()”将数据发送给与其连接的所有客户端。