Hello,
I have a TCP client written in Visual Studio C# and I want it to be able to talk to the TCP server program in my Spark Core. The server is simply going to echo back the text the TCP client is going to sent. But since my home network is DHCP, the Spark Core IP address keeps changing.
After power cycling the Spark Core, how do I determine the IP address? Can I use the Spark Core Device ID (for example 53ff75065075535112345678) or the Core name to determine the IP address?
I know I can determine the IP address of the Spark Core using the following code. But the TCP client has no way in knowing this IP address as this information is only available in Spark Core.
void setup()
{
server.begin();
// Make sure your Serial Terminal app is closed before powering your Core
Serial.begin(9600);
// Now open your Serial Terminal, and hit any key to continue!
while(!Serial.available()) SPARK_WLAN_Loop();
Serial.println(Network.localIP());
Serial.println(Network.subnetMask());
Serial.println(Network.gatewayIP());
Serial.println(Network.SSID());
}
Is there a way to assign a static IP address in the Spark Core?
Below is my TCP Client code for your reference.
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
public class clnt {
public static void Main() {
try {
TcpClient tcpclnt = new TcpClient();
Console.WriteLine(“Connecting…”);
tcpclnt.Connect(“192.168.88.99”,2488); // IP ADDRESS & PORT # OF TCP SERVER IN SPARK CORE
Console.WriteLine(“Connected”);
Console.Write("Enter the string to be transmitted : ");
String str=Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen= new ASCIIEncoding();
byte[] ba=asen.GetBytes(str);
Console.WriteLine(“Transmitting…”);
stm.Write(ba,0,ba.Length);
byte[] bb=new byte[100];
int k=stm.Read(bb,0,100);
for (int i=0;i<k;i++) Console.Write(Convert.ToChar(bb[i]));
tcpclnt.Close();
}
catch (Exception e) {
Console.WriteLine("Error… " + e.StackTrace);
}
}
}
Thanks.