如何通过 TCP 连接发送字节数组(java 编程)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2878867/
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 send an array of bytes over a TCP connection (java programming)
提问by Mark Roberts
Can somebody demonstrate how to send an array of bytes over a TCP connection from a sender program to a receiver program in Java.
有人可以演示如何通过 TCP 连接从 Java 中的发送方程序向接收方程序发送字节数组。
byte[] myByteArray
(I'm new to Java programming, and can't seem to find an example of how to do this that shows both ends of the connection (sender and receiver.) If you know of an existing example, maybe you could post the link. (No need to reinvent the wheel.) P.S. This is NOThomework! :-)
(我是 Java 编程的新手,似乎无法找到显示连接两端(发送方和接收方)的示例。如果您知道现有示例,也许您可以发布链接.(无需重新发明轮子。) PS 这不是作业!:-)
采纳答案by Kevin Brock
The InputStream
and OutputStream
classes in Java natively handle byte arrays. The one thing you may want to add is the length at the beginning of the message so that the receiver knows how many bytes to expect. I typically like to offer a method that allows controlling which bytes in the byte array to send, much like the standard API.
Java 中的InputStream
和OutputStream
类本机处理字节数组。您可能想要添加的一件事是消息开头的长度,以便接收者知道期望的字节数。我通常喜欢提供一种方法来控制要发送字节数组中的哪些字节,就像标准 API 一样。
Something like this:
像这样的东西:
private Socket socket;
public void sendBytes(byte[] myByteArray) throws IOException {
sendBytes(myByteArray, 0, myByteArray.length);
}
public void sendBytes(byte[] myByteArray, int start, int len) throws IOException {
if (len < 0)
throw new IllegalArgumentException("Negative length not allowed");
if (start < 0 || start >= myByteArray.length)
throw new IndexOutOfBoundsException("Out of bounds: " + start);
// Other checks if needed.
// May be better to save the streams in the support class;
// just like the socket variable.
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(len);
if (len > 0) {
dos.write(myByteArray, start, len);
}
}
EDIT: To add the receiving side:
编辑:添加接收方:
public byte[] readBytes() throws IOException {
// Again, probably better to store these objects references in the support class
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = dis.readInt();
byte[] data = new byte[len];
if (len > 0) {
dis.readFully(data);
}
return data;
}
回答by bmargulies
The Oracle Socket Communications Tutorialwould seem to be the appropriate launch point.
Oracle Socket Communications Tutorial似乎是合适的启动点。
Note that it's going to extra troubleto turn characters into bytes. If you want to work at the byte level, just peel that off.
请注意,将字符转换为字节会带来额外的麻烦。如果您想在字节级别工作,只需将其剥离即可。
回答by Alexander Pogrebnyak
This Sun Sockets tutorialshould give you a good starting point
这个Sun Sockets 教程应该给你一个很好的起点
回答by Catchwa
Just start with this examplefrom the Really Big Index. Notice though, that it's designed to transmit and receive characters, not bytes. This isn't a big deal, though - you can just deal with the raw InputStream
and OutputStream
objects that the Socket
class provides. See the API for more info about the different types of readers, writers and streams. Methods you'll be interested in are OutputStream.write(byte[])
and InputStream.read(byte[])
.
就从真正大索引中的这个例子开始。但请注意,它旨在传输和接收字符,而不是字节。不过,这没什么大不了的——您可以只处理类提供的原始对象和对象。有关不同类型的读取器、写入器和流的更多信息,请参阅 API。您会感兴趣的方法是和。InputStream
OutputStream
Socket
OutputStream.write(byte[])
InputStream.read(byte[])
回答by Yann Ramin
What you need to use is the write
method of an java.io.OutputStream
, and the read
method of an java.io.InputStream
, both of which you can retrieve from the Socket
you open.
您需要使用的是write
an的方法和 anjava.io.OutputStream
的read
方法java.io.InputStream
,这两者都可以从Socket
您打开的文件中检索。
回答by Jerry Miller
I'm guessing that the question is worded incorrectly. I found this when searching for an answer to why my use of InputStream and OutputStream seemed to be setting the entire array to 0 upon encountering a byte of value 0. Do these assume that the bytes contain valid ASCII and not binary. Since the question doesn't come right out and ask this, and nobody else seems to have caught it as a possibility, I guess I'll have to satisfy my quest elsewhere.
我猜这个问题措辞不正确。我在搜索为什么我使用 InputStream 和 OutputStream 似乎在遇到值为 0 的字节时将整个数组设置为 0 的答案时发现了这一点。是否假设这些字节包含有效的 ASCII 而不是二进制。由于这个问题并没有直接提出来,而且似乎没有其他人认为这是一种可能性,我想我必须在别处满足我的追求。
What I was trying to do was write a TransparentSocket class that can instantiate either a TCP (Socket/ServerSocket) or a UDP (DatagramSocket) to use the DatagramPacket transparently. It works for UDP, but not (yet) for TCP.
我试图做的是编写一个 TransparentSocket 类,该类可以实例化 TCP (Socket/ServerSocket) 或 UDP (DatagramSocket) 以透明地使用 DatagramPacket。它适用于 UDP,但不适用于 TCP。
Follow-up: I seem to have verified that these streams are themselves useless for binary transfers, but that they can be passed to a more programmer-friendly instantiation, e.g.,
后续:我似乎已经证实这些流本身对于二进制传输是无用的,但是它们可以传递给一个对程序员更友好的实例化,例如,
new DataOutputStream(socket.getOutputStream()).writeInt(5);
new DataOutputStream(socket.getOutputStream()).writeInt(5);
^ So much for that idea. It writes data in a "portable" way, i.e., probably ASCII, which is no help at all, especially when emulating software over which I have no control!
^ 到此为止。它以“可移植”的方式写入数据,即可能是 ASCII,这根本没有帮助,尤其是在模拟我无法控制的软件时!
回答by Revanth Kumar
I would ask you to use ObjectOutputStream and ObjectInputStream. These send everything as an object and receive as the same.
我会要求您使用 ObjectOutputStream 和 ObjectInputStream。这些将所有内容作为对象发送并以相同的方式接收。
ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
os.flush();
ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
os.writeObject(byte_array_that_you_want_to_send);
byte[] temp = (byte[]) is.readObject();
Also remember first create the output stream, flush it and then go ahead with the input stream because if something left out in the stream the input stream wont be created.
还要记住首先创建输出流,刷新它,然后继续输入流,因为如果流中遗漏了某些内容,则不会创建输入流。
回答by Utpal Goswami
import java.io.*;
import java.net.*;
public class ByteSocketClient
{
public static void main(String[] args) throws UnknownHostException, IOException
{
Socket s=new Socket("",6000);
DataOutputStream dout=new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
byte[] a = {(byte)0xC0,(byte)0xA8,(byte)0x01,(byte)0x02,(byte)0x53,(byte)0x4D,(byte)0x41,(byte)0x52,(byte)0x54};
dout.write(a);
dout.close();
s.close();
}
}
回答by Lightbeard
Here is an example that streams 100 byte wav file frames at a time.
这是一次流式传输 100 字节 wav 文件帧的示例。
private Socket socket;
public void streamWav(byte[] myByteArray, int start, int len) throws IOException {
Path path = Paths.get("path/to/file.wav");
byte[] data = Files.readAllBytes(path);
OutputStream out = socket.getOutputStream();
DataOutputStream os = new DataOutputStream(out);
os.writeInt(len);
if (len > 0) {
os.write(data, start, len);
}
}
public void readWav() throws IOException {
InputStream in = socket.getInputStream();
int frameLength = 100; // use useful number of bytes
int input;
boolean active = true;
while(active) {
byte[] frame = new byte[frameLength];
for(int i=0; i<frameLength; i++) {
input = in.read();
if(input < 0) {
active = false;
break;
} else frame[i] = (byte) input;
}
// playWavPiece(frame);
// streamed frame byte array is full
// use it here ;-)
}
}