日期:2014-05-20  浏览次数:20773 次

网络连接的疑惑
用Java实现网络连接的时候只能用DataInputStream,DataOutputStrame吗?
如果想用ObjectInputStream,ObjectOutputStream能不能实现,本人试了,不行,但是我想Java不会功能那么小吧,请高人指点一下。

------解决方案--------------------
Java code
public class Client {    
    public static void main(String[] arg) {    
        try {    
            Employee joe = new Employee(150, "Joe");    
            System.out.println("employeeNumber= " + joe.getEmployeeNumber());    
            System.out.println("employeeName= " + joe.getEmployeeName());    
            Socket socketConnection = new Socket("127.0.0.1", 11111);    
            ObjectOutputStream clientOutputStream = new ObjectOutputStream(    
                    socketConnection.getOutputStream());    
            ObjectInputStream clientInputStream = new ObjectInputStream(    
                    socketConnection.getInputStream());    
            clientOutputStream.writeObject(joe);    
            joe = (Employee) clientInputStream.readObject();    
            System.out.println("employeeNumber= " + joe.getEmployeeNumber());    
            System.out.println("employeeName= " + joe.getEmployeeName());    
            clientOutputStream.close();    
            clientInputStream.close();    
        } catch (Exception e) {    
            System.out.println(e);    
        }    
    }    
}