0
我写了一个与socket通信的程序,但我不知道他们为什么不工作。我的socket程序有问题
服务器代码:
this.serverSocket = new ServerSocket(ServerConnector.port);
this.socketListener = this.serverSocket.accept();
System.out.println(this.socketListener.getPort());
this.objIn = new ObjectInputStream(this.socketListener.getInputStream());
System.out.println("1");
this.objOut = new ObjectOutputStream(this.socketListener.getOutputStream());
System.out.println("1");
this.objOut.writeInt(19999);
System.out.println("1");
this.objOut.writeObject(new Date());
System.out.println("1");
客户端代码:
this.clientSocket = new Socket(ClientConnector.host, ClientConnector.port);
System.out.println(this.clientSocket.getPort());
this.objIn = new ObjectInputStream(this.clientSocket.getInputStream());
System.out.println("1");
this.objOut = new ObjectOutputStream(this.clientSocket.getOutputStream());
System.out.println("1");
int i = (Integer) this.objIn.readInt();
System.out.println(i);
Date date = (Date) this.objIn.readObject();
事实是,他们不显示我建议通过(19999和日期)的任何信息,甚至还可以”打印一行“1”(我添加了测试)。这意味着即使下面的线路也不能正常工作。我真的很困惑这些,谁可以找出错误?
this.objIn = new ObjectInputStream(this.clientSocket.getInputStream());
如果服务器端没有打印出单行“1”,那么套接字甚至没有连接...您确定端口号对于客户端和服务器是否相同? – smichak 2012-04-15 20:00:27
我假设OP确实有成功的连接,但也需要验证。虽然我认为这应该很容易检测OP的一方,因为连接应该抛出一个异常。 – lothar 2012-04-15 20:07:47