#include
#pragma comment (lib, "ws2_32.lib") #define max_connected_num 1 // Maximum number, set to 1 #define max_ipaddress 260 # define socketmsg WM_USER 501 # Define Winsock_veion 0x0101 // MakeWord (2, 0)
Char fname [max_path]; char bufip [max_ipaddress]; char bUFPORT [80]; char szdlgtitle [] = "chat program version 1.0";
WSADATA WS; SOCKET SKT = INVALID_SOCKET; // Server (or client) // socket clientskt [max_connected_num]; // client array, temporary to 5 socket connection_skt = INVALID_SOCKET; // Server and client connection After that, after the established socket // connection, all data communication is through this connection_skt // this new Socket can no longer accept the client connection, only the original one can accept struct sockaddr_in addr; / / Server Socket Address Struct SockAddr_in ClientAddr; // Client
Void logfile (char * p) {file * fp = fopen (FNAME, "A "); FPRINTF (FP, "% S / N", P); fclose (fp);} Bool CreateServer (HWND HWND) {IF ( WSASTARTUP (Winsock_verion, & ws)) {MessageBox (hwnd, "Winsock Initialization Failure", Szdlgtitle, MB_OK | MB_ICONSTOP); WSACLEANUP (); Return False;} // Initialization
if ((skt = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {MessageBox (hwnd, "failed to create Socket!", szDlgTitle, MB_OK); closesocket (skt); return FALSE;} GetDlgItemText (hwnd, IDC_IP, bufIP, MAX_IPADDRESS); GetDlgItemText (hwnd, IDC_PORT, bufPORT, 79); addr.sin_family = AF_INET; addr.sin_addr.S_un.S_addr = htonl (INADDR_ANY); //inet_addr("192.168.10.4");// addr. SIN_PORT = HTONS (ATOI (BUFPORT));
IF (Bind (Skt, SockAddr *) & Addr, SIZEOF (AddR)) == SOCKET_ERROR) {MessageBox (hwnd, "socket binding!", szdlgtitle, mb_ok; returnaf
IF (Listen (SKT, MAX_CONNECTED_NUM) == Socket_ERROR) {MessageBox (HWND, "Monitor Failed!", Szdlgtitle, MB_OK; Return False;} Char Szip [127], Szport [127], BUF [256]; GetdlgiteMText HWND, IDC_IP, SZIP, SIZEOF (SZIP)); getDlgitemtext (hwnd, idc_port, szport, sizeof (szport)); WSPrintf (BUF, ">>>>>> Server at address:% s port:% D listens", SZIP, HTONS (ATOI (BUFPORT))); setdlgitemtext (hwnd, idc_revtxt, buf); // Listen start listening to the client's connection event if (WSAAsYNCselect (SKT, HWND, Socketmsg, FD_ACCEPT) == Socket_ERROR) {MessageBox (hwnd "Wsaasyncselect () failed", szdlgtitle, mb_ok); return false;} return true;}
Bool createclient (hwnd hwnd) {if (WSASTARTUP (Winsock_verion, & ws)! = 0) {MessageBox (hwnd, "Winsock Initialization Failure", Szdlgtitle, MB_OK | MB_ICONSTOP); WSACLEANUP (); Return False;} // Initialization
if ((skt = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {MessageBox (hwnd, "failed to create Socket!", szDlgTitle, MB_OK); closesocket (skt); return FALSE;} GetDlgItemText (hwnd, IDC_IP, bufIP, MAX_IPADDRESS); GetDlgItemText (hwnd, IDC_PORT, bufPORT, 79); addr.sin_family = AF_INET; addr.sin_addr.S_un.S_addr = inet_addr (bufIP); addr.sin_port = htons (atoi (bufPORT)); / * if (Bind (SKT, SockAddr *) == SIZEOF (ADDR)) == SOCKET_ERROR) {MessageBox (hwnd, "socket bind error!", szdlgtitle, mb_ok; return false;} * / int nConnect = Connect (SKT, (SockAddr *) & addr, sizeof (addr)); // Request to connect if (nconnect) MessageBox (hwnd, "connection failure!", Null, mb_ok); Else MessageBox (hwnd, "success!", Null, mb_ok) ;
if (WSAAsyncSelect (skt, hwnd, SOCKETMSG, FD_READ | FD_CLOSE | FD_CONNECT) == SOCKET_ERROR) {MessageBox (hwnd, "WSAAsyncSelect () failed", szDlgTitle, MB_OK); return FALSE;} return TRUE;} BOOL HttpClient (void) {WSADATA WS; Socket S; Struct SockAddr_in Addr; Int IResult; Long Lresult; Char Strsubaddr [100], Strbuffer [100];
LRESULT = WSASTARTUP (0x0101, & WS); s = socket (AF_INET, SOCK_STREAM, 0); // Established a TCP / IP connection
Addr.sin_Family = AF_INET; addr.sin_port = htons (80); addr.sin_addr.s_addr = inet_addr ("168.160.24.185"); // Computer World Daily IResult = Connect (S, (Struct Sockaddr *) & Addr, Sizeof AddR); if (socket_error == IRESULT) {// Connection failed wsacleanup (); return false;} else {// Connection Success STRCPY (strsubaddr, "get / 99 / tips / / / r / n"); strcpy FNAME, "C: //index.htm"; IRESULT = Send (S, STRSUBADDR, STRLEN (STRSUBADDR), 0); // Download file DO {strSet (strbuffer, ''); IRESULT = RECV (S, Strbuffer SIZEOF (STRBUFFER), 0); logfile (strbuffer);} while (IRESULT! = 0);}
WSACLANUP (); Return True;