[英]how to send an array of bytes over a TCP connection (java programming)
有人可以演示如何通過TCP連接從發送器程序向Java中的接收器程序發送字節數組。
byte[] myByteArray
(我是Java編程的新手,似乎無法找到如何執行此操作的示例,顯示連接的兩端(發送方和接收方)。如果您知道現有示例,也許您可以發布鏈接(不需要重新發明輪子。)PS這不是功課!:-)
Java中的InputStream
和OutputStream
類本身處理字節數組。 您可能想要添加的一件事是消息開頭的長度,以便接收器知道預期的字節數。 我通常喜歡提供一種方法,允許控制字節數組中的哪些字節發送,就像標准API一樣。
像這樣的東西:
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);
}
}
編輯 :添加接收方:
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;
}
Oracle套接字通信教程似乎是合適的發布點。
請注意,將字符轉換為字節會更加麻煩 。 如果你想在字節級工作,只需將其剝離。
這個Sun Sockets教程應該為您提供一個很好的起點
你需要用什么write
的方法java.io.OutputStream
,和read
的方法java.io.InputStream
,這兩者都可以從檢索Socket
打開。
我會請你使用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();
還要記住首先創建輸出流,刷新它然后繼續輸入流,因為如果流中遺漏了某些內容,則不會創建輸入流。
我猜這個問題措辭錯誤。 我在尋找一個答案時找到了這個,為什么我在使用InputStream和OutputStream時遇到一個值為0的字節時將整個數組設置為0.這些假設這些字節包含有效的ASCII而不是二進制。 既然這個問題沒有出來問這個問題,而且其他人似乎沒有把它作為一種可能性,我想我必須滿足我在其他地方的任務。
我試圖做的是編寫一個TransparentSocket類,它可以實例化TCP(Socket / ServerSocket)或UDP(DatagramSocket)以透明地使用DatagramPacket。 它適用於UDP,但不適用於TCP。
后續:我似乎已經證實這些流本身對二進制傳輸無用,但它們可以傳遞給程序員更友好的實例化,例如,
new DataOutputStream(socket.getOutputStream())。writeInt(5);
這個想法太多了。 它以“可移植”的方式寫入數據,即可能是ASCII,這根本沒有幫助,尤其是在模擬我無法控制的軟件時!
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();
}
}
這是一個流式傳輸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 ;-)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.