Home > Software engineering >  Socket client small problems
Socket client small problems

Time:11-20

When the program to connect, has been an error, please leaders see, when the compiler used vs 2017
 
# include "PCH. H"
#include
#include
#include//inet_pton
# pragma comment (lib, "ws2_32. Lib")//static connection network library
Int main ()
{
//in a corresponding lib library WSAStartup
WORD version=MAKEWORD (2, 1);
WSADATA MSG.
Int a=WSAStartup (version, & amp; MSG);//the first parameter is specified in a DLL version, the second parameter specifies information about the operation environment of winsock
If (a!=0) {
STD: : cout & lt; <"Open network library failed" & lt; }
The else {
//situations successfully
//create a socket
The SOCKET num=SOCKET (AF_INET, SOCK_STREAM, 0);
If (num==INVALID_SOCKET) {
STD: : cout & lt; <"Failed to create a socket & lt;" }
The else {
STD: : cout & lt; <"Success to create a socket, socket Numbers for" & lt; //client
//connect to other host
//1. Create the corresponding AF family agreement, IP address and port structure
SOCKADDR_IN hello;
Hello. Sin_family=AF_INET;
Hello. Sin_port=htons (4484);//by CMD netstat command to view the currently running a program of the port
//hello. Sin_addr. S_addr=inet_addr (" 127.0.0.1 ");
//use in vs 2017 inet_pton to IP addresses to be included in the structure
Inet_pton (AF_INET, "127.0.0.1," (void *) & amp; Hello);//127.0.0.1 here on behalf of the native
/* this connection is the machine, so just specify the corresponding port number, don't know why not here? */

If (connect (num, (SOCKADDR *) & amp; Hello, sizeof (hello))==0)//returns 0 when the connection is successful, not successful return 1
{
STD: : cout & lt; <"The connection is successful" & lt;





If (closesocket (num)==0)
STD: : cout & lt; <"Disconnected" success & lt; The else
STD: : cout & lt; }
The else
STD: : cout & lt; <"Connection failure" & lt;
WSACleanup ();//shut down the network library
}

}





}

*/

CodePudding user response:

The server is normal?
Try a modified version version=MAKEWORD (2, 2);
Before you connect to set breakpoints, view the hello is normal?
What is the error code WSAGetLastError watch?


CodePudding user response:

reference 1st floor zgl7903 response:
server is normal?
Try a modified version version=MAKEWORD (2, 2);
Before you connect to set breakpoints, view the hello is normal?
What is the error code WSAGetLastError watch?

Find the reason, inet_pton the function of the third argument is wrong, should be to (void *) hello. Sin_addr. S_un. S_addr
  • Related