这几天一直研究Delphi连接.net的socket程序,终于有一些进展。需求:
服务端截取前4个字节,转换为数字,次数字为业务代码。将决定调用哪个业务逻辑。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using PivasUpdate;
using log4net;
using System.Reflection;
using System.Net;
namespace PivasUpdate
{
public class ServiceProcess
{
private const int LENGTH=1024;
private const int GETVERSION = 1;
private const int DOWNLOADNEW = 2;
private const int DOWNLOADFILE = 3;
public static AutoResetEvent allDone = new AutoResetEvent(false);
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private string cliendAddres;
private string INSTALLPATH = ConfigUtils.GetWindowsServiceInstallPath(ConfigUtils.SERVICENAME);
public void ProcessCallback(IAsyncResult ar)
{
Socket handler = null;
try
{
StateObject state = (StateObject)ar.AsyncState;
handler = state.workSocket;
cliendAddres = IPAddress.Parse(((IPEndPoint)handler.RemoteEndPoint).Address.ToString()) + ":"
+ ((IPEndPoint)handler.RemoteEndPoint).Port.ToString() + ":";
int bytesRead = handler.EndReceive(ar);
log.Info(cliendAddres + "接受到数据包大小:" + bytesRead + "字节");
int control = BitConverter.ToInt32(state.buffer, 0);
string c = UTF8Encoding.UTF8.GetString(state.buffer, 4, bytesRead - 4);
switch (control)
{