码迷,mamicode.com
首页 > Windows程序 > 详细

windows网络编程入门

时间:2015-04-12 17:51:11      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

windows Sockets 的版本:

头文件:WINSOCK.h

库文件:wsock32.lib

动态困:WINSOCK.DLL


头文件:WINSOCK2.h

库文件:WS2_32.lib

动态困:WS2_32.DLL


服务端:

WSADATA			wsd;			//WSADATA变量
	SOCKET			sServer;		//服务器套接字
	SOCKET			sClient;		//客户端套接字
	SOCKADDR_IN		addrServ;;		//服务器地址
	char			buf[ BUF_SIZE];	//接收数据缓冲区
	int				retVal;			//返回值




	//初始化套接字动态库
	if (WSAStartup(MAKEWORD(2,2),&wsd) != 0) 
	{
		printf("WSAStartup Failed!!");
		AfxMessageBox("WSAStartup Failed");
		return 1;
	}
	//创建套接字
	sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if (INVALID_SOCKET == sServer) 
	{
		printf("socket failed!\n");
		AfxMessageBox("socket failed!");
		WSACleanup();//释放套接字
		return -1;
	}
	


	//下一步  绑定套接字
	//
	addrServ.sin_family = AF_INET;
	addrServ.sin_port = htons(4999);
	addrServ.sin_addr.s_addr = INADDR_ANY;
	//绑定套接字
	retVal = bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));
	if (SOCKET_ERROR == retVal) 
	{
		AfxMessageBox("bind failed!");
		closesocket(sServer); //关闭套接字
		WSACleanup();//释放套接字资源
		return -1;
	}


	//开始监听
	retVal = listen(sServer,1);
	if (SOCKET_ERROR == retVal) 
	{
		AfxMessageBox("listen failed!");
		closesocket(sServer);
		WSACleanup();
		return -1;
	}


	//设置服务器为监听状态,调用accept()函数等待客户端的连接。该函数处于阻塞状态,直到接受
	//了一个客户端请求、当该函数返回的时候,新建一个套接字,同时返回该客户端的地址。如果调用
	//失败,则关闭


	sockaddr_in   addrClient;
	int addrClientlen = sizeof(addrClient);
	sClient = accept(sServer,(sockaddr FAR*)&addrClient, &addrClientlen);
	if (INVALID_SOCKET == sClient)
	{
		AfxMessageBox("accept failed!");
		closesocket(sServer);
		WSACleanup();
		return -1;
	}


	//接受并显示数据
	ZeroMemory(buf,BUF_SIZE);
	retVal = recv(sClient,buf,BUF_SIZE,0);
	if (SOCKET_ERROR == retVal)
	{
		AfxMessageBox("recv Failed");
		closesocket(sServer);
		closesocket(sClient);
		WSACleanup();
		return -1;
	}
	CString str;
	str.Format("%s",buf);
//	AfxMessageBox(buf);
	m_Recv2.SetWindowText(str);






	closesocket(sServer);	//关闭套接字
	closesocket(sClient);	//关闭套接字
	WSACleanup();			//释放套接字资源;

客户端:

WSADATA			wsd;			//WSADATA变量
	SOCKET			sHost;			//服务器套接字
	SOCKADDR_IN		servAddr;		//服务器地址
	char			buf[BUF_SZIE];	//接收数据缓冲区
	int				retVal;			//返回值
	
	//初始化套结字动态库
	if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
	{
		printf("WSAStartup failed!\n");
		return -1;
	}
	
	//创建套接字
	sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);	
	if(INVALID_SOCKET == sHost)
	{
		printf("socket failed!\n");
		WSACleanup();//释放套接字资源
		return  -1;
	}

	//设置服务器地址
	servAddr.sin_family =AF_INET;
	servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	servAddr.sin_port = htons((short)4999);
	int	nServAddlen  = sizeof(servAddr);

	//连接服务器
	retVal=connect(sHost,(LPSOCKADDR)&servAddr, sizeof(servAddr));	
	if(SOCKET_ERROR == retVal)
	{
		printf("connect failed!\n");	
		closesocket(sHost);	//关闭套接字
		WSACleanup();		//释放套接字资源
		return -1;
	}
	
	//向服务器发送数据
	ZeroMemory(buf, BUF_SZIE);
	strcpy(buf, "MyTCP");
	retVal = send(sHost, buf, strlen(buf), 0);
	if (SOCKET_ERROR == retVal)
	{
		printf("send failed!\n");
		closesocket(sHost);	//关闭套接字
		WSACleanup();		//释放套接字资源
		return -1;
	}
	
	//退出
	closesocket(sHost);	//关闭套接字
	WSACleanup();		//释放套接字资源
	return 0;



windows网络编程入门

标签:

原文地址:http://blog.csdn.net/djb100316878/article/details/45011071

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!