server
/*************************** Server ********************/ // Module Name: Server.c // Description: // This example illustrates a simple TCP server that accepts // incoming client connections. Once a client connection is // established, a thread is spawned to read data from the // client and echo it back (if the echo option is not // disabled). // Compile: // cl -o Server Server.c ws2_32.lib // Command line options: // server [-p:x] [-i:IP] [-o] // -p:x Port number to listen on // -i:str Interface to listen on // -o Receive only, don't echo the data back #include <winsock2.h> #include <stdio.h> #include <stdlib.h> #define DEFAULT_PORT 168 #define DEFAULT_BUFFER 4096 int iPort = DEFAULT_PORT; // Port to listen for clients on char szAddress[128]; // Interface to listen for clients on // Function: ClientThread // Description: // This function is called as a thread, and it handles a given // client connection. The parameter passed in is the socket // handle returned from an accept() call. This function reads // data from the client and writes it back. DWORD WINAPI ClientThread(LPVOID lpParam) { SOCKET sock=(SOCKET)lpParam; char szBuff[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); // echo the data back, do it 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; } // Function: main // Description: // Main thread of execution. Initialize Winsock, parse the // command line arguments, create the listening socket, bind // to the local address, and wait for client connections. int main(int argc, char **argv) { WSADATA wsd; SOCKET sListen,sClient; int iAddrSize; HANDLE hThread; DWORD dwThreadId; struct sockaddr_in local,client; //Initialize Winsock printf("Initialize WinSock\n"); if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) { printf("Failed to load Winsock!\n"); printf("Close this program!\n"); return 1; } ZeroMemory(&local, sizeof(local)); local.sin_family = AF_INET; local.sin_port = htons(iPort); local.sin_addr.s_addr = inet_addr("192.168.0.10"); // Create our listening socket sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (sListen == SOCKET_ERROR) { printf("socket() failed: %d\n", WSAGetLastError()); printf("Close this program!\n"); return 1; } else printf("Socket is ready.\n"); if (bind(sListen, (struct sockaddr *)&local,sizeof(local)) == SOCKET_ERROR) { printf("bind() failed: %d\n", WSAGetLastError()); printf("Close this program!\n"); return 1; } listen(sListen, 8); // In a continous loop, wait for incoming clients. Once one // is detected, create a thread and pass the handle off to it. while (1) { iAddrSize = 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(sListen); WSACleanup(); return 0; }
console
#include <winsock2.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #define DEFAULT_COUNT 20 #define DEFAULT_PORT 168 #define DEFAULT_BUFFER 4096 using namespace std; char szMessage[1024]; // Message to send to sever int iPort = DEFAULT_PORT; // Port on server to connect to // Function: main // Description: // Main thread of execution. Initialize Winsock, parse the // command line arguments, create a socket, connect to the // server, and then send and receive data. int main(void) { WSADATA wsd; SOCKET sClient; char szBuffer[DEFAULT_BUFFER]; int ret,i; struct sockaddr_in server; struct hostent *host = NULL; printf("Initialize WinSock\n"); // Parse the command line and load Winsock if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) { printf("Failed to load Winsock library!\n"); printf("Close this program!"); return 1; } // Create the socket, and attempt to connect to the server sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sClient == INVALID_SOCKET) { printf("socket() failed: %d\n", WSAGetLastError()); printf("Close this program!"); return 1; } printf("Socket is ready.\n"); server.sin_family = AF_INET; server.sin_port = htons(iPort); server.sin_addr.s_addr = inet_addr("192.168.0.10"); //connect to server if (connect(sClient, (struct sockaddr *)&server,sizeof(server)) == SOCKET_ERROR) { printf("connect() failed: %d\n", WSAGetLastError()); closesocket(sClient); printf("Close socket!\n"); printf("Close this program!"); return 1; } printf("Connect to Server!\n"); // Send and receive data do { cin >> szMessage; ret = send(sClient, szMessage, strlen(szMessage), 0); if (ret == 0) break; else if (ret == SOCKET_ERROR) { printf("send() failed: %d\n", WSAGetLastError()); break; } printf("Send %d bytes\n", ret); ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0); if (ret == 0) break; else if (ret == SOCKET_ERROR) { printf("recv() failed: %d\n", WSAGetLastError()); break; } szBuffer[ret] = '\0'; printf("RECV [%d bytes]: '%s'\n", ret, szBuffer); } while( strcmp(szBuffer,"bye") ); closesocket(sClient); WSACleanup(); printf("See you~~\n"); system("pause"); return 0; }
只是個範例