C#如何实现网口通讯?

C#如何实现网口通讯?,第1张

网口通迅啊,你的意思是通过网络和别的计算机通信吗?如果是的话那就是socket通信了,我给你个例子看看\x0d\x0a服务器端:\x0d\x0atry\x0d\x0a{\x0d\x0aintport=2000\x0d\x0astringhost="176.64.158.112"\x0d\x0aIPAddressip=IPAddress.Parse(host)\x0d\x0aIPEndPointipe=newIPEndPoint(ip,port)\x0d\x0as=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)//创建一个Socket类\x0d\x0as.Bind(ipe)//绑定2000端口\x0d\x0as.Listen(0)//开始监听\x0d\x0aShowMessage("Waitforconnect")\x0d\x0awhile(true)\x0d\x0a{\x0d\x0aSockettemp=s.Accept()//为新建连接创建新的Socket。\x0d\x0aShowMessage("Getaconnectfrom"+temp)//this.Invoke(newShowMes(ShowMessage),"Getaconnect")\x0d\x0astringrecvStr=""\x0d\x0abyte[]recvBytes=newbyte[1024]\x0d\x0aintbytes\x0d\x0abytes=temp.Receive(recvBytes,recvBytes.Length,0)//从客户端接受信息\x0d\x0arecvStr=Encoding.ASCII.GetString(recvBytes,0,bytes)\x0d\x0aShowMessage(String.Format("ServerGetMessage:{0}",recvStr))//this.Invoke(newShowMes(ShowMessage),String.Format("ServerGetMessage:{0}",recvStr))//把客户端传来的信息显示出来\x0d\x0astringsendStr="Ok!ClientSendMessageSucessful!"\x0d\x0abyte[]bs=Encoding.ASCII.GetBytes(sendStr)\x0d\x0atemp.Send(bs,bs.Length,0)//返回客户端成功信息\x0d\x0a}\x0d\x0a//temp.Close()\x0d\x0a//s.Close()\x0d\x0a}\x0d\x0acatch(ArgumentNullExceptione)\x0d\x0a{\x0d\x0aShowMessage(String.Format("ArgumentNullException:{0}",e))\x0d\x0a}\x0d\x0acatch(SocketExceptione)\x0d\x0a{\x0d\x0aShowMessage(String.Format("SocketException:{0}",e))\x0d\x0a}\x0d\x0aConsole.WriteLine("PressEntertoExit")\x0d\x0a客户端:\x0d\x0atry\x0d\x0a{\x0d\x0aSocketc\x0d\x0aintport=2000\x0d\x0astringhost="176.64.158.112"\x0d\x0aIPAddressip=IPAddress.Parse(host)\x0d\x0aIPEndPointipe=newIPEndPoint(ip,port)//把ip和端口转化为IPEndPoint实例\x0d\x0ac=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)//创建一个Socket\x0d\x0aShowMessage("Conneting...")\x0d\x0ac.Connect(ipe)//连接到服务器\x0d\x0astringsendStr="hello!Thisisasockettest"\x0d\x0abyte[]bs=Encoding.ASCII.GetBytes(sendStr)\x0d\x0aShowMessage("SendMessage")\x0d\x0ac.Send(bs,bs.Length,0)//发送测试信息\x0d\x0astringrecvStr=""\x0d\x0abyte[]recvBytes=newbyte[1024]\x0d\x0aintbytes\x0d\x0abytes=c.Receive(recvBytes,recvBytes.Length,0)//从服务器端接受返回信息\x0d\x0aif(bytes 回答于 2022-12-14

socket-client

----------------------------------------------

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Text

using System.Windows.Forms

using System.Net

using System.Net.Sockets

using System.Threading

namespace socket_client

{

//客户端

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent()

Control.CheckForIllegalCrossThreadCalls = false

}

Socket s//聊天用

Thread th

//连接

private void button1_Click(object sender, EventArgs e)

{

//步骤1 配置远程服务器信息

IPEndPoint removeServer = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text))

//步骤2 创建套接字

s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

//步骤3 套接字连接远程服务器

s.Connect(removeServer)

//步骤4 提示连接状态

if (s.Connected)

