我想知道是否有两个程序,一个名为server
,另一个名为client
,这两个程序分别说明服务器和客户端,是否可以在一台机器上测试它们?考虑到我定义了客户端套接字如下:
socket = new Socket("127.0.0.1",3000);
编辑: 服务器部分:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
class TCPServer
{
private static ServerSocket serverSocket;
private static Socket socket;
private static PrintWriter toClient;
private static BufferedReader fromClient;
public static void run() throws IOException
{
System.out.println("Server is waiting for connections...");
while (true)
{
openStreams();
processClient();
closeStreams();
}
}
public static void openStreams() throws IOException
{
serverSocket = new ServerSocket(3000);
socket = serverSocket.accept();
toClient = new PrintWriter(socket.getOutputStream(),true);
fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public static void closeStreams() throws IOException
{
fromClient.close();
toClient.close();
socket.close();
serverSocket.close();
}
public static void processClient()throws IOException
{
String msg = null;
do {
msg = fromClient.readLine();
toClient.println("Client said " + msg);
} while (!"exit".equals(msg));
}
}
客户端部分:
package clientserver.pkg1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
class TCPClient
{
private PrintWriter toServer;
private BufferedReader fromServer;
private Socket socket;
public void Client( )throws IOException
{
socket = new Socket("127.0.0.1",3000);
}
public void openStreams() throws IOException
{
toServer = new PrintWriter(socket.getOutputStream(),true);
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void closeStreams() throws IOException
{
fromServer.close();
toServer.close();
socket.close();
}
public void run()throws IOException
{
openStreams();
String msg = "";
Scanner scanner = new Scanner(System.in);
while (!"exit".equals(msg))
{
System.out.print(">");
msg = scanner.nextLine();
toServer.println(msg);
String tmp = fromServer.readLine();
System.out.println("Server said: " + tmp);
}
closeStreams();
}
}
主要班级
public static void main(String[] args) {
try
{
TCPServer server = new TCPServer() {};
TCPClient client = new TCPClient();
client.run();
server.run();
}
catch(IOException e)
{
System.err.println("Error in Client " + e.getMessage());
}
java.lang.NullPointerException
toServer = new PrintWriter(socket.getOutputStream(),true);
public void run()throws IOException
{
openStreams();
.....
}
public static void main(String[] args) {
try
{
TCPClient client = new TCPClient();
client.run();
}
在client.run();
答案 0 :(得分:1)
是的,你可以写这样的程序。有一个useful tutorial for this。 简而言之,这一切都归结为准备一个ServerSocket:
ServerSocket createSocket() {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(3000);
} catch (IOException ex) {
ex.printStackTrace(System.err);
throw ex;
}
return serverSocket;
}
从程序员的角度来看,驻留在远程计算机上的服务器或本地服务器之间没有区别。
您可以尝试编写两个不同的程序,一个用于服务器,另一个用于客户端。 这是客户的主要内容:
public static void main(String[] args) {
try {
TCPClient client = new TCPClient() {};
client.run();
} catch(IOException e) {
System.err.println("Error in Client " + e.getMessage());
}
这对于服务器来说:
public static void main(String[] args) {
try {
TCPServer server = new TCPServer() {};
server.run();
} catch(IOException e) {
System.err.println("Error in Server " + e.getMessage());
}
然后首先运行服务器,然后在另一个控制台中运行客户端。
答案 1 :(得分:0)
当然,您可以在同一台机器上测试它们。 几年前,我使用套接字制作了类似聊天的应用程序,并且在我使用不同的机器进行测试之前,我将它测试到了同一台机器,这很好,。
答案 2 :(得分:0)
您当然可以,只需将客户端和服务器连接到相同的端口号即可。
答案 3 :(得分:0)
您可以在PC的端口上读/写,与您在网络中的位置无关