Linux涓媢dp缂栫▼
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux涓媢dp缂栫▼相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e7%ab%af%e5%8f%a3' title='绔彛'>绔彛
int tcp 绋嬪簭 net 鍘熷瀷 define涓€銆佸嚱鏁?/p>
1銆乺ecvfrom鍑芥暟
UDP浣跨敤recvfrom()鍑芥暟鎺ユ敹鏁版嵁锛屼粬绫讳技浜庢爣鍑嗙殑read()锛屼絾鏄湪recvfrom()鍑芥暟涓鎸囨槑鐩殑鍦板潃銆?/p>
#include <sys/types.h> #include <sys/socket.h> ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); //杩斿洖鍙戦€佹暟鎹殑闀垮害 锛嶉潪0鎴愬姛銆€銆€銆€-1锛嶅け璐?/span>
2銆乻endto鍑芥暟
UDP浣跨敤sendto()鍑芥暟鍙戦€佹暟鎹紝浠栫被浼间簬鏍囧噯鐨剋rite()锛屼絾鏄湪sendto()鍑芥暟涓鎸囨槑鐩殑鍦板潃
#include <sys/types.h> #include <sys/socket.h> ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); //杩斿洖鍙戦€佹暟鎹殑闀垮害 锛嶉潪0鎴愬姛銆€銆€銆€-1锛嶅け璐?/span>
3銆乻ocket鍑芥暟
#include <sys/types.h> #include <sys/socket.h> //鍑芥暟鍘熷瀷 int socket(int domain, int type, int protocol);
涓轰簡鎵ц缃戠粶杈撳叆杈撳嚭锛屼竴涓繘绋嬪繀椤诲仛鐨勭涓€浠朵簨灏辨槸璋冪敤socket鍑芥暟鑾峰緱涓€涓枃浠舵弿杩扮銆?/strong>
绗竴涓弬鏁版寚鏄庝簡鍗忚绨囷紝鐩墠鏀寔5绉嶅崗璁皣锛屾渶甯哥敤鐨勬湁AF_INET(IPv4鍗忚)鍜孉F_INET6(IPv6鍗忚)銆?/p>
绗簩涓弬鏁版寚鏄庡鎺ュ瓧绫诲瀷锛屾湁涓夌绫诲瀷鍙€夛細SOCK_STREAM(瀛楄妭娴佸鎺ュ彛)銆丼OCK_DGRAM(鏁版嵁鎶ュ鎺ュ彛)鍜孲OCK_RAW(鍘熷濂楁帴鍙?锛汿CP涓€鑸€夋嫨SOCK_STREAM锛岃€孶DP閫夋嫨SOCK_DGRAM銆?/p>
4銆乥ind鍑芥暟
#include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
绗竴涓弬鏁版槸socket鍑芥暟杩斿洖鐨勫鎺ュ瓧鐨勬枃浠舵弿杩扮銆傜浜屽拰绗笁涓弬鏁板垎鍒槸涓€涓寚鍚戠壒瀹氫簬鍗忚鐨勫湴鍧€缁撴瀯鐨勬寚閽堝拰璇ュ湴鍧€缁撴瀯鐨勯暱搴︺€?/p>
5銆?span class="hljs-title">sockaddr_in
#include <netinet/in.h>
#include <arpa/inet.h>
struct sockaddr_in { sa_family_t sin_family; //鍦板潃鏃忥紙Address Family锛?/span> uint16_t sin_port ; //16浣?TCP/IP 绔彛鍙?/span> struct in_addr sin_addr; //32浣?IP 鍦板潃 char sin_zero[8]; };
浜屻€乻erver.c
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netinet/ip.h> #include<unistd.h> #include<arpa/inet.h> #include <sys/time.h> #include <unistd.h> #define UDP_PORT 8080 #define UDP_BUF_SIZE 100 #define UDP_SERV_IP_ADDR "192.168.100.178" #define UDP_CLIENT_IP_ADDR "192.168.100.51" int main(void) { int fd = -1; int len = 0; int res = 0; int i; struct sockaddr_in sin; /* 鍒涘缓socket_fd */ if((fd = socket( AF_INET, SOCK_DGRAM , 0)) < 0) { printf("*******SOCKET******** "); return -1; } /* 鍏佽缁戝畾鍦板潃蹇€熼噸鐢? */ int b_reuse = 1; setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof(b_reuse)); /* 濉厖struct sockaddr_in 缁撴瀯浣撳彉閲? */ bzero( &sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(UDP_PORT); /* 璁╂湇鍔″櫒绋嬪簭鑳界粦瀹氬湪浠绘剰鐨処P涓? */ #if 1 sin.sin_addr.s_addr = htonl(INADDR_ANY);//缁戝畾浠讳綍涓绘満涓奿p鍦板潃 #else if((inet_pton(AF_INET, UDP_SERV_IP_ADDR, (void *)&sin.sin_addr)) < 0) { printf("******INET_PTON******* "); return -1; } #endif /* 缁戝畾 */ if(bind (fd, (struct sockaddr *)&sin, sizeof(sin)) <0 ) { printf("*******bind******* "); return -1; } char buf[UDP_BUF_SIZE]; struct sockaddr_in cin; socklen_t addrlen = sizeof(cin); while(1) { bzero(buf ,UDP_BUF_SIZE);銆€銆€銆€銆€銆€
銆€銆€銆€銆€銆€銆€memset(buf,0,sizeof(buf));
銆€銆€銆€銆€銆€銆€len=0;
len = recvfrom(fd, buf, UDP_BUF_SIZE-1,0,(struct sockaddr*)&cin, &addrlen);
銆€銆€銆€銆€銆€if(len > 0) { char ipv4_addr[16]; if(!inet_ntop(AF_INET, (void *)&cin.sin_addr, ipv4_addr, sizeof(cin))) { printf("*******inet_ntop******** "); return -1; } printf("********recv from (%s:%d), date:",ipv4_addr, ntohs(sin.sin_port)); for(i = 0;i< len;i++) printf("%02X ", buf[i]); printf(" "); }
銆€銆€銆€銆€sendto(fd,buf,len,0,(struct sockaddr*)&cin, addrlen); } close(fd); return 0; }
以上是关于Linux涓媢dp缂栫▼的主要内容,如果未能解决你的问题,请参考以下文章
linux鍩虹11-bash缂栫▼(瀛楃涓叉祴璇?鍜?for寰幆)
鍦╓indows涓嬭繘琛岀殑缂栫▼浜哄憳锛屼綘鐪熺殑闇€瑕佸涔犱笅Linux
鏋佸婕敾锛氬嚱鏁板紡缂栫▼鐨勪笘鐣?| Linux 涓浗