赞
踩
服务器
- #include <stdio.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <string.h>
- #include <unistd.h>
-
- #define ERR_MSG(msg) do{\
- fprintf(stderr, "__%d__:", __LINE__);\
- perror(msg);\
- }while(0)
-
- #define PORT 8888
- #define IP "192.168.31.212"
-
- int main(int argc, const char *argv[])
- {
- //创建报式套接字
- int sfd = socket(AF_INET, SOCK_DGRAM, 0);
- if(sfd < 0)
- {
- ERR_MSG("socket");
- return -1;
- }
-
- //填充地址信息结构体,真是的地址信息结构体根据地址族制定
- //AF_INET : man 7 ip
- struct sockaddr_in sin;
- sin.sin_family = AF_INET; //必须填AF_INET;
- sin.sin_port = htons(PORT); //端口号的网络字节序;1024~49151
- sin.sin_addr.s_addr = inet_addr(IP); //本机IP,ifconfig查看;
-
-
- //绑定服务器的地址信息结构体到套接字文件描述符上,必须绑定
- if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
- {
- ERR_MSG("bind");
- return -1;
- }
- printf("bind success\n");
-
- ssize_t res = 0;
- char buf[128] = "";
-
- struct sockaddr_in cin; //存储数据包的来源
- socklen_t addrlen = sizeof(cin);
-
-
- while(1)
- {
- bzero(buf, sizeof(buf));
- //接收, 接收信息的同时,接收该数据包从谁哪里来
- res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen);
- if(res < 0)
- {
- ERR_MSG("recvfrom");
- return -1;
- }
- printf("[%s:%d] : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
-
- //发送:谁给我发信息,我就将信息发还给谁
- strcat(buf, "*_*");
- if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, sizeof(cin)) < 0)
- {
- ERR_MSG("sendto");
- return -1;
- }
- printf("sendto success\n");
-
- }
- //关闭文件描述符
- close(sfd);
- return 0;
- }
-
客户端
- #include <stdio.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <string.h>
- #include <unistd.h>
-
- #define ERR_MSG(msg) do{\
- fprintf(stderr, "__%d__:", __LINE__);\
- perror(msg);\
- }while(0)
-
- #define CLI_PORT 6666
- #define CLI_IP "192.168.31.212"
-
- #define SER_PORT 8888
- #define SER_IP "192.168.31.212"
-
- int main(int argc, const char *argv[])
- {
- //创建报式套接字
- int sfd = socket(AF_INET, SOCK_DGRAM, 0);
- if(sfd < 0)
- {
- ERR_MSG("socket");
- return -1;
- }
-
- //填充服务器地址信息结构体,真是的地址信息结构体根据地址族制定
- //AF_INET : man 7 ip,
- //由于下面的sendto要发送给服务器,所以需要先填充服务器的地址信息结构体
- //供给下面的sendto函数使用;
- struct sockaddr_in sin;
- sin.sin_family = AF_INET; //必须填AF_INET;
- sin.sin_port = htons(SER_PORT); //服务器的端口
- sin.sin_addr.s_addr = inet_addr(SER_IP); //服务器IP
-
- ssize_t res = 0;
- char buf[128] = "";
-
- struct sockaddr_in rcvaddr; //存储数据包的来源
- socklen_t addrlen = sizeof(rcvaddr);
-
-
- while(1)
- {
- bzero(buf, sizeof(buf));
- //发送,数据给服务器
- printf("请输入>>>");
- fgets(buf, sizeof(buf), stdin);
- buf[strlen(buf)-1] = 0;
-
- if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
- {
- ERR_MSG("sendto");
- return -1;
- }
- printf("sendto success\n");
-
- //接收, 接收信息的同时,接收该数据包从谁哪里来
- res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&rcvaddr, &addrlen);
- if(res < 0)
- {
- ERR_MSG("recvfrom");
- return -1;
- }
- printf("[%s:%d] : %s\n", inet_ntoa(rcvaddr.sin_addr), ntohs(rcvaddr.sin_port), buf);
-
-
- }
- //关闭文件描述符
- close(sfd);
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。