记录一下
获得本机IP
char hostname[128];
gethostname(hostname,128);
struct hostent *pHost = gethostbyname(hostname);
for (int i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; i++)
{
printf("%s\n",inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]));
localIP += inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]);
}
WSACleanup();
发送按钮处理函数
void CUdpDlg::OnOK() { // TODO: Add extra validation here CWnd* pWnd = GetDlgItem(IDC_EDIT1); CString msg,ip,port; pWnd->GetWindowText(msg); pWnd = GetDlgItem(IDC_EDIT2); pWnd->GetWindowText(ip); pWnd = GetDlgItem(IDC_EDIT3); pWnd->GetWindowText(port); Sendudp(msg.GetBuffer(0),ip.GetBuffer(0),atoi(port.GetBuffer(0))); }
HANDLE hThread; int recstart = 0; SOCKET socketSrv; void CUdpDlg::OnButton1() { // TODO: Add your control notification handler code here LPDWORD ThreadID=0; if(!recstart) { hThread=CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)ReciveUdp, NULL,0, ThreadID); GetDlgItem(IDC_BUTTON1)->SetWindowText("停止"); //GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE); recstart = 1; } else { TerminateThread(hThread,0); FreeConsole(); closesocket(socketSrv); WSACleanup(); GetDlgItem(IDC_BUTTON1)->SetWindowText("接收"); recstart = 0; } }
//----------------------------------------------- void Sendudp(char *msg,char *ip,int port) { /* socket编程三步骤 1、创建套接字socket 2、发送数据 3、关闭套接字 */ WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ return; } /* Confirm that the WinSock DLL supports 2.2.*/ /* Note that if the DLL supports versions greater */ /* than 2.2 in addition to 2.2, it will still return */ /* 2.2 in wVersion since that is the version we */ /* requested. */ if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup( ); return; } SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0); SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(port); sendto(sockClient,msg, strlen(msg)+1,0, (SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); closesocket(sockClient); WSACleanup(); } void ReciveUdp() { /* 1、创建socket 2、将套接字绑定到一个本地地址和端口上bind 3、等待接受数据receive from 4、关闭套接字。 */ WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ return; } /* Confirm that the WinSock DLL supports 2.2.*/ /* Note that if the DLL supports versions greater */ /* than 2.2 in addition to 2.2, it will still return */ /* 2.2 in wVersion since that is the version we */ /* requested. */ if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup( ); return; } //SOCKET socketSrv; socketSrv=socket(AF_INET,SOCK_DGRAM,0); SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(5000); bind(socketSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); SOCKADDR_IN addrClient; int len=sizeof(SOCKADDR); char recvBuf[1024]; AllocConsole(); //打开控制台 freopen("CONOUT$","w+t",stdout); freopen("CONIN$","r+t",stdin); while(1) { recvfrom(socketSrv,recvBuf,1024,0,(SOCKADDR*)&addrClient,&len); printf("%s\n",recvBuf); } // FreeConsole(); // closesocket(socketSrv); // WSACleanup(); }