0

I have simple TCP/IP Server Windows and Client Android. How I can send received text to another appilication for example notepad or excel cell, any window application with input field. Data Received in background

 private void Events_DataRecieved(object sender, SimpleTcp.DataReceivedEventArgs e)
    {
        var ipandPort = e.IpPort;
        var data = Encoding.UTF8.GetString(e.Data);
        Console.WriteLine(data);  
        Send(data);
    }

I try this

 [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    public void Send(String data)
    {
        System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad"); //search for process notepad
        if (p.Length > 0) //check if window was found
        {
            SetForegroundWindow(p[0].MainWindowHandle); //bring notepad to foreground
        }

        SendKeys.SendWait(data); //send key to notepad
    }

How I can send text to any application runnuble on PC after DataRecieved. Like https://barcodetopc.com/ here Thx for help.

Mike
  • 1
  • 1

2 Answers2

0

As far as I can see you are using the SimpleTcp Nuget package. You can use several 3rd party applications to control the data exchange. Examples of these applications are Hercules or Docklight.

You can create a server in Hercules with your own IP address and the port you specify. Just come to the server tab and say Listen.

In C# application, you can first create a Client.

string IpAddress = "192.168.1.40"; // Example Ip Address
string Port = "23";

public SimpleTcpClient tcpClient = new SimpleTcpClient(Ip, Convert.ToInt32(Port), false, null, null);
tcpClient.Connect();

if (tcpClient.IsConnected)
{
    tcpClient.Events.DataReceived += Events_DataReceived;
    tcpClient.Events.Connected += Events_Connected;
    tcpClient.Events.Disconnected += Events_Disconnected;
}

If you want to listen to messages sent to you

private void Events_DataRecieved(object sender, SimpleTcp.DataReceivedEventArgs e)
{
        string IpPort = e.IpPort;
        var data = Encoding.UTF8.GetString(e.Data);
        Console.WriteLine(data);
}

If you want to send a message

byte[] message = new byte[] {0x00, 0x01, 0x02, 0x03};
private void EthernetSend()
{
    
    tcpClient.Send("Message"); // If you want to send a string expression
    tcpClient.Send(message); // If you want to send a hexadecimal expression
}
Ozgur Saklanmaz
  • 528
  • 3
  • 17
  • I already have client and server, I can recieve message from clent by anroid and Server. Question is how send recived data to Another Application like Execel for example https://barcodetopc.com/ – Mike Sep 20 '21 at 11:55
  • Sorry, I misunderstood. Writing or reading data to Excel is a very different task. Does [this](https://stackoverflow.com/questions/23041021/how-to-write-some-data-to-excel-file-xlsx) link solve your problem? – Ozgur Saklanmaz Sep 21 '21 at 05:34
0

Solved problem by using Clipboard

    public void Send2(String data)
    {

        Thread thread = new Thread(() => System.Windows.Clipboard.SetText(data));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();

        SendKeys.SendWait("^v");

    }
Mike
  • 1
  • 1