我是node的新手,正在尝试编写发送原始十六进制数据的最小tcp客户端。如果我应该使用缓冲区,那怎么办?如果我可以将十六进制作为字符串发送,那怎么办?非常感谢指导!
这是当前的无效代码:
var hexVal = `504f5354202f6c696e653320485454502f312e310d0a557365722d4167656e743a206e6f64652d6170700d0a4163636570743a202a2f2a0d0a686f73743a203139322e3136382e31342e39343a333030300d0a636f6e74656e742d747970653a206170706c69636174696f6e2f6a736f6e0d0a636f6e74656e742d6c656e6774683a2031390d0a436f6e6e656374696f6e3a20636c6f73650d0a0d0a227b757365726e616d653a202776616c277d22` // my raw hex, unwantendly sent as string
var net = require('net');
var HOST = '192.168.14.94';
var PORT = 3000;
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.write(hexVal);
});
client.on('data', function(data) { // 'data' is an event handler for the client socket, what the server sent
console.log('DATA: ' + data);
client.destroy(); // Close the client socket completely
});
// Add a 'close' event handler for the client socket
client.on('close', function() {
console.log('Connection closed');
});
服务器:
nc -lvp 3000
答案 0 :(得分:1)
给定一个十六进制字符串作为原始字节发送,有一种更方便的方法来执行您想要的操作。
当前,您使用的是Uint8Array
,每个字节需要被编码为0x41
或类似的字符。
但是,给定十六进制字符串,您可以像这样准备原始的十六进制缓冲区:
const hexString = "41424344";
const rawHex = Buffer.from(hexString, 'hex');
然后您可以将缓冲区写入套接字:
let client = new net.Socket();
client.connect(PORT, IP, () => {
console.log("Connected");
client.write(rawHex); //This will send the byte buffer over TCP
})
希望这会有所帮助
答案 1 :(得分:0)
您需要先设置服务器!!!
然后只有客户端才能连接到它...
package com.example.music_player;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
public class Youtube_Source extends AppCompatActivity {
WebView youtube;
Button play;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube__source);
youtube=findViewById(R.id.youtube);
youtube.loadUrl("https://www.youtube.com");
youtube.setWebViewClient(new WebViewClient());
WebSettings settings=youtube.getSettings();
settings.setJavaScriptEnabled(true);
Intent intent=getIntent();
//getting the list of songs from the internal memory from MainActivity
final ArrayList <String> songs=intent.getStringArrayListExtra("songs");
//Toast.makeText(this,String.valueOf(songs.size()),Toast.LENGTH_SHORT).show();
play=findViewById(R.id.button7);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(Youtube_Source.this,MusicPlayer.class);
i.putExtra("url",youtube.getUrl());
i.putExtra("position",String.valueOf(4));
i.putStringArrayListExtra("songs",songs);
String url=youtube.getUrl();
//Toast.makeText(Youtube_Source.this,url,Toast.LENGTH_SHORT).show();
startService(i);
}
});
}
@Override
public void onBackPressed() {
if (youtube.canGoBack()) {
youtube.goBack();
}
else {
super.onBackPressed();
}
}
}
您甚至还可以将服务器和客户端的代码拆分为两个单独的文件...
然后先启动服务器,然后启动客户端
答案 2 :(得分:0)
这解决了它:
var bytesToSend = [0x50, 0x4f, ...],
hexVal = new Uint8Array(bytesToSend);