首页 > 代码库 > Windows下C语言的Socket编程例子(TCP和UDP)

Windows下C语言的Socket编程例子(TCP和UDP)

原文: Windows下C语言的Socket编程例子(TCP和UDP)

刚刚学windows编程,所以想写学习笔记,这是一个简单的Socket程序例子,开发环境是vc6:

首先是TCP

server端:
#include "stdafx.h"#include <stdio.h>#include <winsock2.h>#pragma comment(lib,"ws2_32.lib")int main(int argc, char* argv[]){	//初始化WSA	WORD sockVersion = MAKEWORD(2,2);	WSADATA wsaData;	if(WSAStartup(sockVersion, &wsaData)!=0)	{		return 0;	}	//创建套接字	SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);	if(slisten == INVALID_SOCKET)	{		printf("socket error !");		return 0;	}	//绑定IP和端口	sockaddr_in sin;	sin.sin_family = AF_INET;	sin.sin_port = htons(8888);	sin.sin_addr.S_un.S_addr = INADDR_ANY; 	if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)	{		printf("bind error !");	}	//开始监听	if(listen(slisten, 5) == SOCKET_ERROR)	{		printf("listen error !");		return 0;	}	//循环接收数据	SOCKET sClient;	sockaddr_in remoteAddr;	int nAddrlen = sizeof(remoteAddr);	char revData[255]; 	while (true)	{		printf("等待连接...\n");		sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);		if(sClient == INVALID_SOCKET)		{			printf("accept error !");			continue;		}		printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));				//接收数据		int ret = recv(sClient, revData, 255, 0);				if(ret > 0)		{			revData[ret] = 0x00;			printf(revData);		}		//发送数据		char * sendData = http://www.mamicode.com/"你好,TCP客户端!/n";>
client端:
#include "stdafx.h"#include <WINSOCK2.H>#include <STDIO.H>#pragma  comment(lib,"ws2_32.lib")int main(int argc, char* argv[]){	WORD sockVersion = MAKEWORD(2,2);	WSADATA data; 	if(WSAStartup(sockVersion, &data) != 0)	{		return 0;	}	SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);	if(sclient == INVALID_SOCKET)	{		printf("invalid socket !");		return 0;	}	sockaddr_in serAddr;	serAddr.sin_family = AF_INET;	serAddr.sin_port = htons(8888);	serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); 	if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)	{		printf("connect error !");		closesocket(sclient);		return 0;	}	char * sendData = http://www.mamicode.com/"你好,TCP服务端,我是客户端!/n";>

然后是UDP

server端:
#include "stdafx.h"#include <stdio.h>#include <winsock2.h>#pragma comment(lib, "ws2_32.lib") int main(int argc, char* argv[]){	WSADATA wsaData;	WORD sockVersion = MAKEWORD(2,2);	if(WSAStartup(sockVersion, &wsaData) != 0)	{		return 0;	}	SOCKET serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 	if(serSocket == INVALID_SOCKET)	{		printf("socket error !");		return 0;	}	sockaddr_in serAddr;	serAddr.sin_family = AF_INET;	serAddr.sin_port = htons(8888);	serAddr.sin_addr.S_un.S_addr = INADDR_ANY;	if(bind(serSocket, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)	{		printf("bind error !");		closesocket(serSocket);		return 0;	}		sockaddr_in remoteAddr;	int nAddrLen = sizeof(remoteAddr); 	while (true)	{		char recvData[255];  		int ret = recvfrom(serSocket, recvData, 255, 0, (sockaddr *)&remoteAddr, &nAddrLen);		if (ret > 0)		{			recvData[ret] = 0x00;			printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));			printf(recvData);					}		char * sendData = http://www.mamicode.com/"一个来自服务端的UDP数据包/n";>client端:
#include "stdafx.h"#include <stdio.h>#include <winsock2.h>#pragma comment(lib, "ws2_32.lib") int main(int argc, char* argv[]){	WORD socketVersion = MAKEWORD(2,2);	WSADATA wsaData; 	if(WSAStartup(socketVersion, &wsaData) != 0)	{		return 0;	}	SOCKET sclient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);		sockaddr_in sin;	sin.sin_family = AF_INET;	sin.sin_port = htons(8888);	sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");	int len = sizeof(sin);		char * sendData = http://www.mamicode.com/"来自客户端的数据包./n";>
测试是可以通过的,就是不知道有那些地方可以改进,那些地方是可以删除的,如果有人知道可以给我留言。
http://shop110737039.taobao.com/

Windows下C语言的Socket编程例子(TCP和UDP)