|
//m_Socket和client分别是SOCKET和struct sockaddr_in的全局对象
void __fastcall TForm1::Button1Click(TObject *Sender)
{
WSADATA wsaD;
WORD wVersionRequested = MAKEWORD(1,1);
if(WSAStartup(wVersionRequested, &wsaD) !=0 ){
MessageDlg("错误1!",mtInformation,TMsgDlgButtons()<<mbOK,0);
return;
}
if((m_Socket = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET ){
MessageDlg("错误2!",mtInformation,TMsgDlgButtons()<<mbOK,0);
return;
}
client.sin_family = AF_INET;
client.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
client.sin_port = htons( 878787 );
int len = sizeof(client);
if(connect( m_Socket, (struct sockaddr*)&client, sizeof( client)) == SOCKET_ERROR ){
MessageDlg("错误3!",mtInformation,TMsgDlgButtons()<<mbOK,0);
return;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
char buf[5] = {'1','2','3','4','5'};
if( send( m_Socket, buf, 5, 0 ) == SOCKET_ERROR ){
MessageDlg("错误4!",mtInformation,TMsgDlgButtons()<<mbOK,0);
return;
}
}
问题是这样的,按Button1连接成功(没有出错,服务器端也能收到连接成功的信息)之后,再按Button2为什么还会出错?哪位大侠能告诉我少了哪个步骤? |
|