|
// m_Socket and client are global objects of SOCKET and struct sockaddr_in, respectively
void __fastcall TForm1 :: Button1Click (TObject * Sender)
{
WSADATA wsaD;
WORD wVersionRequested = MAKEWORD (1,1);
if (WSAStartup (wVersionRequested,&wsaD)! = 0) {
MessageDlg ("Error 1!", MtInformation, TMsgDlgButtons () << mbOK, 0);
return;
}
if ((m_Socket = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
MessageDlg ("Error 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 ("Error 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 ("Error 4!", MtInformation, TMsgDlgButtons () << mbOK, 0);
return;
}
}
The problem is this, after pressing Button1 to connect successfully (there is no error, the server can also receive a message that the connection was successful), why does it go wrong when I press Button2? Which hero can tell me which step is missing? |
|