Winsock learning 1: Using Winsock to implement the Server end of the Socket communication.

xiaoxiao2021-03-06  88

Study the Winsock API, the example of the Server end written on the book.

The source code is as follows:

/ * * File: server.cpp * * /

#include #include #include

#define default_port 5150 # Define default_buffer 4096int port = default_port Download Adobe Reader DEFAULT_PORT

Bool binterface = true; bool brecvonly = false; char szaddress [128]; // function: usage // description: // print usage information and exitvoid usage () {printf ("usage: server [-p: x] [ I: IP] [-O] / n / n "); Printf (" -p: x port number to listen on / n "); Printf (" -i: str interface to listen on / n "); printf "-O dont't echo the data back / n"); Printf ("Camel 2004-8-29"); EXITPROCESS (1);

// function: void validateargs (int Argc, char ** argv) {INT i; for (i = 1; i 3) STRCPY (Szaddress, & Argv [i] [3]); Break; Case 'o': BRECVONLY = true; Break; Default: Usage (); Break;}}}

}

// DWORD WINAPI CLIENTTHREAD (LPVOID LPPARAM) {socket Sock = (socket) LPPAR_BUFF [DEFAULT_BUFFER]; int RET, NLEFT, IDX; While (1) {// Perform A Blocking Recv () Call Ret = Recv (SOCK , szbuff, default_buffer, 0); if (RET == 0) // GRACEFUL Close Break; Else If (RET == Socket_ERROR) {Printf ("RECV () Failed:% D / N", wsagetlasterror ()); Break } SZBUFF [RET] = '/ 0'; Printf ("Recv: '% S' / N", SZBuff); //////////////ww, do it // if (! BRECVONLY) {NLEFT = RET; IDX = 0; // // Make Sure We Write All the data // while (nleft> 0) {RET = Send (Sock, & SZBuff [IDX], NLEFT, 0); if (Ret == 0) Break; Else if (Ret == Socket_ERROR) {Printf ("Send () failed:% d / n", wsagetlasterror (); break;} nLEFT - = RET; IDX = RET;}}

} Return 0;

}

int main (int argc, char ** argv) {printf ( "Server.cpp / n"); WSADATA wsd; SOCKET sListen, sClient; int iAddrSize; HANDLE hThread; DWORD dwThreadId; struct sockaddr_in local, client; ValidateArgs (argc, Argv); if (WSAStartup (MakeWord (2, 2), & WSD)! = 0) {PrintF ("Failed to Load Winsock! / N); Return 1;} /// Create Out Listening Socket // SListen = Socket (AF_INET, SOCK_STREAM, IPPROTO_IP); if (slintf ("socket () failed:% d / n", wsagetlasterror ()); return 1;} // select the local interface, and bind to it // if (bInterface) {local.sin_addr.s_addr = inet_addr (szAddress); if (local.sin_addr.s_addr == INADDR_NONE) usage ();} else local.sin_addr.s_addr = htonl (INADDR_ANY); local.sin_family = AF_INET; local.sin_port = HTons (iPort);

IF (Bind (SLISTEN) & LOCAL, SIZEOF (Local) == Socket_ERROR) {Printf ("Bing () Failed:% D / N", wsagetlasterror ()); Return 1;} listen (slisten, 8); // // in a Continuous Loop, Wait for Incoming Clients.once One // Is Detected, Create a Thread and pass the handle off to it. // while (1) {ipdrsize = sizeof (client); SCLIENT = accept (sListen, (struct sockaddr *) & client, & iAddrSize); if (sClient == INVALID_SOCKET) {printf ( "accept () failed:% d / n", WSAGetLastError ()); break;} printf ( "Accepted client :% s:% d / n ", inet_ntoa (client.sin_addr), ntohs (client.sin_port); hthread = CreateThread (Null, 0, ClientthRead, (LPVOID) SCLIENT, 0, & DWTHREADID; if (hthread == NULL) {Printf ("CreateTHread () Failed:% D / N", getLastError ()); Break;} CloseHandle (HTHREAD);} closesocket (); wsacleanup (); return 0;}

/ / -------------------------------------------------------------------------------------------- ------------

When compiling under VC 6.0, due to the compiler of VC is not familiar. open

Errors that are unable to resolve the function name in the Winsock API

...

Error LNK2001: Unresolved External Symbol _inet_addr @ 4 Error LNK2001: Unresolved External Symbol_htons @ 4 Error LNK2001: Unresolved External Symbol _Socket @ 12 ...

It is obtained by adding WS2_32.lib to the project.

Project -> setting -> link -> Object / library modules Add WS2_32.LIB

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

New Post(0)