Flutter: Sending un ascii characters to server using socket.io - flutter

Im using flutter socket.io to connect to our server. The server doesnt use http protocol etc, straight connect, read and write, so I cant use WebSockets. I have the following:
Socket socket;
Uint8List BTBuffer = new Uint8List(10); //a buffer to hold data
Socket.connect("192.168.68.120", 21000);
.then((Socket sock) {
socket = sock;
socket.listen(dataHandler); //read data from server (data handler reads the data from server)
BTBuffer[0]=1;BTBuffer[1]=2;
socket.write(BTBuffer);
socket.destroy();
});
The above works, but it sends the values as strings and not decimal values. How can I send the values 1 and 2 for example in decimal? I was assuming there would be a write command pointing to a buffer and number of bytes to send.
Many Thanks
Scott

Related

How to create modbus master/server tcp in flutter?

I want to create desktop app to communicate with PLC using modbus. I found modbus package, but I can not implement modbus server. There is only client implementation. I've read modbus source code and found that modbus client was created using Socket from dart:io. Can we create modbus server using ServerSocket?. I've tried to create tcp server using ServerSocket and modbus client then connect it. Modbus client can connect to tcp server and tcp server will receive Uint8List data if modbus client do writeSingleRegister or readHoldingRegisters.
Server socket will receive this data if modbus client do write single register with address 4 and value 5
[0,1,0,0,0,6,10,6,0,4,0,5]
1 is sequence
The second 6 => always 6 for write single register
4 is address
5 is value
Server socket wil receive this data if modbus client do read single register with address 4 and amount 5
[0,1,0,0,0,6,10,3,0,4,0,5]
1 is sequence
3 => always 3 for read single register
4 is address
5 is amount
Create server
void createServer(){
ServerSocket.bind("127.0.0.1", 4000)
.then((server) {
server.listen((socket) {
socket.listen((event) {
print(event);
// how to send data result to client?
// socket.write() and socket.add() give errors
socket.write(Uint16List.view(event.buffer,0,1));
// error => Unhandled Exception: RangeError (byteOffset): Invalid value: Not in inclusive range 0..3: 4
});
});
});
}
Future<void>read(modbus.ModbusClient client,int address, int amount)async{
Uint16List data = await client.readHoldingRegisters(address, amount);
print(data);
}
Problem
How to send data to modbus client if client do readHoldingRegisters
using ServerSocket?

IP Packet message decryption

I'm using:
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
to get information from a network NIC on a specific IP.
I stripped the IP header and the protocol header (whether that is TCP, UDP or ICMP).
Now getting to the actual message, I already supposed this is not possible, but I'm new to network programming.
Is there a universal way to decode messages that are coming in from the byteArray?
Most people do this:
string temp = Encoding.ASCII.GetString(packetData);
I guess assuming that the original encoding is in ASCII.
Is there a way to walk through the byte[] array of the message part and decrypt it systematically to its original value?
Or do you need to have the foreknowledge of the original encoding type and offsets?
thanks!

TCP Socket connection- Unable to read data sent from external client even after serializing through ByteArrayLengthHeaderSerializer

I have implemented TCP socket server which accepts incoming XML messages from client. I could send messages through telnet.
But when I am trying to establish connection and send message through python script, I was getting IOException:CRLF not found before max message length: 2048.So I have added ByteArrayLengthHeaderSerializer to serialize and deserialize, but now I am getting below error.
IOException:Message length 1014132591 exceeds max message length: 2048
Though I am increasing the max message length I am getting IOException:Stream closed after 46 of 1014132591
Could someone let me know how to fix the issue.
final AbstractServerConnectionFactory crLfServer = context.getBean(AbstractServerConnectionFactory.class);
ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
serializer.setMaxMessageSize(1000 * 1024);
crLfServer.setSerializer(serializer);
crLfServer.setDeserializer(serializer);
I have implemented using Spring Integration.Below is the snippet for my inbound adapter
#Bean
public TcpReceivingChannelAdapter inboundAdapter(AbstractServerConnectionFactory connectionFactory) {
System.out.println("Creating inbound adapter");
TcpReceivingChannelAdapter inbound = new TcpReceivingChannelAdapter();
inbound.setConnectionFactory(connectionFactory);
//inbound.
inbound.setOutputChannel(fromTcp());
return inbound;
}
I think might be better to send exactly that CRLF in your script after the message. That will be exactly delimiter for the messages to deserialize. This is one what is used by the mentioned Telnet. However you need to come back to the default deserializer in the connection factory configuration.

Receiving data from lua tcp socket without data size

I've been working in a socket tcp connection to a game server. The big problem here is that the game server send the data without any separators - since it sends the packet lenght inside the data -, making impossible to use socket:receive("*a") or "*l". The data received from the server does not have a static size and are sent in HEX format. I'm using this solution:
while true do
local rect, r, st = socket.select({_S.sockets.main, _S.sockets.bulle}, nil, 0.2)
for i, con in ipairs(rect) do
resp, err, part = con:receive(1)
if resp ~= nil then
dataRecv = dataRecv..resp
end
end
end
As you can see, I can only get all the data from the socket by reading one byte and appending it to a string, not a good way since I have two sockets to read. Is there a better way to receive data from this socket?
I don't think there is any other option; usually in a situation like this the client reads a packet of specific length to figure out how much it needs to read from the rest of the stream. Some protocols combine new line and the length; for example HTTP uses line separators for headers, with one of the headers specifying the length of the content that follows the headers.
Still, you don't need to read the stream one-by-one character as you can switch to non-blocking read and request any number of characters. If there is not enough to read, you'll get partially read content plus "timeout" signaled, which you can handle in your logic; from the documentation:
In case of error, the method returns nil followed by an error message
which can be the string 'closed' in case the connection was closed
before the transmission was completed or the string 'timeout' in case
there was a timeout during the operation. Also, after the error
message, the function returns the partial result of the transmission.

Detecting the connection is closing in a chat server written in Python 3

Usually Python chat servers contain the following lines :
while 1:
data = conn.recv(1024)
if not data: break
where the connection conn was defined as :
conn, addr = s.accept()
It seems to me that when the connection is closing, the client sends an empty string to the server. Am I right ?
Yes, this is correct.
And now this answer is not too short anymore.
On a blocking socket, recv() blocks until it can return at least one byte of data. If the other end closes the socket, recv() returns an empty string (zero bytes of data).