Home > Back-end >  Devc write socket communication problems, a great god answers
Devc write socket communication problems, a great god answers

Time:11-27

#include
#include

# pragma comment (lib, "ws2_32. Lib")

Int main (int arg c, char * argv [])
{
//initialize the WSA
The WORD sockVersion=MAKEWORD (2, 2);
WSADATA wsaData;
If (WSAStartup (sockVersion, & amp; WsaData)!=0)
{
return 0;
}

//create a socket
SOCKET slisten=SOCKET (AF_INET SOCK_STREAM, IPPROTO_TCP);
If (slisten==INVALID_SOCKET)
{
Printf (" socket error!" );
return 0;
}

//binding IP and port
Sockaddr_in sin;
Sin. Sin_family=AF_INET;
Sin. The sin_port=htons (8888);
Sin. The sin_addr. S_un. S_addr=INADDR_ANY;
If (bind (slisten, (LPSOCKADDR) & amp; Sin, sizeof (sin))==SOCKET_ERROR)
{
Printf (" bind error!" );
}

//to start listening to
If (listen (slisten, 5)==SOCKET_ERROR)
{
Printf (" listen error!" );
return 0;
}

//loop receiving data
The SOCKET sClient;
Sockaddr_in remoteAddr;
Int nAddrlen=sizeof (remoteAddr);
Char revData [255].
While (true)
{
Printf (" waiting connection... \n");
SClient=accept (slisten, (SOCKADDR *) & amp; RemoteAddr, & amp; NAddrlen);
If (sClient==INVALID_SOCKET)
{
Printf (" accept error!" );
continue;
}
Printf (" receives a connection: % s \ r \ n ", inet_ntoa (remoteAddr. Sin_addr));

//receiving data
Int ret=recv (sClient revData, 255, 0);
If (ret & gt; 0)
{
RevData (ret)=0 x00;
The printf (revData);
}

//send data
Const char * sendData publishes the event="HTTP://https://bbs.csdn.net/topics/: hello, TCP client!/n ";
Send (sClient sendData publishes the event, strlen (sendData publishes the event), 0).
Closesocket (sClient);
}

Closesocket (slisten);
WSACleanup();
return 0;
}


This is a code, error is as follows:
C: \ Users \ W \ AppData \ Local \ Temp \ ccZUV5Ry o tryc. CPP: (. The text + 0 x32) : undefined reference to ` __imp_WSAStartup '
C: \ Users \ W \ AppData \ Local \ Temp \ ccZUV5Ry o tryc. CPP: (. The text + 0 x5e) : undefined reference to ` __imp_socket '
C: \ Users \ W \ AppData \ Local \ Temp \ ccZUV5Ry o tryc. CPP: (. The text + 0 x9c) : undefined reference to ` __imp_htons'
C: \ Users \ W \ AppData \ Local \ Temp \ ccZUV5Ry o tryc. CPP: (. The text + 0 XDB) : undefined reference to ` __imp_connect '
C: \ Users \ W \ AppData \ Local \ Temp \ ccZUV5Ry o tryc. CPP: (. The text + 0 x104) : undefined reference to ` __imp_closesocket '
C: \ Users \ W \ AppData \ Local \ Temp \ ccZUV5Ry o tryc. CPP: (. The text + 0 x1a0) : undefined reference to ` __imp_recv '
C: \ Users \ W \ AppData \ Local \ Temp \ ccZUV5Ry o tryc. CPP: (. The text + 0 x1db) : undefined reference to ` __imp_closesocket '
E: \ \ computer network operation four \ collect2 exe [Error] ld returned 1 exit status

CodePudding user response:

There should be no contain the necessary header files,

CodePudding user response:

#include

# pragma comment (lib, "ws2_32")

CodePudding user response:

Do you have the library system in
Your search

CodePudding user response:

For using MinGW dev is no ws2_32. Lib this library file, its corresponding library file is libws2_32. A;
Can be added in the project properties linker of the library files, or in the compilation environment - lws2_32 link command to join parameters

CodePudding user response:

Give it up, anyway I carry a lot of code, the last line of the [Error] ld returned 1 exit status is to not to drop,

CodePudding user response:

V here is to want to add # pragma comment (lib, "ws2_32. Lib"), devc++ used,

CodePudding user response:

Use vc + +, dev++ is bad to use!

CodePudding user response:

V is the , the highest utilization rate, after all, this is the work of the software

CodePudding user response:

Add libws2_32. A and linwsock32. A to link in the library

CodePudding user response:

To be continued, 9/f,
reference _1006 response:
v is the , the highest utilization rate, after all, this is the work of the software

VS is the path, but VS have made a lot of things to help you,
If want to learn how to configure, v or a lack of one
  • Related