{

label4.Text = "连接服务器成功!"

//步骤5 循环接收服务器发来的消息

th = new Thread(new ThreadStart(BB))

th.IsBackground = true

th.Start()

}

}

void BB()

{

while (true)

{

byte[] bb = new byte[1024]

int i= s.Receive(bb)//接收数据,返回每次接收的字节总数

string removeMsg = Encoding.Unicode.GetString(bb,0,i)

if (removeMsg == "CMD--EXIT")//收到的是退出通知

{

label4.Text = "无连接"

DialogResult re=MessageBox.Show("服务器已经关闭.\n\"确定\"后退出程序,\n\"取消\"继续停留!", "消息提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)

MessageBox.Show(re.ToString())

if (re == DialogResult.OK)

{

sendExit()//告诉服务器我退出了

Application.Exit()

}

return

}

richTextBox1.AppendText(removeMsg+"\n")

richTextBox1.ScrollToCaret()

}

}

//发送消息

private void button3_Click(object sender, EventArgs e)

{

string msg = "客户端:" + richTextBox2.Text

byte[] bb = Encoding.Unicode.GetBytes(msg)

s.Send(bb)

richTextBox2.Text = ""

richTextBox2.Focus()

}

//发送“客户端退出提示”

void sendExit()

{

string cmd = "CMD--EXIT"

byte[] bb = Encoding.Unicode.GetBytes(cmd)

s.Send(bb)

}

}

}

socket-server

--------------------------------------------------------------

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Text

using System.Windows.Forms

using System.Net

using System.Net.Sockets

using System.Threading

namespace socket_server

{

//服务器

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent()

Control.CheckForIllegalCrossThreadCalls = false//可以调试时,不捕捉控件创建线程错误

}

Thread th

Socket s1//监听用

Socket s2//聊天用

//监听

private void button1_Click(object sender, EventArgs e)

{

IPAddress ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0]

//步骤1 创建网络端点IPEndPoint

IPEndPoint myServer = new IPEndPoint(ip, 888)

//步骤2 创建套接字Socket

s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

//步骤3 套接字绑定到网络端点

s1.Bind(myServer)

label1.Text = ip+": 888 \n等待客户端连接......"

th = new Thread(new ThreadStart(AA))

th.IsBackground = true

th.Start()

}

void AA()

{

//步骤4 监听

s1.Listen(5)

//步骤5 接受客户端连接

s2 = s1.Accept()

//步骤6 判断连接状态

if (s2.Connected)

{

label1.Text = "已有客户端连接!"

//步骤7 循环接收客户端消息

while (true)

{

byte[] bb = new byte[1024]

int i= s2.Receive(bb)

string removeMsg = Encoding.Unicode.GetString(bb,0,i)

if (removeMsg == "CMD--EXIT")//收到的是退出通知

{

label1.Text = "客户端已经取消了连接"

return

}

richTextBox1.AppendText( removeMsg+"\n" )

richTextBox1.ScrollToCaret()

}

}

}

//停止监听

private void button2_Click(object sender, EventArgs e)

{

sendExit()//告诉客户端

s2.Shutdown(SocketShutdown.Both)

s1.Close()

th.Abort()

label1.Text = "无连接"

}

//发送消息

private void button3_Click(object sender, EventArgs e)

{

string msg = "服务器:" + richTextBox2.Text

byte[] bb = Encoding.Unicode.GetBytes(msg)

s2.Send(bb)

richTextBox2.Text = ""

richTextBox2.Focus()

}

//发送“服务器退出提示”

void sendExit()

{

string cmd = "CMD--EXIT"

byte[] bb = Encoding.Unicode.GetBytes(cmd)

s2.Send(bb)

}

}

}

因为不太了解GOOSE协议,几分钟简单google了一下,wikipedia上说的语焉不详,是这样的,在windows平台,用户态都是通过socket进行网络通信的,但socket是位于tcp/ip之上的一套 *** 作接口,它可以 *** 作tcp/udp数据,也可以通过raw方式 *** 作ip数据,但无论如何,都在网络层之上,如果GOOSE协议不是tcp/ip之上的协议,那么就得写ndis协议驱动了,以在pc侧网卡设备之上支持这种协议(或者幸运的话,对端设备提供了windows平台的驱动那更好,如果是这样,则查看用户手册,如何使用它)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/11529947.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存