我想发送多个随机值Socket。我认为数组是发送它们的最佳方法。但是我不知道如何向Socket写一个数组OutputStream?
我的Java课:
导入java.io.ByteArrayOutputStream; 导入java.io.IOException; 导入java.io.InputStream;
导入java.net.Socket; 导入java.io. *; 导入java.util.Random;
public class NodeCommunicator {
public static void main(String[] args) {
try {
Socket nodejs = new Socket("localhost", 8181);
Random randomGenerator = new Random();
for (int idx = 1; idx <= 1000; ++idx){
Thread.sleep(500);
int randomInt = randomGenerator.nextInt(35);
sendMessage(nodejs, randomInt + " ");
System.out.println(randomInt);
}
while(true){
Thread.sleep(1000);
}
} catch (Exception e) {
System.out.println("Connection terminated..Closing Java Client");
System.out.println("Error :- "+e);
}
}
public static void sendMessage(Socket s, String message) throws IOException {
s.getOutputStream().write(message.getBytes("UTF-8"));
s.getOutputStream().flush();
}
}