Socket

xiaoxiao2021-03-05  19

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 . · Select Project-Settings-Link and add WS2_32.lib in the library module list. Main function int _tmain (int Argc, tchar * argv [], tchar * envp []) {int nretcode = 0; cout << "press escape to terminate program / r / n"; afxbeginthread (ServerThread, 0); While _Getch ()! = 27); Return Nretcode;} We do in main () is to turn on a thread and then loop a _getch () call. _Getch () is just a button waiting for a key and returns the ASCII value of this read character. We have been loop until 27 this value - Since 27 is the ASCII code of the ESCAPE key. What you might want to know is that even if we press Escape, the thread we open will also be a state of activity. Don't worry for these things, because when main () is returned, the process will be terminated, the thread that the main thread is turned on will also be terminated. ServerThread function Now what I have to do is listed in our ServerThread function, and use the code's annotation to explain what the relevant code line is done. Our TCP server mainly doing things is to listen to port 20248, this number is the member ID I am in Code Project. The event in this process is: When the client is connected, the server will send back a message to the client to inform the IP address, then turn off the connection and continue to receive the 20248 port. It also prints an IP address from the connection from the console. All in all, you might think this is an absolutely useless program. In fact, some people in you may even think it is useless as SndRec32.exe in Windows. I said, you are too harsh.

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.

转载请注明原文地址:https://www.9cbs.com/read-32515.html

New Post(0)