1. 程式人生 > >socket程式設計基於UDP的服務端客戶端回聲程式

socket程式設計基於UDP的服務端客戶端回聲程式

前兩次的socket網路程式設計程式都是基於TCP 的,而這次使用UDP非面向連線的協議來寫的,只管傳送就可以了,在傳送資料量小的情況下非常有效率,傳送的這些資料不能是重要的資料,因為可能發生資料丟失、損毀等情況。Talk is cheap , show me the code.

server.c

#include<stdlib.h>
#include<stdio.h>
#include<WinSock2.h>
#define RECV_BUF_SIZE 1024
#pragma comment(lib,"ws2_32.lib")
int main(){
	WSADATA wsd;
	SOCKET sServer;
	SOCKADDR_IN addrServer, addrClient;
	int recvCount = 100;
	int sizeAddrClient = 0;
	char recvBuf[RECV_BUF_SIZE];
	memset(recvBuf, 0, RECV_BUF_SIZE);
	//初始化socket庫
	if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0){
		ErrorHandling("WSAStartup error!");
	}
	puts("初始化socket庫");

	//建立服務端socket
	sServer = socket(AF_INET, <strong><span style="color:#ff0000;">SOCK_DGRAM</span></strong>, 0);
	if (sServer == INVALID_SOCKET){
		ErrorHandling("socket() error!");
	}
	puts("建立服務端socket");

	//初始化網路地址資訊
	memset(&addrServer, 0, sizeof(addrServer));
	addrServer.sin_family = AF_INET;
	addrServer.sin_addr.S_un.S_addr = htonl(ADDR_ANY);
	addrServer.sin_port = htons(6000);
	puts("初始化網路地址資訊");

	//繫結地址資訊到服務端socket
	if (bind(sServer, (SOCKADDR*)&addrServer, sizeof(addrServer)) == SOCKET_ERROR){
		ErrorHandling("bind() error!");
	}
	printf("%d",WSAGetLastError());
	puts("繫結地址資訊到服務端socket");
	sizeAddrClient = sizeof(SOCKADDR);
	//迴圈接收訊息(我規定最多為200次)
	while (recvCount>0){
		int recvNum = recvfrom(sServer, recvBuf, RECV_BUF_SIZE,0, (SOCKADDR*)&addrClient, &sizeAddrClient);
		printf("%d",recvNum);
		sendto(sServer, recvBuf, RECV_BUF_SIZE, 0, (SOCKADDR*)&addrClient, sizeAddrClient);
		printf("Echo:%s\n", recvBuf);
		memset(recvBuf, 0, 1024);
		recvCount--;
	}
	//關閉socket
	closesocket(sServer);
	WSACleanup();
	puts("關閉socket");
	system("pause");
	return 0;
}
client.c
</pre><pre class="cpp" name="code" snippet_file_name="blog_20160404_5_4651503" code_snippet_id="1634444">#include<stdlib.h>
#include<stdio.h>
#include<WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
void ErrorHandling(char * msg){
	puts(msg);
	system("pause");
	exit(1);
}
int main(){
	WSADATA wsd;
	SOCKET  sClient;
	SOCKADDR_IN addrServer;
	int sizeaddrServer = 0;
	int recvCount = 100;
	char charBuf[1024] = { 0 };
	//初始化socket庫
	if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0){
		ErrorHandling("WSAStartup error!");
	}
	puts("初始化socket庫");

	//建立客戶端socket
	sClient = socket(AF_INET, SOCK_DGRAM, 0);
	if (sClient == INVALID_SOCKET){
		ErrorHandling("socket() error!");
	}
	puts("建立客戶端socket");

	//初始化網路地址資訊
	memset(&addrServer, 0, sizeof(addrServer));
	addrServer.sin_family = AF_INET;
	addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	addrServer.sin_port = htons(6000);
	puts("初始化網路地址資訊");

	//連線服務端
	if (connect(sClient, (SOCKADDR*)&addrServer, sizeof(addrServer)) == SOCKET_ERROR){
		ErrorHandling("connect error!");
	}
	puts("現在可以向服務端傳送訊息");
	//迴圈傳送訊息(我規定最多為200次)
	int recvNum = 0;
	char charRecv[1024];
	char recvBuf[1024];
	memset(recvBuf, 0, 1024);
	memset(charRecv, 0, 1024);
	int addrSize=sizeof(SOCKADDR);
	while (recvCount>0){
		memset(charBuf, 0, 1024);
		memset(recvBuf, 0, 1024);
		printf("To:");
		gets(charBuf);
		int sendLen = sendto(sClient, charBuf, 1024,0,(SOCKADDR*)&addrServer,sizeof(SOCKADDR));
		int recvLen = recvfrom(sClient, recvBuf, 1024, 0, (SOCKADDR*)&addrServer, &addrSize);
		if (recvLen==-1){
			printf("伺服器未響應\n");
		}
		else{
			printf("來自伺服器的訊息:%s\n",recvBuf);
		}
		
		recvCount--;
	}
	//關閉socket
	closesocket(sClient);
	WSACleanup();
	puts("關閉socket");
	system("pause");
	return 0;
}
不明白的地方,請回復。