有没有办法可以在 java 中创建一个网络流数组。C# 支持创建网络流数组。
AFAIK 我们需要创建单独的 InputStreams 和 OutputStreams 以便在 Java 中接收和发送数据。
我想做的是建立多个 TCP 连接来发送和接收数据。
在java中有解决这个问题的方法吗?
有没有办法可以在 java 中创建一个网络流数组。C# 支持创建网络流数组。
AFAIK 我们需要创建单独的 InputStreams 和 OutputStreams 以便在 Java 中接收和发送数据。
我想做的是建立多个 TCP 连接来发送和接收数据。
在java中有解决这个问题的方法吗?
您可以通过创建多个 Socket 实例来实现,如下所示:
服务器.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
private static int CLIENT_COUNT = 0;
public static void main(String[] args) throws IOException
{
ServerSocket ss = new ServerSocket(2000);
while(true)
{
Socket s = ss.accept();
new SocketHandler("Handler#" + ++CLIENT_COUNT, s).start();
}
}
}
class SocketHandler extends Thread
{
private PrintWriter pw;
private BufferedReader br;
public SocketHandler(String name, Socket socket) throws IOException
{
super(name);
this.pw = new PrintWriter(socket.getOutputStream(), true);
this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
@Override
public void run()
{
String message;
try
{
while((message = br.readLine()) != null)
{
System.out.println("Server#" + getName() + " - Client sent: " + message);
sendMessage("Server#" + getName() + ": echo " + message);
}
}
catch(IOException e)
{
e.printStackTrace();
System.out.println("Server#" + getName() + " -> IOException");
}
}
public void sendMessage(String message)
{
pw.println(message);
System.out.println("Server#" + getName() + " is sending ~ " + message);
}
}
客户端.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client
{
private static int SOCKET_COUNT = 0;
public static void main(String[] args) throws UnknownHostException, IOException
{
Socket s1 = new Socket(InetAddress.getLocalHost(), 2000);
Socket s2 = new Socket(InetAddress.getLocalHost(), 2000);
Socket s3 = new Socket(InetAddress.getLocalHost(), 2000);
Socket s4 = new Socket(InetAddress.getLocalHost(), 2000);
LocalSocketHandler h1 = new LocalSocketHandler("LocalHandler#" + ++SOCKET_COUNT, s1);
LocalSocketHandler h2 = new LocalSocketHandler("LocalHandler#" + ++SOCKET_COUNT, s2);
LocalSocketHandler h3 = new LocalSocketHandler("LocalHandler#" + ++SOCKET_COUNT, s3);
LocalSocketHandler h4 = new LocalSocketHandler("LocalHandler#" + ++SOCKET_COUNT, s4);
h1.start();
h2.start();
h3.start();
h4.start();
h1.sendMessage("I am socket #1!");
h2.sendMessage("I am socket #2!");
h3.sendMessage("I am socket #3!");
h4.sendMessage("I am socket #4!");
}
}
class LocalSocketHandler extends Thread
{
private PrintWriter pw;
private BufferedReader br;
public LocalSocketHandler(String name, Socket socket) throws IOException
{
super(name);
this.pw = new PrintWriter(socket.getOutputStream(), true);
this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
@Override
public void run()
{
String message;
try
{
while((message = br.readLine()) != null)
{
System.out.println("Client#" + getName() + " - Server sent: " + message);
}
}
catch(IOException e)
{
e.printStackTrace();
System.out.println("Client#" + getName() + " -> IOException");
}
}
public void sendMessage(String message)
{
pw.println(message);
System.out.println("Client#" + getName() + " is sending ~ " + message);
}
}
客户端控制台:
Client#LocalHandler#1 is sending ~ I am socket #1!
Client#LocalHandler#2 is sending ~ I am socket #2!
Client#LocalHandler#3 is sending ~ I am socket #3!
Client#LocalHandler#4 is sending ~ I am socket #4!
Client#LocalHandler#1 - Server sent: Server#Handler#1: echo I am socket #1!
Client#LocalHandler#3 - Server sent: Server#Handler#3: echo I am socket #3!
Client#LocalHandler#2 - Server sent: Server#Handler#2: echo I am socket #2!
Client#LocalHandler#4 - Server sent: Server#Handler#4: echo I am socket #4!
服务器的控制台:
Server#Handler#1 - Client sent: I am socket #1!
Server#Handler#1 is sending ~ Server#Handler#1: echo I am socket #1!
Server#Handler#3 - Client sent: I am socket #3!
Server#Handler#3 is sending ~ Server#Handler#3: echo I am socket #3!
Server#Handler#2 - Client sent: I am socket #2!
Server#Handler#2 is sending ~ Server#Handler#2: echo I am socket #2!
Server#Handler#4 - Client sent: I am socket #4!
Server#Handler#4 is sending ~ Server#Handler#4: echo I am socket #4!