Winsock (VB6) vs Socket in C#

Asked

Viewed 308 times

1

I have a code on VB6 that uses MSWINSCK.OCX to send commands to a device, the code is below. But I have to switch to C# and I’m trying to use the Sockets class, the problem I’m facing is that the C#program, using Sockets, is not getting the answer from the equipment. In VB6, using MSWINSCK.OCX, it works normally I can send commands and receive the answer.

Code VB6

Private Sub CmdClear_Click()
   TxtRcv.Text = ""
End Sub

Private Sub CmdSend_Click()
   WinSock.RemoteHost = "255.255.255.255" 'ip do equipamento'
   WinSock.SendData TxtSend.Text
End Sub

Private Sub WinSock_DataArrival(ByVal bytesTotal As Long)
   WinSock.GetData s$
   TxtRcv.Text = TxtRcv.Text + s$ + Chr$(13) + Chr$(10)
End Sub

C# code using Sockets

private static void Main(string[] args)
    {
        var host = IPAddress.Parse("192.168.25.200");

        var hostep = new IPEndPoint(host, 3000);

        var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Unspecified);

        try
        {
            sock.Connect(hostep);
        }
        catch (SocketException e)
        {
            Console.WriteLine("Problem connecting to host");
            Console.WriteLine(e.ToString());
            sock.Close();
            return;
        }

        try
        {
            sock.SendTo(Encoding.ASCII.GetBytes("V"), hostep);
        }
        catch (SocketException e)
        {
            Console.WriteLine("Problem sending data");
            Console.WriteLine(e.ToString());
            sock.Close();
            return;
        }

        var receivingUdpClient = new UdpClient(3000);

        try
        {
            var receiveBytes = receivingUdpClient.Receive(ref hostep);

            var returnData = Encoding.ASCII.GetString(receiveBytes);

            Console.WriteLine("Mensagem recebida " + returnData);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        sock.Close();

    }
}
  • Which equipment ?

  • is a Tibbo. http://www.tibbo.com

  • Have you seen the article by Sockets? https://msdn.microsoft.com/pt-br/library/system.net.sockets.socket(v=vs.110). aspx

  • Put Socket in listening mode (Socket.Listen())? If you are using a TCP protocol I recommend enabling and executing this method in the first socket declarations. if using UDP, check that IP protections are declared to Edge (Erestricted) or unrestricted (Unrestricted), to change the protection use the method SetIPProtection.

1 answer

0

You opened a socket called "Sock", made a connection, and sent a string, in this case the letter "V". But out of nowhere, you open a "Udpclient" and totally ignore the open socket called "Sock"... From what I understand you want to send a string and receive another string. Perhaps it is better to leave the socket aside and use a better class for this, the Tcpclient it is better because you can work with Stream which already makes it easier not to work directly with array of bytes. look at an example:

        var host = IPAddress.Parse("192.168.25.200");
        var hostep = new IPEndPoint(host, 3000);

        var tcpClient = new TcpClient(hostep);

        using (var wr = new StreamWriter(tcpClient.GetStream()))
        using (var rd = new StreamReader(tcpClient.GetStream()))
        {
            wr.WriteLine("V");
            wr.Flush();
            var retorno = rd.ReadLine();
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.