Author: Nishant S Original link: http: //www.codeproject.com/internet/winsockintro01.asp a simple TCP socket server on the WinSock API is a set of libraries for the Microsoft Windows operating system, which was originally based on Berkeley socket, but there is some special changes in Microsoft. In this article, I have to try to introduce you how to use Winsock to make socket programming, and assume that you didn't have experience in network programming on any operating system. If you have only a separate machine, then you can still use the Winsock program design. You can use local loopback addresses named localhost, and its IP address is 127.0.0.1. In this way, if you run a TCP server on the machine, the client program on the same machine can be connected to the server using this loop address. Simple TCP Server In this article, I will introduce you to Winsock through a simple TCP server, and we will create this program step by step. However, before we start, you must do something, so we can prepare for the starting our Winsock programs. · First, use the VC 6.0 Application Wizard to create a Win32 Console Application. · Select the Add Support for MFC option. · Open the stdafx.h file and add this line: #include
UINT ServerThread (LPVOID PPARAM) {cout << "Starting Up TCP Server / R / N"; // Socket is actually a TypedEf for unsigned int. // In UNIX, the socket handle is like a file handle, is unsigned int. // Since these are not true under Windows, then we define a new data type, named Socket. Socket Server; // WSADATA is a structure, WSAStartup calls will be filled. Wsadata wsadata; // sockaddr_in specifies the address of the socket for the TCP / IP socket. // Other protocols use similar structures. SockAddr_in local; // wsastartup initializes the program call Winsock. // The first parameter specifies the highest version of the Winsock specification allowed by the program. INT wsaret = wsastartup (0x101, & wsadata); // If successful, WSAStartUp returns zero. // If we fail, we will exit. IF (wsaret! = 0) {return 0;} // Now let's assign a value for the SockAddr_in structure. Local.sin_family = AF_INET; // Address family local.sin_addr.s_addr = inaddr_any; // Online IP address local.sin_port = htons (u_short) 20248); // Use the port // created by the Socket function. Our Socket. Server = Socket (AF_INET, SOCK_STREAM, 0); // If the socket () function fails, we will exit. IF (server == invalid_socket) {return 0;} // bind links our sockets and socketdr_in structures. // It mainly uses local addresses and a specific port to connect sockets. // If it returns a non-zero value, it means an error. IF (Bind (SOCKADDR *) & local, sizeof (local))! = 0) {return 0;} // listen command socket monitoring from the client connection. // The second parameter is the maximum number of connections. IF (Listen (Server, 10)! = 0) {return 0;} // We need some variables to save the client's socket, so we have declared this. SOCKET Client; INCKADDR_IN from; int.comlen = sizeof (from); while // Unlimited loop {char Temp [512]; // accept () will receive upcoming client connections. Client = Accept (STRUCKADDR *) & from, & fromlease; sprintf (Temp, "Your IP IS% S / R / N", INET_NTOA (from.sin_addr); // We simply send this to the client String.