void CtsclIEntDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知處理程序代碼
Word wVersionRequested;
WSADATA wsaData;
int err;
int nScokErr;
wVersionRequested=MAKEWord(1,1);
//生成版本號1.1
err=WSAStartup(wVersionRequested,&wsaData);
if(err!=0)
{
MessageBox("找不到合適的Dll文件","錯誤",MB_OK);
return;
}
//確定WinSock Dll是否支持1.1版本
//如果Dll支持高於1.1版本。確定返回的版本號是客戶要求的1.1
if(LOBYTE(wsaData.wVersion)!=1||HIBYTE(wsaData.wVersion)!=1)
{
MessageBox("Dll版本不適合本應用程序!","錯誤",MB_OK);
WSACleanup();
return;
}
SOCKET sClientSock; sockaddr_in addr; int nSockErr; //創建套接字 sClientSock=socket(AF_INET,SOCK_STREAM,0); addr.sin_family=AF_INET; addr.sin_port=0;//自動分配端口 addr.sin_addr.s_addr=htons(INADDR_ANY); //為套接字分配地址 if(bind(sClIEntSock,(LPSOCKADDR)&addr,sizeof(addr))==SOCKET_ERROR) { nSockErr=WSAGetLastError(); MessageBox("綁定套接字時出錯:"+nSockErr,"錯誤",MB_OK); WSACleanup(); return; } sockaddr_in ServAddr; ServAddr.sin_family =AF_INET; ServAddr.sin_port=htons(6000); ServAddr.sin_addr.s_addr=inet_addr("192.168.0.252"); //連接到服務器 if(connect(sClientSock,(struct sockaddr *)&ServAddr,sizeof(ServAddr))==SOCKET_ERROR) { nScokErr=WSAGetLastError(); MessageBox("連接錯誤!","錯誤",MB_OK); } MessageBox("連接成功!","成功",MB_OK); Sleep(3000); char FAR *buf; buf="1234567890123456"; send(sClientSock,buf,16,0); closesocket(sClIEntSock);
OnOK();
}