How to send byte array via TCP when using newLISP - newlisp

I want to send two bytes which represent an unsigned short in big-endian to server via TCP. But net-send only supports string parameter.
Could anyone tell me how to do this with newLISP?

It works now.
CODE: SELECT ALL
(set 'socket (net-connect "localhost" 8889))
16
(set 'size (pack ">d" 19))
"\000\019"
(net-send socket size)
2
pack returns a string buffer that contains my two bytes, net-send sends the string to server.
My C++ server got two bytes, 0 and 19.
Thanks.

Related

How to send hexadecimal commands to a monitor over a Serial Port with PowerShell

Okay this is a bit of a weird one but due to my complete lack of knowledge on how to use Serial Ports or PowerShell i couldn't think of anywhere else to go.
What I'm trying to do is send basic commands to a monitor that has a RS232 port on it that can be used to control the properties of the monitor, i.e. Brightness, Contrast, Backlight etc.
I'm attempting to use PowerShell to do this for testing purposes. I can create the $port in PowerShell and assign it to the relevant COM# that the monitor is connected to but I'm at a loss as to how to actually send the command to it as it must be Hexadecimal for the controller on the monitor to understand it.
The monitor is capable of returning an acknowledgement using the same Hex layout but I'm unable to find a way of showing that response on the Powershell console.
This is what I have been able to get so far.
PS C:\Users\Kingdel> [System.IO.Ports.SerialPort]::getportnames()
COM1
COM2
COM3
COM4
COM5
COM6
PS C:\Users\Kingdel> $port= new-Object System.IO.Ports.SerialPort COM1,9600,None,8,one
PS C:\Users\Kingdel> $port.open()
PS C:\Users\Kingdel> $port.WriteLine("0xA6,0x01,0x00,0x00,0x00,0x03,0x01,0x31,0x94")
PS C:\Users\Kingdel>
Anyone able to point me in the right direction as to how I can send this command to the monitor and to view the returned acknowledgement.
I am open to trying different terminals, I have tried PuTTy and Termite and neither of them were successful as far as I can tell.
That's a really good question. Maybe I can help with this.
The SerialPort.WriteLine() method takes in a string to write to the output buffer, so using this, you're essentially sending an argument of strings.
To send something over to the [System.IO.Ports.SerialPort] object, you need to use SerialPort.Write() with a Byte[] argument. The Write() method can take in a number of bytes to the serial port using data from a buffer.
You also need to send it three arguments which are buffer Byte[], offset Int32, and a count Int32. So in your case, you can do the following:
[Byte[]] $hex = 0xA6,0x01,0x00,0x00,0x00,0x03,0x01,0x31,0x94
$port.Write($hex, 0, $hex.Count)
The buffer argument is the array that contains the data to write to the port, offset is the zero-based byte offset in the buffer array at which to begin copying the bytes to the port, and the count is the number of bytes to write.

python: send UDP message

I have the following challenge sending UDP packet:
The packet is 40 bytes long where all fields constant except some counter and checksum.
header='\xaf\x18\x25\x25'
message= 'ABCDEFGHIGKLMNOPQRTSUVXYZ0123456'
i=1
#do some checksum calculation and store result into the checksum variable
message=header + chr(i) + data + chr(checksum >>8) + chr(checksum & 0xFF)
sock.sendto(message.encode('utf-8'), (DST_IP, int(DST_PORT)))
However, looking into a wireshark, I can see that the message is 43 bytes where i have a 0xC2 at first location instead of the actual header 1st byte and 0XC3 and 0xC2 before the checksums MSB & LSB (which are the 3 extra bytes)
Any ssugestion what is the issue and how to fix it?
changed the encoding solved the issue
sock.sendto(message.encode('charmap'), (DST_IP, int(DST_PORT)))

Wireshark and length of data

I used wireshark, under our protocol we send and receive 5 bytes in payload, even wireshark say 5 bytes received , but show : 0000010001 : 10 digit.
How i read it?
It's hexadecimal - each two digits correspond to one byte.

scapy encoding when stringed

What is this encoding below that you get when you string a packet in scapy? This is certainly not hex.
str(IP()) ’E\x00\x00\x14\x00\x01\x00\x00#\x00|\xe7\x7f\x00\x00\x01\x7f\x00\x00\x01’
the \x is the hex notation. In this case when you use str(IP()) you are trying to convert the packet data into string which is not completely valid because not every raw hex data could be found in ASCII table to substitute it with a letter so any hex that couldn't be converted will seen in this format \x14.
I think the following example will help:
viewing packet summary using scapy method
encoding the packet data into hex format to view using python methods
Welcome to Scapy (2.1.1-dev)
>>> pkt=IP()
>>> pkt.summary()
'127.0.0.1 > 127.0.0.1 ip'
>>> data=str(pkt)
>>> data.encode('hex')
'450000140001000040007ce77f0000017f000001'
>>>
consider these points:
in SCAPY if you create an IP layer without determining the source and destination the loopback address will be set as default for both as shown in the example
'127.0.0.1 > 127.0.0.1 ip'
.summary() is a Scapy method
str(), .encode are a python methods

Can I construct in Perl a network type message that has odd byte length

$str = "0xa"; #my hex number
$m = pack("n",hex("$str")); --> the output is 000a
$m = pack("c",hex("$str")); --> the output is 0a
I need the result to be only a. The bottom line is that, with pack, I can send on a socket, messages that have odd length (like A675). If I try to send A675B then with pack I will have A6750B
A675 is two bytes. A675B is two and a half bytes. Sockets don't support sending anything smaller than a byte. You could send a flag that tells the receiver to ignore one nybble of the message, but that's about it.