1. 程式人生 > >流式套接字——簡單的回射伺服器示例

流式套接字——簡單的回射伺服器示例

文章目錄

Server

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>

#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main(int argc, char **argv) 
{
	WSADATA wsaData;
	int iResult;
	SOCKET ListenSocket = INVALID_SOCKET;
	SOCKET ClientSocket = INVALID_SOCKET; 
	struct addrinfo *result = NULL;
	struct addrinfo hints;
	int iSendResult; 
	char recvbuf[DEFAULT_BUFLEN];
	int recvbuflen = DEFAULT_BUFLEN;
	//初始化WinSock
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); 
	if(iResult != 0) {
		printf("WSAStartup failed with error: %d\n", iResult);
		return 1;
	}
	ZeroMemory(&hints, sizeof(hints));
	//宣告IPV4地址族,流式套接字,TCP協議
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	//解析伺服器地址和埠號
	iResult = getaddrinfo("192.168.42.166", DEFAULT_PORT, &hints, &result);
	if(iResult != 0) {
		printf("getaddrinfo failed with error: %d\n", iResult);
		WSACleanup();
		return 1;
	}

	//為面向連線的伺服器建立套接字
	ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
	if(ListenSocket == INVALID_SOCKET) {
		printf("socket failed with error: %ld\n", WSAGetLastError());
		freeaddrinfo(result);
		WSACleanup();
		return 1;
	}

	//為監聽套接字繫結本地地址和埠號 
	iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
	if(iResult == SOCKET_ERROR) {
		printf("bind failed with error: %d\n", WSAGetLastError());
		freeaddrinfo(result);
		WSACleanup();
		return 1;
	}
	freeaddrinfo(result);

	//在監聽套接字上等待連線請求
	if(listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR) {
		printf("listen failed with error: %d\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
		return 1;
	}
	printf("TCP server starting...\n");
	//接受客戶端的請求連線
	ClientSocket = accept(ListenSocket, NULL, NULL);
	if(ClientSocket == INVALID_SOCKET) {
		printf("accept failed: %d\n", WSAGetLastError());
		closesocket(ClientSocket);
		WSACleanup();
		return 1;	
	}
	closesocket(ListenSocket);
	
	//持續接收資料,直到對方關閉連線
	do {
		iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
		if(iResult > 0) {
			//情況1:成功接收到資料
			printf("Bytes received: %d\n", iResult);
			//將緩衝區的內容回送給客戶端
			iSendResult = send(ClientSocket, recvbuf, iResult, 0);
			if(iSendResult == SOCKET_ERROR){
				printf("send failed with error: %d\n", WSAGetLastError());
				closesocket(ClientSocket);
				WSACleanup();
				return 1;
			}
			printf("Bytes sent: %d\n", iSendResult);
		}
		else if(iResult == 0) 
			//情況2:關閉連線
			printf("Connection closing...\n");
		else {
			//情況3:接收發生錯誤
			printf("recv failed with error: %d\n", WSAGetLastError());
			closesocket(ClientSocket);
			WSACleanup();
			return 1;
		}
	}while(iResult > 0);
	//關閉連線
	iResult = shutdown(ClientSocket, SD_SEND);
	if(iResult == SOCKET_ERROR) {
		printf("shutdown failed with error: %d\n", WSAGetLastError());
		closesocket(ClientSocket);
		WSACleanup();
		return 1;
	}
	//關閉套接字
	closesocket(ClientSocket);
	//釋放資源
	WSACleanup();
	return 0;
}

Client

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>

#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main(int argc, char **argv) 
{
	WSADATA wsaData;
	SOCKET ConnectSocket = INVALID_SOCKET;
	struct addrinfo *result = NULL, *ptr = NULL, hints ;
	char *sendbuf = "this is a test";
	char recvbuf[DEFAULT_BUFLEN];
	int iResult;
	int recvbuflen = DEFAULT_BUFLEN;
	if(argc != 2) {
		printf("usage: %s server-name\n", argv[0]);
		return 1;
	}

	//初始化套接字
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if(iResult != 0) {
		printf("WSAStartup failed with error: %d\n", iResult);
		return 1;
	}
	ZeroMemory(&hints, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	//解析伺服器地址和埠號
	iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
	if(iResult != 0) {
		printf("getaddrinfo failed with error: %d\n", iResult);
		WSACleanup();
		return 1;
	}
	//嘗試連線伺服器地址,直到成功
	for(ptr = result; ptr != NULL; ptr = ptr->ai_next) {
		//建立套接字
		ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
		if(ConnectSocket == INVALID_SOCKET) {
			printf("socket failed with error: %ld\n", WSAGetLastError());
			WSACleanup();
			return 1;
		}
		//向伺服器請求連線
		iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
		if(iResult == SOCKET_ERROR) {
			closesocket(ConnectSocket);
			ConnectSocket = INVALID_SOCKET;
			continue;
		}
		break;
	}
	freeaddrinfo(result);
	if(ConnectSocket == INVALID_SOCKET) {
		printf("Unbale to connect to server!\n");
		WSACleanup();
		return 1;
	}
	//傳送緩衝區中的測試資料
	iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
	if(iResult == SOCKET_ERROR) {
		printf("send failed with error: %d\n", WSAGetLastError());
		closesocket(ConnectSocket);
		WSACleanup();
		return 1;
	}
	printf("Bytes Sent: %ld\n", iResult);
	//資料傳送結束,呼叫shutdown()函式宣告不再發送資料,此時客戶端仍可以接收資料
	iResult = shutdown(ConnectSocket, SD_SEND);
	if(iResult == SOCKET_ERROR) {
		printf("shutdown failed with error: %d\n", WSAGetLastError());
		closesocket(ConnectSocket);
		WSACleanup();
		return 1;
	}
	//持續接收資料,直到伺服器關閉連線
	do {
		iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
		if(iResult > 0) 
			printf("Bytes received: %d\n", iResult);
		else if(iResult == 0) 
			printf("Connection closed\n");
		else 
			printf("recv failed with error: %d\n", WSAGetLastError());
	}while(iResult > 0);
	//關閉套接字
	closesocket(ConnectSocket);
	//釋放資源
	WSACleanup();
	return 0;
}