Hello i have a code:
public class Network
{
public Socket read;
public IPEndPoint ie;
public Network(string ip)
{
read = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ie = new IPEndPoint(IPAddress.Parse(File.ReadAllText("c:\\ipaddress.txt")), 502);
}
public bool InitConnection()
{
read.Connect(ie);
return read.Connected;
}
public int send(byte[] data)
{
return read.Send(data);
}
public byte[] receive()
{
byte[] data = new byte[41];
int num = read.Receive(data);
return data;
}
public void close_connection()
{
//read.Close();
read.Shutdown(SocketShutdown.Both);
}
public void refreshSocket()
{
read.Shutdown(SocketShutdown.Both);
}
}
- The above class is used to establish socket connection with a device.
- In the main class:
SCENARIO1:
static void main()
{
byte[] data = new data[]{0x01,0x02,0x44,0x34,0x23,0x11};
Networking net = new Networking();
if(net.InitConnection())
{
net.send(data);
net.receive();
}
}
SCENARIO2:
static void main()
{
byte[] data = new data[]{0x01,0x02,0x44,0x34,0x23,0x11};
byte[] ret = new ret[41];
Socket read = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
read.Connect("172.16.3.230", 502);
read.Send(data);
read.Receive(ret);
foreach(byte b in ret)
Console.WriteLine(b.ToString("x2"));
}
Thanks,
- Rahul