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();
}