socket通讯

同步通讯

只要知道remoteSocket IPaddress和Port即可

被动接收信息时,需要长连接

/*CustomerTimer*/
public class CustomerTimer
{
	public DateTime(Duetime(double time_ms)
	{
		return DateTime.Now.AddMilliseconds(time_ms);
	}
	public bool Timeout(DateTime duetime)
	{
		TimeSpan diff=duetime-DateTime.Now;
		if(diff.TotalSeconds>0)
			return false;
		else
			return true;
	}
	public void Delay(int time_ms)
	{
		Thread.Sleep(time_ms);
	}
}
public static CustomerTimer checkTime;
/* connect*/
public bool SocketConnect()
	Socket remoteSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, 				ProtocolType.Tcp);
	remoteSocket.SendTimeout = 2000;
	remoteSocket.ReceiveTimeout = 2000;
	IAsyncResult ar = remoteSocket.BeginConnect(IPAddress.Parse(equip_ip), equip_port, 			null, null);
	DateTime connDueTime = checkTime.Duetime(T3);
	while (!ar.IsCompleted)
	{
		if (checkTime.Timeout(connDueTime))
		{
			break;
		}
	}
	return remoteSocket.Connected;
} 

/*send remote ip and address message*/
public bool SendMsg(byte[] sendMsg)
{
	int waitTime=3;
	int sendPoint=0;
	int curSend=0;
	bool result=false;
	while(waitTime>0)
	{
		if(sendPoint<sendMsg.Length)
		{
			curSend=remoteSocket.Send(sendMsg,sendPoint,sendMsg.Length-sendPoint,SocketFlags.None);
			sendPoint +=curSend;
		}
		else
		{
			result = true;
			break;
		}
		waitTime--;
		Thread.Sleep(100);
	}
	reture result;
}      
/*receive data*/
public bool ReceiveData(Socket remoteSocket)
{
	int count,smgLength;
	bool isReceived=false;
	count=remoteSocket.Available;
	recTimeout=checTime.DueTime(5000);
	while(count<=4)
	{
		checkTime.Delay(100);
     	count=remoteSocket,Available;
		if(checkTime.Timeout(recTimeout))
 		{
 			return isReceived;
  		}
  		if(isAppClosed)
  			return isReceived;

	}
	isReceived=true;
	return isReceived;
}    

异步通讯

发送要知道remote IPaddress和Port,接收要知道local端Port;

发送端发送后可以立即关闭

/*receive data from local ip and port */
private void WaitData()
{
	Socket localSocket = new new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 5004);
    localSocket.Bind(localEndPoint);
    localSocket.Listen(150);
    while(true)
    {
    		int count, msgLength;
    		count = localSocket.Available;
    		 recTimeout = checkTime.Duetime(5000);
    		 while(count<=4)
    		 {
    		 	checkTime.Delay(100);
    		 	count = localSocket.Available;
    		 	if(checkTime.Timeout(recTimeout))
    		 	{
    		 		break;
    		 	}
    		 }
    		 byte[] receiveBytes=new byte[count];
    		 receiveBytes.Receive(receiveBytes);
    		 ....
    }
}
/*send data to remote ip and port */
private void SendMessage()
{
	SocketConnect();
	...
	if(SendMsg(byte[] sendMsg))
	{
		remoteSocket.Shutdown(SocketShutdown.Send);
  		remoteSocket.close()

	}
	...
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fwsylin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值