public void DownloadDeviceId() { IPAdd = "192.168.0.6"; Port = 3000; UdpClient client = new UdpClient(); TDeviceId id = new TDeviceId(); try { IPEndPoint sep = new IPEndPoint(IPAddress.Parse(IPAdd), Port); client.Connect(sep); client.Send(new byte[] { 0x02, 0x00, 0x02, 0x01, 0x00, 0xFD, 0x03 }, 7); IPEndPoint rep = new IPEndPoint(IPAddress.Any, 0); IPEndPoint rep1 = new IPEndPoint(IPAddress.Any, 0); Byte[] recBytes = client.Receive(ref rep); Byte[] recBytes1 = client.Receive(ref rep1); id.Rec = recBytes1; RemoteEbs rem = new RemoteEbs(); rem.Rec = recBytes1; Console.WriteLine("Nawiązano Połączenie"); rem.StartServer(); client.Close(); } catch { Console.WriteLine("Niepoprawny adres lub nie można nawiązać połączenia", "Connection Error"); } }
public void StartServer() { //Console.WriteLine(SerialNum); TcpListener server = Create(Server); try { server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while (true) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); data = null; // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Received: {0}", data); // Process the data sent by the client. data = data.ToUpper(); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); // Send back a response. TDeviceId id = new TDeviceId(); if (data == "{CMD1") { string SerialNumber = ""; Console.WriteLine(id.SerialNumberMethod(ref SerialNumber, Rec)); byte[] SerialNumberByte = ConvertStringToByte(SerialNumber); stream.Write(SerialNumberByte, 0, SerialNumberByte.Length); } else if (data == "{CMD2") { string NameLenght = ""; Console.WriteLine(id.NameLengthMethod(ref NameLenght, Rec)); byte[] NameLenghtByte = ConvertStringToByte(NameLenght); stream.Write(NameLenghtByte, 0, NameLenghtByte.Length); } } // Shutdown and end connection client.Close(); } } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { // Stop listening for new clients. server.Stop(); } }