当前位置:编程学习 > Delphi >>

Delphi socket连接.net Socket

[csharp] 
  
这几天一直研究Delphi连接.net的socket程序,终于有一些进展。
需求:
服务端截取前4个字节,转换为数字,次数字为业务代码。将决定调用哪个业务逻辑。
[html]  
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)  
                {  
                    case GETVERSION:  
                        log.Info(cliendAddres + "获取最新版本,请求版本号:" + c);  
                        GetVersionNo(c, handler);  
                        break;  
                    case DOWNLOADNEW:  
                        log.Info(cliendAddres + "下载更新配置文件:" + INSTALLPATH+"/"+c);  
                        DownLoadNew(c, handler);  
                        break;  
                    case DOWNLOADFILE:  
                        log.Info(cliendAddres + "下载文件," + INSTALLPATH+"/"+c);  
                        DownLoadFile(INSTALLPATH + "/" + c, handler);  
                        break;  
                    default:  
                        break;  
                }  
  
            }  
            catch (Exception e)  
            {  
                log.Error(cliendAddres + "程序出错," + e.Message);  
            }  
            finally  
            {  
                if (handler != null)  
                {  
                    handler.Shutdown(SocketShutdown.Both);  
                    handler.Close();  
                    handler = null;  
                    log.Info("与客户端:" + cliendAddres + "断开连接。");  
                }  
            }  
             
        }  
  
        public void DownLoadFile(string filePath,Socket handler)  
        {  
            if (!File.Exists(filePath))  
            {  
                string fail = "文件不存在.";  
                log.Info(cliendAddres+"访问文件:"+filePath+"不存在。");  
                byte[] failBytes = Encoding.UTF8.GetBytes(fail);  
                handler.BeginSend(failBytes, 0, failBytes.Length, 0, new AsyncCallback(SendCallBack), handler);  
                allDone.WaitOne();  
                return;  
            }  
  
            h
补充:软件开发 , Delphi ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,