| |

VerySource

 Forgot password?
 Register
Search
View: 14|Reply: 6

SOCKET object sending message problem?

[Copy link]

1

Threads

3

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-1-10 19:20:01
| Show all posts |Read mode
// 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?
Reply

Use magic Report

0

Threads

18

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 China

Post time: 2020-1-16 14:18:01
| Show all posts
WSAGetLastError Take a look at what is wrong.
Reply

Use magic Report

1

Threads

3

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

 Author| Post time: 2020-1-18 02:18:01
| Show all posts
Returns 10093?
Reply

Use magic Report

1

Threads

3

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

 Author| Post time: 2020-1-18 10:54:01
| Show all posts
void __fastcall TListenThread :: Execute () {
    while (! Terminated) {
         if (m_AcceptSocket = accept (AcppetSocket, (struct sockaddr *)&client,&nLen)! = INVALID_SOCKET) {
             return;
         }
    }
}
? Nobody knows what
This is part of the server code for testing, m_AccptSocket and client are global objects
Reply

Use magic Report

0

Threads

18

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 China

Post time: 2020-1-21 11:27:01
| Show all posts
The application did not call WSAStartup, or WSAStartup failed.
Reply

Use magic Report

0

Threads

18

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 China

Post time: 2020-1-21 12:27:02
| Show all posts
The landlord should be able to add the ws2_32.lib file to the project.
Reply

Use magic Report

0

Threads

4

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

Post time: 2020-8-5 21:45:01
| Show all posts
Bangding
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list