char hostname[128]; hostent* hn; gethostname(hostname,128); hn = gethostbyname(hostname); IP 地址=inet_ntoa(*(struct in_addr *)hn->h_addr_list[0]) IP_ADAPTER_INFO ipAdaInfo; ULONG ipSize=sizeof(ipAdaInfo); if(GetAdaptersInfo(&ipAdaInfo,&ipSize)==ERROR_SUCCESS){ AfxMessageBox(ipAdaInfo.IpAddressList.IpAddress.String);//本机IP 地址 } 你完全不必自己取IP,客户端接受socket 连接后可以根据 IP 包直接得到IP 地址,取法为 sockaddr_in remoteServer; accept(listenSock,&remoteServer,sizeof(remoteServer)); char* cIP=inet_ntoa(remoteServer.sin_addr);//得到该数据包源IP ring 的方法更通用,可以查别的机器的IP. 如果你要写Internet 程序,就不能这样去取 IP 地址,这样取的是局域网IP,不是你在Internet 上的真IP,一定得从 IP 中取得IP 地址,即上面的 第二个方法. Socket api Client: #ifndef UDPClientH #define UDPClientH #include #include #include #include #include #include "CCEdit.h" #define WM_SOCK WM_USER+100 class TLANForm : public TForm { __published: // IDE-managed Components TEdit *Port; TLabel *Label1; TLabel *Label2; TComboBox *Prot; TButton *Button1; TLabel *Label3; TEdit *Addr; TCCEdit *TxtEdit; void __fastcall FormCreate(TObject *Sender); void __fastcall Button1Click(TObject *Sender); void __fastcall FormDestroy(TObject *Sender); private: // User declarations void __fastcall OnRecv(TMessage &Message); public: // User declarations __fastcall TLANForm(TComponent* Owner); BEGIN_MESSAGE_MAP VCL_MESSAGE_HANDLER(WM_SOCK,TMessage,OnRecv); END_MESSAGE_MAP(TForm); }; extern PACKAGE TLANForm *LANForm; #endif .cpp File #include #pragma hdrstop #include "UDPClient.h" #include "WinSock.h" #pragma package(smart_init) #pragma link "CCEdit" #pragma resource "*.dfm" TLANForm *LANForm; enum PROTO {TCP=0,UDP=1}; SOCKET m_Socket=INVALID_SOCKET; PROTO m_Protocol=TCP; __fastcall TLANForm::TLA...