<返回更多

C语言进行Linux网络编程

2022-03-17    AioT智能家居
加入收藏

之前介绍了网络字节序。网络编程之网络字节序详解

今天就来实现一下简单的网络程序。

先看几个函数

// 创建socket套接字
int socket(int domain, int type, int protocol);
// 绑定服务器地址
int bind(int sockfd, const struct sockaddr *addr,
                socklen_t addrlen);
// 接收连接
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
// 连接服务器
int connect(int sockfd, const struct sockaddr *addr,
                   socklen_t addrlen);

服务器端程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/.NET.h>
#include <sys/socket.h>

typedef struct _MSG {
	int id;
	short length;
	short reserve;
	char content[100];
} MSG;

int main(int argc, char *argv[])
{
	int serv_sock;
	int clnt_sock;

	struct sockaddr_in serv_addr;
	struct sockaddr_in clnt_addr;
	socklen_t clnt_addr_size;

	char message[] = "Hello world!";

	if (argc != 2)
	{
		printf("Usage: %s <port>n", argv[0]);
		return -1;
	}

	serv_sock = socket(AF_INET, SOCK_STREAM, 0);
	if (serv_sock == -1) {
		printf("socket errorn");
		return -1;
	}

	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	// 转换为网络字节序,INADDR_ANY表示绑定本机所有ip地址
	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	// 将端口转换为网络字节序
	serv_addr.sin_port = htons(atoi(argv[1]));

	if (bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
		printf("bind errorn");
		return -1;
	}

	if (listen(serv_sock, 5) == -1) {
		printf("listen errorn");
		return -1;
	}

	clnt_addr_size = sizeof(clnt_addr);
	clnt_sock = accept(serv_sock, (struct sockaddr *)&clnt_addr, &clnt_addr_size);
	if (clnt_sock == -1) {
		printf("accept errorn");
	}

	for (int i = 0; i < 10; i++) {
		MSG msg;
		msg.id = htonl(i);
		msg.length = htons(strlen("hello world"));
		msg.reserve = htons(0);
		strcpy(msg.content, "hello world");

		write(clnt_sock, (char *)&msg, sizeof(MSG));
	}

	close(clnt_sock);
	close(serv_sock);

	return 0;
}

客户端程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

typedef struct _MSG {
	int id;
	short length;
	short reserve;
	char content[100];
} MSG;

int main(int argc, char *argv[])
{
	int sock;
	struct sockaddr_in serv_addr;
	int str_len;

	if (argc != 3)
	{
		printf("Usage: %s <IP> <port>n", argv[0]);
		return -1;
	}

	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == -1) {
		printf("socket errorn");
		return -1;
	}

	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
	serv_addr.sin_port = htons(atoi(argv[2]));

	if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
		printf("connect errorn");
		return -1;
	}

	for (;;) {
		MSG msg;
		str_len = read(sock, &msg, sizeof(msg));
		if (str_len <= 0) {
			printf("read errorn");
			break;
		}
		msg.id = ntohl(msg.id);
		msg.length = ntohs(msg.length);
		printf ("msg(id, length, content): %3d, %3d, %sn", msg.id, msg.length, msg.content);
	}


	close(sock);

	return 0;
}

运行效果如下

 

C语言进行Linux网络编程

 

 

 

C语言进行Linux网络编程
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>