udp
来源:互联网 发布:javascript教程哪个好 编辑:程序博客网 时间:2024/06/06 04:49
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <arpa/inet.h>#include <unistd.h>int main(int argc, char *argv[]){ int sock; //sendto中使用的对方地址 struct sockaddr_in toAddr; //在recvfrom中使用的对方主机地址 struct sockaddr_in fromAddr; int recvLen; unsigned int addrLen; char recvBuffer[128]; sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(sock < 0) { printf("创建套接字失败了.\r\n"); exit(0); } memset(&fromAddr, 0, sizeof(fromAddr)); fromAddr.sin_family = AF_INET; fromAddr.sin_addr.s_addr = htonl(INADDR_ANY); fromAddr.sin_port = htons(4000); if(bind(sock, (struct sockaddr *)&fromAddr, sizeof(fromAddr)) < 0) { printf("bind() 函数使用失败了.\r\n"); close(sock); exit(1); } while(1) { addrLen = sizeof(toAddr); if((recvLen = recvfrom(sock, recvBuffer, 128, 0, (struct sockaddr *)&toAddr, &addrLen)) < 0) { printf("()recvfrom()函数使用失败了.\r\n"); close(sock); exit(1); } if(sendto(sock, recvBuffer, recvLen, 0, (struct sockaddr *)&toAddr, sizeof(toAddr)) != recvLen) { printf("sendto fail\r\n"); close(sock); exit(0); } return 0; }}
0 0
- UDP
- UDP
- udp
- UDP
- UDP
- udp
- UDP
- UDP
- udp
- udp
- UDP
- UDP
- UDP
- UDP
- UDP
- UDP
- udp
- UDP
- 午评:将大概率试探一均线支撑
- 解决 Android Studio 的 ADB not responding 错误
- Linux进程间通信方式--本地socket
- SwipeRefreshLayout自动刷新的问题
- Bisearch Summary
- udp
- RVM切换ruby版本
- Cookie 和 Session机制详解
- 线程管理(一)AsyncTask与Thread的差别
- 下载Tomcat源码,并作为工程导入到Eclipse中
- 怎样选择(FC-SAN)光纤通道(存储)交换机
- 使用tcmalloc,它的central freelist很多,一直没有释放
- dxmemdata 连接 dbgrideh进行排序
- 用spring的InitializingBean的afterPropertiesSet来初始化