Created
October 6, 2018 07:23
-
-
Save Vchekryzhov/2c12460e682bb5ed5e4eb7073178bd0d to your computer and use it in GitHub Desktop.
autoSearch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void SetDataToServer(){ | |
if(SearchServer.ipServer == ""){ | |
SearchServer udp = new SearchServer(); | |
udp.Start(); | |
udp.Send("softPLC"); | |
} | |
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); | |
string vel = plcValue_vel.TextValue; | |
string pos = plcValue_state.TextValue; | |
string plc = plcValue_temp.TextValue; | |
string state = plcValue_state.TextValue; | |
string tool = plcValue_tool.TextValue; | |
string id = "10"; | |
string port = ":8080"; | |
string protocol = "http://"; | |
string path = "/set"; | |
string ip = SearchServer.ipServer; | |
if(ip!=""){ | |
try | |
{ | |
string text1 = protocol + ip + port + path+ "?id="+id+"&vel="+ vel + "&pos=" + pos + "&plc=" + plc + "&state=" + state + "&tool=" + tool; | |
System.Net.HttpWebRequest request1 = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(text1); | |
System.Net.HttpWebResponse response1 = (System.Net.HttpWebResponse)request1.GetResponse(); | |
} | |
catch(Exception ex) | |
{ | |
} | |
} | |
} | |
using System; | |
using System.Net.Sockets; | |
using System.Net; | |
using System.Text; | |
using System.Threading; | |
class SearchServer | |
{ | |
const int PORT_NUMBER = 5555; | |
static public string ipServer = ""; | |
Thread t = null; | |
public string getIpServer(){ | |
return SearchServer.ipServer; | |
} | |
public void Start() | |
{ | |
if (t != null) | |
{ | |
throw new Exception("Already started, stop first"); | |
} | |
if (SearchServer.ipServer !=""){ | |
return; | |
} | |
Console.WriteLine("Started listening"); | |
StartListening(); | |
} | |
public void Stop() | |
{ | |
try | |
{ | |
udp.Close(); | |
Console.WriteLine("Stopped listening"); | |
} | |
catch { /* don't care */ } | |
} | |
private readonly UdpClient udp = new UdpClient(PORT_NUMBER); | |
IAsyncResult ar_ = null; | |
private void StartListening() | |
{ | |
ar_ = udp.BeginReceive(Receive, new object()); | |
} | |
private void Receive(IAsyncResult ar) | |
{ | |
IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER); | |
byte[] bytes = udp.EndReceive(ar, ref ip); | |
string message = Encoding.ASCII.GetString(bytes); | |
Console.WriteLine("From {0} received: {1} ", ip.Address.ToString(), message); | |
if (message == "is machine info server"){ | |
SearchServer.ipServer = ip.Address.ToString(); | |
} | |
Console.WriteLine("From {0} received: {1} ", ip.Address.ToString(), message); | |
StartListening(); | |
} | |
public void Send(string message) | |
{ | |
UdpClient client = new UdpClient(); | |
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("255.255.255.255"), PORT_NUMBER); | |
byte[] bytes = Encoding.ASCII.GetBytes(message); | |
client.Send(bytes, bytes.Length, ip); | |
client.Close(); | |
Console.WriteLine("Sent: {0} ", message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment