I've created a calculator which outputs two double numbers based on given numbers, using c# windows form application.
I would like to output these numbers onto another computer that is connected to the LAN (Ethernet). I've tried to use both sockets and WCF yet couldn't find a proper way to make it work.
IPHostEntry ipHostInfo = Dns.Resolve(DnsGetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 61);
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sender.Connect(remoteEP);
lblInfo.Text = sender.RemoteEndPoint.ToString();
byte[] azm = new byte[] {byte.Parse(Azm.ToString()) };
byte[] ele = new byte[] {byte.Parse(Ele.ToString()) };
sender.Shutdown(SocketShutdown.Both);
sender. Close();
}
this is something I tried to make yet it didn't work.
I hope what I have requested is possible and really appreciate any help you can provide.
You forgot to actually send the data.
byte[] azm = new byte[] {byte.Parse(Azm.ToString()) };
byte[] ele = new byte[] {byte.Parse(Ele.ToString()) };
sender.Send(azm); //<-- You forgot to call these two.
sender.Send(ele); //<-- You forgot to call these two.
sender.Shutdown(SocketShutdown.Both);
sender. Close();
Read more about Socket.Send()
on the MSDN documentation.
Keep in mind though that a byte
can only go from 0 to 255. So if you plan to use larger numbers you have to use an int
or long
instead. This also means that the endpoint have to read more bytes.
byte[] azm = BitConverter.GetBytes(int.Parse(Azm.ToString()));
byte[] ele = BitConverter.GetBytes(int.Parse(Ele.ToString()));
If you use an int
the endpoint has to read 4 bytes, if you use long
the endpoint has to read 8 bytes.
Reversing BitConverter.GetBytes()
can be done like this:
int azm = BitConverter.ToInt32(<byte array here>, 0);
...or...
long azm = BitConverter.ToInt64(<byte array here>, 0);