⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multicast_server.c

📁 用C语言写的Linux下基于socket通讯的聊天程序
💻 C
字号:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <stdio.h> #define MAXBUF 256 //#define PORT 5000 //#define GROUP "224.0.1.21" int main(void) {	char tmp,GROUP[50]="224.0.1.21";	int PORT=5000;	printf("Set configuration now?(if you choose not, default configuration will be used.) y/n:");	scanf("%c",&tmp);	if(tmp=='y')	{		printf("Input group address:");		scanf("%s",GROUP);		printf("Input port number:");		scanf("%d",&PORT);	}		int s; 	struct sockaddr_in srv; 	char buf[MAXBUF]; 	bzero(&srv, sizeof(srv)); 	srv.sin_family = AF_INET; 	srv.sin_port = htons(PORT); 	if (inet_aton(GROUP, &srv.sin_addr) < 0) { 		perror("inet_aton"); 		return 1; 	} 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 		perror("socket"); 		return 1; 	} 		printf("Please input the message:");	while (fgets(buf, MAXBUF, stdin)) { 		if (sendto(s, buf, strlen(buf), 0,(struct sockaddr *)&srv, sizeof(srv)) < 0)  			perror("recvfrom");  		else //		printf("Send message %s to group: %s\n", buf,GROUP); 		printf("Please input the message:");	} } 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -