----------------------------------------------
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平台的驱动那更好,如果是这样,则查看用户手册,如何使用它)欢迎分享,转载请注明来源:内存溢出
评论列表(0条)