I have a code in VB6 that uses MSWINSCK.OCX to send commands to an equipment, the code is below. But I have to switch to C # and am attempting to use the Sockets class, the problem I'm facing is that the C # program, using Sockets, is not getting the response from the device. In VB6, using MSWINSCK.OCX, it works normal I can send the commands and receive the response.
VB6 code
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();
}
}