Home > other >  Socket programming, using TCP protocol server program - binding error 10038.
Socket programming, using TCP protocol server program - binding error 10038.

Time:10-24



# pragma comment (lib, "ws2_32. Lib")//the second version of the 32-bit Windows in the lib files are added to the project of the socket of the dynamic link library

# include
# include
# include



Void main (void)
{
WSADATA wsaData;//define a data, the data used to initialize the Windows sockets socket (* * * -- -- -- this is the first step - * * *)
The SOCKET ListeningSocket;//define a socket
The SOCKET NewConnection;//define a client socket
SOCKADDR_IN ServerAddr;//create the server address
SOCKADDR_IN ClientAddr;//create the client address

Int ClientAddrLen;//declare the client address length
Int Ret.//check if initialization is successful, how many bytes received
Int Port=5150;
Char DataBuffer [1024].
If ((Ret=WSAStartup (MAKEWORD (2, 2), & amp; WsaData))!=0)
{
Printf (" WSAStartup failed with the error % d \ n ", Ret);//initialization failed return error information
system("pause");
return;
}
If ((ListeningSocket=socket (AF_INET SOCK_STREAM, IPPROTO_TCP)==INVALID_SOCKET))//use the if determine whether create success
{
Printf (" socket failed with the error % d \ n ", WSAGetLastError);//using false information is WSAGetLastError
WSACleanup ();//clean up before the end of the
system("pause");
return;

}
ServerAddr. Sin_family=AF_INET;
ServerAddr. Sin_port=htons (Port);
ServerAddr. Sin_addr. S_un. S_addr=htonl (INADDR_ANY);
If ((bind (ListeningSocket, (SOCKADDR *) & amp; ServerAddr, sizeof (ServerAddr)))==SOCKET_ERROR)
{
Printf (" bind failed with the error % d \ n ", WSAGetLastError ());
Closesocket (ListeningSocket);
WSACleanup ();
system("pause");
return;
}
If ((listen (ListeningSocket, 5))==SOCKET_ERROR)
{
Printf (" listen failed with the error % d \ n ", WSAGetLastError ());
Closesocket (ListeningSocket);
WSACleanup ();
system("pause");
return;
}
Printf (" We are waiting a connection on port % d. \ n ", the port).
Printf (" Listen (on)... \n");
If ((NewConnection=accept (ListeningSocket, (SOCKADDR *) & amp; ClientAddr, & amp; ClientAddrLen))==INVALID_SOCKET)
//in this definition a client socket after receiving will get a client socket
{
Printf (" ACCEPT FAILED WITH the ERROR % d \ n ", WSAGetLastError ());
Closesocket (ListeningSocket);
WSACleanup ();
system("pause");
return;
}
Printf (" We successfully got a connectiong the from % s: % d \ n ", inet_ntoa (ClientAddr. Sin_addr), ntohs (ClientAddr. Sin_port));
If ((Ret=recv (NewConnection, DataBuffer, sizeof (DataBuffer), 0))==SOCKET_ERROR)
{
Printf (" recv failed with the error % d \ n ", WSAGetLastError ());
Closesocket (NewConnection);
WSACleanup ();
system("pause");
return;
}
//has been successfully received at this time the client sends data, to display its
Printf (" We have successfully received % d bytes. \ n ", Ret);
DataBuffer [Ret]='\ 0';//end \ 0 said the end of the string
Printf (" % s \ n ", DataBuffer);
Printf (" Ww are now going to close the client connectiong. \ n ");
Closesocket (NewConnection);
WSACleanup ();
system("pause");
}
  • Related