Я пытаюсь сделать голосовой чат, для этого я использую сокет с клиентом в java и сервером в nodejs.
Но по причине, когда я отправляю буфер, содержащий голос и заголовок (заголовок 42, а голосовой буфер 1,2,3,4..15), при приеме буфер не идентичен, но содержит другие части того, что я мог бы отправить.
Здесь часть производителя / потребителя: (в java)
/**
* Connects to the server and handles threads for Input/Output.
*/
@Override
public void run() {
try {
socket = new Socket(HOST, PORT);
socket.setKeepAlive(true);
} catch (Exception e) {
System.err.println("Could not connect to " + HOST + "/" + PORT + ":" + e.getMessage());
return;
}
//Reads data received from server
//Consumer
new Thread(new Runnable() {
@Override
public void run() {
while (socket.isConnected()) {
try {
DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
byte[] input = new byte[16];
int read = in.read(input, 0, 16);
System.out.println("Size: " + read + ", data: " + Arrays.toString(input));
} catch (IOException e) {
System.err.println("Could not read data from server:" + e.getMessage());
}
}
}
}).start();
//Sends data to server
//Producer
new Thread(new Runnable() {
@Override
public void run() {
while (socket.isConnected()) {
try {
byte[] voiceBuffer = new byte[16];
voiceBuffer[0] = 42;
for (byte i = 1; i < 16; i++) {
voiceBuffer[i] = i;
}
socket.getOutputStream().write(voiceBuffer, 0, voiceBuffer.length);
} catch (Exception e) {
System.err.println("Could not send data to server:"+ e.getMessage());
}
}
}
}).start();
}
здесь часть сервера: (в node.js)
var net = require('net');
// creates the server
var server = net.createServer();
//emitted when server closes ...not emitted until all connections closes.
server.on('close',function(){
console.log('Server closed !');
});
// emitted when new client connects
server.on('connection',function(socket){
socket.name = socket.remoteAddress + ":" + socket.remotePort
server.getConnections(function(error,count){
console.log('Number of concurrent connections to the server : ' + count);
});
socket.on('data',function(data){
socket.write(data);
});
socket.on('drain',function(){
socket.resume();
});
socket.on('error',function(error){
console.log('Error : ' + error);
});
socket.on('timeout',function(){
console.log('Socket timed out !');
socket.end('Timed out!');
});
socket.on('end',function(data){
console.log('Socket ended from other end!');
console.log('End data : ' + data);
});
socket.on('close',function(error){
if(error){
console.log('Socket was closed coz of transmission error');
}
});
});
// emits when any error occurs -> calls closed event immediately after this.
server.on('error',function(error){
console.log('Error: ' + error);
});
//emits when server is bound with server.listen
server.on('listening',function(){
console.log('Server is listening!');
});
server.maxConnections = 10;
//static port allocation
server.listen(2222);
Вот результат от потребителя :
Size: 16, data: [42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Size: 16, data: [42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Size: 16, data: [42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Size: 16, data: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 42, 1, 2, 3]
Size: 16, data: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 42, 1, 2, 3]
Size: 16, data: [12, 13, 14, 15, 42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Size: 16, data: [42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Size: 16, data: [42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Size: 16, data: [8, 9, 10, 11, 12, 13, 14, 15, 42, 1, 2, 3, 4, 5, 6, 7]
Size: 16, data: [42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Size: 16, data: [42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Size: 16, data: [12, 13, 14, 15, 42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Size: 16, data: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 42, 1, 2, 3]
Size: 16, data: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 42, 1, 2, 3]
Size: 16, data: [12, 13, 14, 15, 42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Size: 16, data: [42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Size: 16, data: [42, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Size: 16, data: [8, 9, 10, 11, 12, 13, 14, 15, 42, 1, 2, 3, 4, 5, 6, 7]
У меня должно быть число 42 в массиве 0, но это не так, что не так в моем коде?