快速业务通道

CB嶄喘socket api亟利大宥儷殻會

作者 佚名技术 来源 程序设计 浏览 发布时间 2012-06-30
 if(m_Protocol==TCP){
    if(connect(m_Socket,(struct sockaddr*)&To,sizeof(struct sockaddr))){
      sprintf(szTmp,"Failed to connect the object!,error no:%d",::WSAGetLastError());
      ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
      closesocket(m_Socket);
      ::WSACleanup();
      return;
    }
    int nError=select(1,0,&FDSET,0,0);
    if(nError<=0){
    sprintf(szTmp,"Failed to select socket!,error no:%d",::WSAGetLastError());
    closesocket(m_Socket);
    ::WSACleanup();
    return;}
   }
   //Send data
   int nLen=TxtEdit->Text.Length();
   if(nLen>4096){
    sprintf(szTmp,"The buffer is too size to send,it shoud not be more than 4096 bytes!");
    ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
    closesocket(m_Socket);
    ::WSACleanup();
    return;
   }
   strncpy(buf,TxtEdit->Text.c_str(),nLen);
   switch(m_Protocol)
   {
    case TCP:
      nSize=send(m_Socket,buf,nLen,0);
      //ShowMessage(nSize);
      break;
    case UDP:
      nSize=sendto(m_Socket,buf,nLen,0,(struct sockaddr*)&To,sizeof(struct sockaddr));
      //ShowMessage(nSize);
      break;
   }
   if(::WSAAsyncSelect(m_Socket,Handle,WM_SOCK,FD_READ|FD_CLOSE)){
    sprintf(szTmp,"Failed to register socket event!,error no:%d",::WSAGetLastError());
    ::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
    closesocket(m_Socket);
    ::WSACleanup();
    return;}
}
void __fastcall TLANForm::FormDestroy(TObject *Sender)
{
   closesocket(m_Socket);
   ::WSACleanup();
}
Socket api Server:
.h File
#ifndef UDPServerH
#define UDPServerH
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <stdio.h>
#include "WinSock.h"
/*#define WM_SOCKET WM_USER+1000
#define INITSOCKETSUCCESS 0
#define INITSOCKETFAILURE 1
#define CREATELISTENSOCKETSUCCESS 2
#define CREATELISTENSOCKETFAILURE 3
#define SETLISTENSOCKETSUCCESS 4
#define SETLISTENSOCKETFAILURE 5
#define BINDLISTENSOCKETSUCCESS 6
#define BINDLISTENSOCKETFAILURE 7
#define LISTENSOCKETSUCCESS 8
#define LISTENSOCKETFAILURE 9
#define ACCEPTSOCKETSUCCESS 10
#define ACCEPTSOCKETFAILURE 11
*/
class TPSTNForm : public TForm
{
__published: // IDE-managed Components
   TEdit *Port;
   TLabel *Label1;
   TMemo *Memo1;
   TButton *Button1;
   TButton *Button2;
   TLabel *Label2;
   TComboBox *Prot;
   void __fastcall Button1Click(TObject *Sender);
   void __fastcall Button2Click(TObject *Sender);
   void __fastcall FormCreate(TObject *Sender);
   void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
public: // User declarations
   __fastcall TPSTNForm(TComponent* Owner);
};
enum PROTO {TCP,UDP};
class TCommunication:publi

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号