///////////////////////////////////////
Windows中正在使用的端口如何关闭
·首先确认被占用的端口号,就拿443端口来说
2·然后就是查看443端口的程序PID;
① 进入cmd界面(快捷键win+R或者点击运行,输入cmd,进入cmd界面)
②输入 netstat -ano|findstr 443
③查看第二列ip后面数字为443的程序,最后一列是PID
④记录端口号为443的PID值
3·最后打开任务管理器,查看PID列,找到记录的PID值,点击结束进程即可
/////////////////////////////////////////
最近在使用结构体与字节数组转化来实现socket间数据传输。现在开始整理一下。对于Marshal可以查阅msdn,关于字节数组与结构体转代码如下:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.IO;
using
System.Runtime.InteropServices;
namespace
FileSendClient
{
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
struct
StructDemo
{
public
byte
a;
public
byte
c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public
byte
[] b;
public
byte
d;
public
int
e;
}
unsafe
class
Program
{
static
void
Main(
string
[] args)
{
StructDemo sd;
sd.a = 0;
sd.d = 0;
sd.c = 0;
sd.b =
new
byte
[3] { 0, 0, 1 };
sd.e = 5;
int
size = 0;
//此处使用非安全代码来获取到StructDemo的值
unsafe
{
size = Marshal.SizeOf(sd);
}
byte
[] b = StructToBytes(sd,size);
ByteToStruct(b,
typeof
(StructDemo));
}
//将Byte转换为结构体类型
public
static
byte
[] StructToBytes(
object
structObj,
int
size)
{
StructDemo sd;
int
num = 2;
byte
[] bytes =
new
byte
[size];
IntPtr structPtr = Marshal.AllocHGlobal(size);
//将结构体拷到分配好的内存空间
Marshal.StructureToPtr(structObj, structPtr,
false
);
//从内存空间拷贝到byte 数组
Marshal.Copy(structPtr, bytes, 0, size);
//释放内存空间
Marshal.FreeHGlobal(structPtr);
return
bytes;
}
//将Byte转换为结构体类型
public
static
object
ByteToStruct(
byte
[] bytes, Type type)
{
int
size = Marshal.SizeOf(type);
if
(size > bytes.Length)
{
return
null
;
}
//分配结构体内存空间
IntPtr structPtr = Marshal.AllocHGlobal(size);
//将byte数组拷贝到分配好的内存空间
Marshal.Copy(bytes, 0, structPtr, size);
//将内存空间转换为目标结构体
object
obj = Marshal.PtrToStructure(structPtr, type);
//释放内存空间
Marshal.FreeHGlobal(structPtr);
return
obj;
}
}
}
C#的UDP协议的异步实现
服务器端代码:
using System;
using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; namespace AsyncServer { // 定义 UdpState类 public class UdpState { public UdpClient udpClient; public IPEndPoint ipEndPoint; public const int BufferSize = 1024; public byte[] buffer = new byte[BufferSize]; public int counter = 0; } // 异步UDP类 public class AsyncUdpSever { // 定义节点 private IPEndPoint ipEndPoint = null; private IPEndPoint remoteEP = null; // 定义UDP发送和接收 private UdpClient udpReceive = null; private UdpClient udpSend = null; // 定义端口 private const int listenPort = 1100; private const int remotePort = 1101; UdpState udpReceiveState = null; UdpState udpSendState = null; // 异步状态同步 private ManualResetEvent sendDone = new ManualResetEvent(false); private ManualResetEvent receiveDone = new ManualResetEvent(false); public AsyncUdpSever() { // 本机节点 ipEndPoint = new IPEndPoint(IPAddress.Any, listenPort); // 远程节点 remoteEP = new IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], remotePort); // 实例化 udpReceive = new UdpClient(ipEndPoint); udpSend = new UdpClient(); // 分别实例化udpSendState、udpReceiveState udpReceiveState = new UdpState(); udpReceiveState.udpClient = udpReceive; udpReceiveState.ipEndPoint = ipEndPoint; udpSendState = new UdpState(); udpSendState.udpClient = udpSend; udpSendState.ipEndPoint = remoteEP; } public void ReceiveMsg() { Console.WriteLine("listening for messages"); while (true) { lock (this) { // 调用接收回调函数 IAsyncResult iar = udpReceive.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState); receiveDone.WaitOne(); Thread.Sleep(100); } } } // 接收回调函数 private void ReceiveCallback(IAsyncResult iar) { UdpState udpReceiveState = iar.AsyncState as UdpState; if (iar.IsCompleted) { Byte[] receiveBytes = udpReceiveState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint); string receiveString = Encoding.ASCII.GetString(receiveBytes); Console.WriteLine("Received: {0}", receiveString); //Thread.Sleep(100); receiveDone.Set(); SendMsg(); } } // 发送函数 private void SendMsg() { udpSend.Connect(udpSendState.ipEndPoint); udpSendState.udpClient = udpSend; udpSendState.counter++; string message = string.Format("第{0}个UDP请求处理完成!", udpSendState.counter); Byte[] sendBytes = Encoding.Unicode.GetBytes(message); udpSend.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), udpSendState); sendDone.WaitOne(); } // 发送回调函数 private void SendCallback(IAsyncResult iar) { UdpState udpState = iar.AsyncState as UdpState; Console.WriteLine("第{0}个请求处理完毕!", udpState.counter); Console.WriteLine("number of bytes sent: {0}", udpState.udpClient.EndSend(iar)); sendDone.Set(); } // 主函数 public static void Main() { AsyncUdpSever aus = new AsyncUdpSever(); Thread t = new Thread(new ThreadStart(aus.ReceiveMsg)); t.Start(); Console.Read(); } } }
客户端代码:
using System;
using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; namespace AsyncClient { // 定义 UdpState类 public class UdpState { public UdpClient udpClient = null; public IPEndPoint ipEndPoint = null; public const int BufferSize = 1024; public byte[] buffer = new byte[BufferSize]; public int counter = 0; } // 异步UDP类 public class AsyncUdpClient { public static bool messageSent = false; // Receive a message and write it to the console. // 定义端口 private const int listenPort = 1101; private const int remotePort = 1100; // 定义节点 private IPEndPoint localEP = null; private IPEndPoint remoteEP = null; // 定义UDP发送和接收 private UdpClient udpReceive = null; private UdpClient udpSend = null; private UdpState udpSendState = null; private UdpState udpReceiveState = null; private int counter = 0; // 异步状态同步 private ManualResetEvent sendDone = new ManualResetEvent(false); private ManualResetEvent receiveDone = new ManualResetEvent(false); // 定义套接字 //private Socket receiveSocket; //private Socket sendSocket; public AsyncUdpClient() { // 本机节点 localEP = new IPEndPoint(IPAddress.Any, listenPort); // 远程节点 remoteEP = new IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], remotePort); // 实例化 udpReceive = new UdpClient(localEP); udpSend = new UdpClient(); // 分别实例化udpSendState、udpReceiveState udpSendState = new UdpState(); udpSendState.ipEndPoint = remoteEP; udpSendState.udpClient = udpSend; udpReceiveState = new UdpState(); udpReceiveState.ipEndPoint = remoteEP; udpReceiveState.udpClient = udpReceive; //receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //receiveSocket.Bind(localEP); //sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //sendSocket.Bind(remoteEP); } // 发送函数 public void SendMsg() { udpSend.Connect(remoteEP); //Thread t = new Thread(new ThreadStart(ReceiveMessages)); //t.Start(); Byte[] sendBytes; string message; while (true) { message = "Client" + (counter++).ToString(); lock (this) { sendBytes = Encoding.ASCII.GetBytes(message); udpSendState.counter = counter; // 调用发送回调函数 udpSend.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), udpSendState); sendDone.WaitOne(); Thread.Sleep(200); ReceiveMessages(); } } } // 发送回调函数 public void SendCallback(IAsyncResult iar) { UdpState udpState = iar.AsyncState as UdpState; if (iar.IsCompleted) { Console.WriteLine("第{0}个发送完毕!", udpState.counter); Console.WriteLine("number of bytes sent: {0}", udpState.udpClient.EndSend(iar)); //if (udpState.counter == 10) //{ // udpState.udpClient.Close(); //} sendDone.Set(); } } // 接收函数 public void ReceiveMessages() { lock (this) { udpReceive.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState); receiveDone.WaitOne(); Thread.Sleep(100); } } // 接收回调函数 public void ReceiveCallback(IAsyncResult iar) { UdpState udpState = iar.AsyncState as UdpState; if (iar.IsCompleted) { Byte[] receiveBytes = udpState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint); string receiveString = Encoding.Unicode.GetString(receiveBytes); Console.WriteLine("Received: {0}", receiveString); receiveDone.Set(); } } // 主函数 public static void Main() { AsyncUdpClient auc = new AsyncUdpClient(); auc.SendMsg(); Console.Read(); } } } |