本文介绍了客户端/服务器不接受ObjectInput和Output流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有两个服务器端进程的程序.一台服务器仅将ArrayList发送给客户端.另一个服务器首先从客户端获取一个字符串,然后找到与该ID对应的正确记录,然后将记录发回.
I have one program with two serverside processes. One server just sends an ArrayList to client. The other server first takes a string from client and then finds proper record corresponding to the ID and sends the record back.
第二个服务器进程出现问题.请参阅下面的println语句,其中显示卡在此处".那就是程序挂起的地方.
I'm having problems with the second server process. See the println statement below where it says "Gets stuck here". That's where the program hangs.
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.Statement;
import java.util.ArrayList;
public class CustServer{
public static void main(String args[]) throws IOException, ClassNotFoundException{
ServerSocket serverSocket1 = null;
ServerSocket serverSocket2 = null;
ArrayList<Customer> list = null;
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
String connectionURL = "jdbc:odbc:customer";
Connection con = null;
Statement stmt = null;
String sqlStatement = "SELECT ID, CustomerName, Address," +
" City, State, Zip, Orders FROM Customer";
ResultSet rs = null;
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(connectionURL);
stmt = con.createStatement();
rs = stmt.executeQuery(sqlStatement);
list = new ArrayList<Customer>(39);
while(rs.next()){
list.add(
new Customer(
rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4), rs.getString(5), rs.getString(6),
rs.getString(7)));
}
rs.close();
stmt.close();
con.close();
}catch (Exception ex) { ex.printStackTrace(); }
try {
serverSocket1 = new ServerSocket(8889);
System.out.println("Customer DB server is running at port 8889");
} catch (IOException e){
System.out.println("Could not listen on port: 8889.");
System.exit(1);
}
Socket clientSocket = null;
try{
clientSocket = serverSocket1.accept();
}catch (IOException e){
System.out.println("Server 1: Accept failed.");
System.exit(1);
}
//A server program which returns a list of customer IDs from the database.
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(list);
System.out.println(list.size() + "Customer IDs (list) were sent.");
out.flush();
out.close();
clientSocket.close();
serverSocket1.close();
//second server process:
//Takes a customer I D and returns the customer record
try {
serverSocket2 = new ServerSocket(8888);
System.out.println("Customer DB server is running at port 8888");
} catch (IOException e){
System.out.println("Could not listen on port: 8889.");
System.exit(1);
}
try{
clientSocket = serverSocket2.accept();
}catch (IOException e){
System.out.println("Server 2: Accept failed.");
System.exit(1);
}
System.out.println("Gets stuck here."); //<<<<<HERE
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
ObjectOutputStream out1 = new ObjectOutputStream(clientSocket.getOutputStream());
String id = (String) in.readObject();
String record = null;
for(Customer c : list){
if(c.getID().equals(id))
record = c.toString();
}
out1.writeObject(record);
out1.flush();
out1.close();
in.close();
clientSocket.close();
serverSocket2.close();
}
}
客户端:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ClientGUI extends JFrame implements ListSelectionListener{
//gui components
JList jlist = null;
String requestID = null; //Assigned to selected ID in JList.
JScrollPane scpane = null;
JTextField field = null;
JPanel pane = null;
DefaultListModel<String> listModel = null;
ArrayList<String> idList = null;
//client stuff:
Socket sock1 = null;
Socket sock2 = null;
ObjectInputStream in = null;
ObjectOutputStream out = null;
public ClientGUI() throws ClassNotFoundException{
//get List of IDs from Server1
try{
sock1 = new Socket("FahadAhmed-PC", 8889);
in = new ObjectInputStream(sock1.getInputStream());
idList = new ArrayList<String>(29);
ArrayList<Customer> custList = null;
custList = (ArrayList<Customer>) in.readObject();
for(Customer c : custList){
idList.add(c.getID());
}
in.close();
sock1.close();
sock2 = new Socket("FahadAhmed-PC", 8888);
}catch(UnknownHostException e) {
System.err.println("Don't know about host: FahadAhmed-PC");
System.exit(1);
}catch(IOException e){
System.err.println(e);
System.exit(1);
}
//Setup GUI
jlist = new JList(idList.toArray());
jlist.setVisibleRowCount(10);
scpane = new JScrollPane(jlist);
jlist.addListSelectionListener(this);
pane = new JPanel(new BorderLayout());
pane.setPreferredSize(new Dimension(300, 300));
field = new JTextField(29);
field.setEditable(false);
pane.add(scpane, BorderLayout.PAGE_START);
pane.add(field, BorderLayout.PAGE_END);
this.setContentPane(pane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String args[]) throws ClassNotFoundException{
ClientGUI gui = new ClientGUI();
}
@Override
public void valueChanged(ListSelectionEvent arg0) {
if(!arg0.getValueIsAdjusting())
try {
getRecord(jlist.getSelectedValue().toString());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void getRecord(String getID) throws ClassNotFoundException {
try{
in = new ObjectInputStream(sock2.getInputStream());
out = new ObjectOutputStream(sock2.getOutputStream());
System.out.println(getID + "sent to getRecord method");
out.writeObject(getID);
String rec = (String) in.readObject();
field.setText(rec);
out.flush();
out.close();
in.close();
}catch(UnknownHostException e) {
System.err.println("Don't know about host: FahadAhmed-PC");
System.exit(1);
}catch(IOException e){
System.err.println(e);
System.exit(1);
}
}
}
推荐答案
您需要在两端的ObjectInputStream之前创建ObjectOutputStream.
You need to create the ObjectOutputStream before the ObjectInputStream at both ends.
这篇关于客户端/服务器不接受ObjectInput和Output流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!