java 如何测试示例 TCP Socket 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11495605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test sample TCP Socket program
提问by Guillermo Alvarez
I have a sample TCP client and server application that I want to run but I am not sure how to run them. It says I need to compile the client program in one host and the server application in another but I have no idea how to do this when I just have one computer. I know this is very simple but I need a little help to get me started.
我有一个我想运行的示例 TCP 客户端和服务器应用程序,但我不确定如何运行它们。它说我需要在一台主机上编译客户端程序,在另一台主机上编译服务器应用程序,但是当我只有一台计算机时,我不知道如何做到这一点。我知道这很简单,但我需要一点帮助才能开始。
Here is the sample TCP Server application:
这是示例 TCP 服务器应用程序:
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6790);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Here is the sample TCPClient application
这是示例 TCPClient 应用程序
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("10.0.1.2", 6790);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
Is something off here?
这里有东西吗?
Here is exactly what I do
这正是我所做的
(source: skitch.com)
(来源:skitch.com)
(source: skitch.com)
(来源:skitch.com)
This is the first time I use Eclipse so I could be doing something wrong there. I normally use DrJava but that didn't let me run both at the same time for some reason.
这是我第一次使用 Eclipse,所以我可能在那里做错了什么。我通常使用 DrJava,但由于某种原因,这并没有让我同时运行两者。
回答by Greg Hewgill
Almost any socket program can run both halves on the samecomputer. Just run the server in one window, the client in another, and specify the connection address 127.0.0.1 (which means localhost
, your own computer).
几乎任何套接字程序都可以在同一台计算机上运行两半。只需在一个窗口中运行服务器,在另一个窗口中运行客户端,并指定连接地址 127.0.0.1(即localhost
您自己的计算机)。
回答by Ruskin Janowski
For anyone using this code, I just stepped through the program and see that the connection is established successfully (if you change to the appropriate IP such as 'localhost'). However both the TCPServer and the TCPClient do a readLine first so they are both stuck waiting for input which never comes. Change one of them to write something first.
对于使用此代码的任何人,我只是逐步执行该程序并看到连接已成功建立(如果您更改为适当的 IP,例如“本地主机”)。然而,TCPServer 和 TCPClient 首先执行 readLine,因此它们都被困在等待永远不会到来的输入。换一个先写点东西。