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 ?
– Roberto Araujo
is a Tibbo. http://www.tibbo.com
– JcSaint
Have you seen the article by Sockets? https://msdn.microsoft.com/pt-br/library/system.net.sockets.socket(v=vs.110). aspx
– Roberto Araujo
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 methodSetIPProtection.– CypherPotato