Windows平台下MingGW的网络socket编程模型

Windows平台下MingGW的网络socket编程模型


1、TCP服务器

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <winsock2.h>
#include <pthread.h>

#define LOCAL_IP      "192.168.31.254"
#define LOCAL_PORT    ((uint16_t)10126)

#define debug(...)                                     \
						do                             \
						{                              \
							printf(__VA_ARGS__);       \
							fflush(stdout);            \
						}while(0)



int main(void)
{
	int ret = 0;
	WSADATA ws;
	SOCKET sock, newsock;
	struct sockaddr_in local;
	struct sockaddr_in client;
	int addrlen = 0;
	int nrecv   = 0;
	char buf[512];

	if(WSAStartup(MAKEWORD(2, 2), &ws) != 0)
	{
		ret = -1;
		goto __exit;
	}

	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(sock == INVALID_SOCKET)
	{
		ret = -2;
		goto __exit;
	}

	local.sin_family = AF_INET;
	local.sin_addr.S_un.S_addr = inet_addr(LOCAL_IP);
	local.sin_port = htons(LOCAL_PORT);
	memset(local.sin_zero, 0X00, sizeof(local.sin_zero));

	if(bind(sock, (const struct sockaddr *)&local, sizeof(local)) == SOCKET_ERROR)
	{
		ret = -3;
		goto __exit;
	}

	listen(sock, 5);

	addrlen = sizeof(local);

	debug(" accept %s:%d...\r\n", LOCAL_IP, LOCAL_PORT);

	if((newsock = accept(sock, (struct sockaddr *)&client, &addrlen)) == SOCKET_ERROR)
	{
		ret = -4;
		goto __exit;
	}

	debug(" %s:%d connected\r\n", inet_ntoa(client.sin_addr), client.sin_port);

	while(1)
	{
		if((nrecv = recv(newsock, buf, sizeof(buf), 0)) <= 0)
			break;

		if(strncmp(buf, "quit", 4) == 0)
			break;

		if(send(newsock, buf, nrecv, 0) < 0)
			break;
	}

__exit:
	closesocket(sock);
	closesocket(newsock);
	return ret;
}

2、TCP客户端

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <winsock2.h>
#include <pthread.h>

#define LOCAL_IP      "192.168.31.254"
#define LOCAL_PORT    ((uint16_t)10126)

#define debug(...)                                     \
						do                             \
						{                              \
							printf(__VA_ARGS__);       \
							fflush(stdout);            \
						}while(0)


int main(void)
{
	int ret = 0;
	WSADATA ws;
	SOCKET sock;
	struct sockaddr_in server;
	int nrecv   = 0;
	char buf[100];

	if(WSAStartup(MAKEWORD(2, 2), &ws) != 0)
	{
		ret = -1;
		goto __exit;
	}

	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(sock == INVALID_SOCKET)
	{
		ret = -2;
		goto __exit;
	}

	server.sin_family = AF_INET;
	server.sin_addr.S_un.S_addr = inet_addr(LOCAL_IP);
	server.sin_port = htons(LOCAL_PORT);

	if(connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0)
	{
		ret = -3;
		goto __exit;
	}

	memset(buf, 'A', sizeof(buf));

	while(1)
	{
		if(send(sock, buf, sizeof(buf), 0) < 0)
			break;

		if((nrecv = recv(sock, buf, sizeof(buf), 0)) <= 0)
			break;

		if(strncmp(buf, "quit", 4) == 0)
			break;
	}

__exit:
	closesocket(sock);
	return ret;
}


ends…

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

觉皇嵌入式

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值