Home > Back-end >  Code on both ends of the total, to use Visual Studio c, what I use is Dev - c has an error, and boss
Code on both ends of the total, to use Visual Studio c, what I use is Dev - c has an error, and boss

Time:03-09

The first paragraph
#include
#include
# pragma comment (lib, "ws2_32. Lib")
Int main (int arg c, char * * argv)
{
///create a socket
WORD myVersionRequest;
WSADATA wsaData;
MyVersionRequest=MAKEWORD (1, 1);
Int err;
Err=WSAStartup (myVersionRequest, & amp; WsaData);
if (! Err) {
Printf (" open sockets \ n ");
} else {
Printf (" ERROR: nested word not open!" );
return 1;
}

///further bind socket
The SOCKET serSocket=SOCKET (AF_INET, SOCK_STREAM, 0);//create the identifiable socket

///need to bind the parameter
SOCKADDR_IN addr.
Addr. Sin_family=AF_INET;
Addr. Sin_addr. S_un. S_addr=htonl (INADDR_ANY);//IP address
Addr. Sin_port=htons (6000);//bind port

Bind (serSocket, (SOCKADDR *) & amp; Addr, sizeof (SOCKADDR));//bind complete
Listen (serSocket, 5);//the second parameter representative to receive the most number of connections

//////////////////////////////////////////////////////////////////////////
//monitor
//////////////////////////////////////////////////////////////////////////
SOCKADDR_IN clientsocket;
Int len=sizeof (SOCKADDR);
While (1)
{
The SOCKET serConn=accept (serSocket, (SOCKADDR *) & amp; Clientsocket, & amp; Len);//if not accept but conection here, and will continuously monitor
Char sendBuf [100].

Sprintf (sendBuf, "hello, % s!" , inet_ntoa (clientsocket sin_addr));//to find the corresponding IP and the line print there
Send: printf (" % s \ n ", sendBuf);
Send (serConn, sendBuf, strlen (sendBuf) + 1, 0).

Char receiveBuf [100].//receive
Recv (serConn receiveBuf, sizeof (receiveBuf), 0).
Printf (" recv: % s \ n ", receiveBuf);

Closesocket (serConn);//close
WSACleanup ();//release resources operation
return 0;
}
return 1;
}

The second paragraph
#include
#include
# pragma comment (lib, "ws2_32. Lib")
Int main (int arg c, char * * argv)
{
Int err;
WORD versionRequired;
WSADATA wsaData;
VersionRequired=MAKEWORD (1, 1);
Err=WSAStartup (versionRequired, & amp; WsaData);//agreement library version information
if (! Err) {
Printf (" the client already open nested word! \n");
} else {
Printf (" ERROR: failed to open the client nested word! \n");
return 1;//end
}
The SOCKET clientSocket=SOCKET (AF_INET, SOCK_STREAM, 0);

SOCKADDR_IN clientsock_in;
Clientsock_in. Sin_addr. S_un. S_addr=inet_addr (" 127.0.0.1 ");
Clientsock_in. Sin_family=AF_INET;
Clientsock_in. Sin_port=htons (6000);

The connect (clientSocket, (SOCKADDR *) & amp; Clientsock_in, sizeof (SOCKADDR));//connection
Char receiveBuf [100].
If (true) {

Send: printf (" % s \ n ", "hello, this is the client");
Send (clientSocket, "hello, this is the client", strlen (" hello, this is the client ") + 1, 0).

The recv (clientSocket, receiveBuf, 101, 0);
Printf (" Recv: % s \ n ", receiveBuf);

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

Every time is this Error C: \ Users \ \ Desktop \ the Web_Service 86153 \ collect2 exe [Error] ld returned 1 exit status

CodePudding user response:

Write a Windows application, no Visual Studio is asking for trouble,
It is time for IDE, it is better to install Visual Studio IDE, early now,,

CodePudding user response:

Compilation is to add ws2_32 library, my GCC, compiled the first paragraph by program, the second paragraph didn't try,
 E: \ Workspace> GCC -o demo demo. C - lws2_32 

E: \ Workspace> GCC - version
GCC (tdm64-1) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.,
This is free software; See the source for copying the conditions. There is NO
The giant; Not even for MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE.
  • Related