windows环境下C语言socket编程

来源(CSDN博客)

From: https://blog.csdn.net/weixin_41953890/article/details/80898958

windows环境下C语言socket编程

weixin_41953890 2018-07-03 15:27:00 16679 收藏 34

分类专栏: socket编程

版权

最近由于实验需要,要求写一个c程序与java程序通信的软件,为了测试首先写了一个windows环境下c语言的socket(tcp)通信程序。

首先socket通信的步骤:



图一 socket通信步骤(转载)



图二 三次握手协议(转载)



图三 三次握手协议(转载)

服务器端

#include <winsock2.h>
#include <windows.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和端口
	struct 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;
	struct sockaddr_in remoteAddr;
	int nAddrlen = sizeof(remoteAddr);
	//char revData[255];
	printf("等待连接...\n");
	sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
	while (1)
	{
		char revData[255];
		//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);
		//printf(ret);
		if (ret > 0)
		{
			revData[ret] = 0x00;
			printf(revData);
		}
		//发送数据
		char * sendData = "你好,TCP客户端!\n";
		send(sClient, sendData, strlen(sendData), 0);
	}
		closesocket(sClient);
	closesocket(slisten);
	WSACleanup();
	return 0;
}


客户端

#include <winsock2.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
#include <STDIO.H>
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;
	}
 
	struct 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, (struct sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
	{
		printf("connect error !");
		closesocket(sclient);
		return 0;
	}
	for (int i = 0; i < 1000; i++)
	{
 
		char * sendData = "csi接收  二进制文件未解析\n";
		send(sclient, sendData, strlen(sendData), 0);
		char recData[255];
		/*int ret = recv(sclient, recData, 255, 0);
		if (ret > 0)
		{
	
			recData[ret] = 0x00;
			//printf(recData);
			printf("%d%s",i,recData);
		}
		*/
	}
	closesocket(sclient);
	WSACleanup();
	return 0;
}


后续会给出c程序与java程序通信的例子。

Link: http://www.asm32.net/article_details.aspx?id=7381


浏览次数 0 发布时间 2020-08-07 05:47:09 从属分类 C/C++ 【评论】【 】【打印】【关闭
 
| www.asm32.net | 2006版 | 资料中心 | linux | asm/asm32 | C/C++ | VC++ | java | Python | 书签 | ASP.Net书签 | 京ICP备09029108号-1