Socket编程笔记

服务端:

步骤如下:

(1)WinSock初始化----WSAStartup();
(2)创建套接字(socket
());
(3)将套接字绑定到一个本地地址和端口上(bind())
(4)将套接字设为监听模式,准备接收客户端请求(listen())
(5)等待客户请求到来;当请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept()阻塞接口)
(6)用返回的套接字和客户端进行通信(send / recv)
(7)返回,等待另一个客户请求。
(8)关闭套接字。

代码:

#include <stdio.h>  
#include <winsock2.h>  

#pragma comment(lib, "Ws2_32.lib")
//#define SERVER_PORT 4000 //侦听端口  
char file_path[] = "D:\\rec.h264";

int main(int argc, char *argv[])
{
	if (argc < 3)
	{
		printf("Usage : REC IP And Port\n");
		exit(1);
	}
	char *ip = argv[1];
	int port = atoi(argv[2]);

	FILE *pFile = fopen(file_path, "wb+");
	WORD wVersionRequested;
	WSADATA wsaData;
	int ret, nLeft, length;
	SOCKET sListen, sServer; //侦听套接字,连接套接字  
	struct sockaddr_in saServer, saClient; //地址信息     
	   
	//WinSock初始化  
	wVersionRequested = MAKEWORD(2, 2); //希望使用的WinSock DLL 的版本  
	ret = WSAStartup(wVersionRequested, &wsaData);
	if (ret != 0)
	{
		printf("WSAStartup() failed!\n");
		//return 0;  
	}
	//创建Socket,使用TCP协议  
	sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sListen == INVALID_SOCKET)
	{
		WSACleanup();
		printf("socket() faild!\n");
		//return 0;  
	}
	//构建本地地址信息  
	saServer.sin_family = AF_INET; //地址家族  
	saServer.sin_port = htons(port); //注意转化为网络字节序  
	saServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //使用INADDR_ANY 指示任意地址  

	//绑定  
	ret = bind(sListen, (struct sockaddr *)&saServer, sizeof(saServer));
	if (ret == SOCKET_ERROR)
	{
		printf("bind() faild! code:%d\n", WSAGetLastError());
		closesocket(sListen); //关闭套接字  
		WSACleanup();
		//return 0;  
	}

	//侦听连接请求  
	ret = listen(sListen, 5);
	if (ret == SOCKET_ERROR)
	{
		printf("listen() faild! code:%d\n", WSAGetLastError());
		closesocket(sListen); //关闭套接字  
		//return 0;  
	}

	printf("Waiting for client connecting!\n");

	while (1)
	{
		length = sizeof(saClient);
		sServer = accept(sListen, (struct sockaddr *)&saClient, &length);
		if (sServer == INVALID_SOCKET)
		{
			printf("accept() faild! code:%d\n", WSAGetLastError());
			closesocket(sListen); //关闭套接字  
			WSACleanup();
			return 0;
		}
		printf("server accept\n");

		
		char receiveMessage[5000];
		char *pRec = receiveMessage;
		while (1)
		{
			//接收数据  
			ret = recv(sServer, receiveMessage, 5000, 0);
			if (ret == SOCKET_ERROR)
			{
				printf("recv() failed!\n");
				return 0;
			}
			if (ret == 0) //客户端已经关闭连接  
			{
				printf("Client has closed the connection\n");
				break;
			}
			printf("rec frame ret:%d\n",ret);
			fwrite(pRec, ret, 1, pFile);//存储接收的数据
			fflush(pFile);
		}
	}
	  closesocket(sListen);  
	  closesocket(sServer);  
	  WSACleanup();  
	  system("pause");
	  return 0;
}



客户端:

步骤如下:

(1)WinSock初始化----WSAStartup();
(2)创建套接字(socket)
(3)向服务器发出连接请求(connect)/UDP不需要此步
(4)和服务器端进行通信(send / recv)
(5)关闭套接字

代码:

#include <stdio.h>  
#include <stdlib.h>  
#include <winsock2.h>  
#pragma comment(lib, "Ws2_32.lib")
#define SERVER_PORT 4000 //侦听端口  
#define USE_UDP 0
char file_path[] = "D:\\rec.h264";

int main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int ret;
	SOCKET sClient; //连接套接字  
	struct sockaddr_in saServer; //服务器地址信息  
	
	BOOL fSuccess = TRUE;
	/*FILE *pFile = fopen(file_path,"wb+"); */
	//WinSock初始化  
	wVersionRequested = MAKEWORD(2, 2); //希望使用的WinSock DLL的版本  
	ret = WSAStartup(wVersionRequested, &wsaData);  //加载套接字库  
	if (ret != 0)
	{
		printf("WSAStartup() failed!\n");
		//return 0;  
	}
	//确认WinSock DLL支持版本2.2  
	if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
	{
		WSACleanup();   //释放为该程序分配的资源,终止对winsock动态库的使用  
		printf("Invalid WinSock version!\n");
		//return 0;  
	}

	//构建服务器地址信息  
	saServer.sin_family = AF_INET; //地址家族  
	saServer.sin_port = htons(SERVER_PORT); //注意转化为网络节序  
	saServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

	//创建Socket,使用TCP/UDP协议  
#if USE_UDP
	sClient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (sClient == INVALID_SOCKET)
	{
		WSACleanup();
		printf("socket() failed!\n");
		//return 0;  
	}
#else
	sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sClient == INVALID_SOCKET)
	{
		WSACleanup();
		printf("socket() failed!\n");
		//return 0;  
	}
	//连接服务器  
	ret = connect(sClient, (struct sockaddr *)&saServer, sizeof(saServer));
	if (ret == SOCKET_ERROR)
	{
		printf("connect() failed!\n");
		printf("socket error! : %d", WSAGetLastError());
		closesocket(sClient); //关闭套接字  
		WSACleanup();
		//return 0;  
	}
#endif



	


	char sendMessage[] = "send test";
	while (1)
	{
		ret = send(sClient, (char *)&sendMessage, sizeof(sendMessage), 0);
		if (ret == SOCKET_ERROR)
		{
			printf("send() failed!\n");
		}
		else
			printf("client info has been sent!\n");
		Sleep(1000);
	}
	
	/*char recvBuf[256];
	memset(recvBuf,0,sizeof(char));
	int len = recv(sClient, recvBuf, 256, 0);
	char *tmp = new char[len];
	
	memcpy(tmp, recvBuf, len);
	printf("%s\n", tmp);*/
	/*fwrite(recvBuf, sizeof(unsigned char), len, pFile);
	fflush(pFile);*/
	closesocket(sClient); //关闭套接字  
	WSACleanup();
	system("pause");
	//return 0;  
}
参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值