编程学习网 > 编程语言 > C/C++开发 > c语言如何进行网上编程(C语言进行Linux网络编程)
2022
09-24

c语言如何进行网上编程(C语言进行Linux网络编程)

现如今,网络时代的发展,网络编程在编程体系中占据很大一部分!无论什么语言都需要支持网络编程。接下来就和大家分享一下c语言如何进行网上编程。

先看几个函数

// 创建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/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 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 error\n"); 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 error\n"); return -1;
	} if (listen(serv_sock, 5) == -1) { printf("listen error\n"); 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 error\n");
	} 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 error\n"); 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 error\n"); return -1;
	} for (;;) {
		MSG msg;
		str_len = read(sock, &msg, sizeof(msg)); if (str_len <= 0) { printf("read error\n"); break;
		}
		msg.id = ntohl(msg.id);
		msg.length = ntohs(msg.length); printf ("msg(id, length, content): %3d, %3d, %s\n", msg.id, msg.length, msg.content);
	}


	close(sock); return 0;
}

运行效果如下

以上就是“c语言如何进行网上编程(C语言进行Linux网络编程)”的详细内容,想要了解更多C语言教程欢迎持续关注编程学习网

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取