weixin_42401733
2010-10-19 13:15
浏览 254
已采纳

请大家帮忙看看我的程序问题在什么地方! 谢谢!

下面是关于一个 Client 和 Server 的一个非常简单的程序,要达到的目的就是通过把几个数从Client传到Server,然后Server又把一些数传给Client。但结果是Server能够得到Client传来的数,可是Server不能把数传给Client。请问大家,问题出在什么地方?

另外,这样的传输问题,一定要用到线程吗? 非常感谢!

[code="java"]
import java.io.*;
import java.net.*;

public class ClientTest {
public static void main(String[] args) throws IOException {
Socket clientSocket = new Socket(InetAddress.getLocalHost()
.getHostAddress(), 8887);
System.out.print(InetAddress.getLocalHost().getHostAddress());

DataOutputStream outToServer = new DataOutputStream(
    new BufferedOutputStream(clientSocket.getOutputStream()));
outToServer.writeInt(123);

DataInputStream inFromServer = new DataInputStream(new BufferedInputStream(
    clientSocket.getInputStream()));
System.out.println("From Server: " + inFromServer.readInt());

outToServer.flush();
outToServer.close();
inFromServer.close();
clientSocket.close();

}
}

[/code]
[code="java"]
import java.io.*;
import java.net.*;

public class ServerTest {
public static void main(String[] args) throws IOException {
ServerSocket welcomeSocket = new ServerSocket(8887);
System.out.println("Warten auf Client ...");
Socket connectionSocket = welcomeSocket.accept();

DataInputStream inFromClient = new DataInputStream(new BufferedInputStream(
    connectionSocket.getInputStream()));
System.out.println("From Cleint: " + inFromClient.readInt());

DataOutputStream outToClient = new DataOutputStream(
    new BufferedOutputStream(connectionSocket.getOutputStream()));
outToClient.writeInt(234);
System.out.println("Daten wurden vom Server gesendet!");

inFromClient.close();
outToClient.flush();
outToClient.close();
connectionSocket.close();
welcomeSocket.close();

}
}
[/code]

6条回答 默认 最新

相关推荐 更多相似问题