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

📄 socket.c.svn-base

📁 本人收集整理的一份c/c++跨平台网络库
💻 SVN-BASE
字号:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netinet/ip.h>#include <arpa/inet.h>#include "socket.h"#include "tun.h"extern int debug;intudp_socket(struct sockaddr_in *laddr, struct sockaddr_in *raddr) {	int fd;		if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 ) {		if (debug) perror("socket()");		return -1;	}		if ( bind(fd, (struct sockaddr *)laddr, sizeof(struct sockaddr_in)) == -1 ) {		if (debug) perror("bind()");		return -1;	}		return fd;}void *udp_read_tun_write( void *args ) {	struct udp_tun *foo;	foo = (struct udp_tun *)args;	int bytes = 0;	size_t fromlen;	char buf[1500];		while (1) {		if ( (bytes = recvfrom(foo->skfd, buf, sizeof(buf), 0, (struct sockaddr *)foo->addr, &fromlen)) == -1) {			if (debug) perror("recvfrom()");		}		else {			if (debug) printf("recv(): %d\n", bytes);		}				if ( (bytes = tun_write(foo->tunfd, buf, bytes) ) < 0)			fprintf(stderr, "tun_write(): %d\n", bytes);	}		return 0;}void *tun_read_udp_write( void *args ) {	struct udp_tun *foo;	foo = (struct udp_tun *)args;	int bytes = 0;	char buf[1500];	struct iphdr ip_hdr;		while (1) {		if ( (bytes = read(foo->tunfd, buf, sizeof(buf))) < 0 ) {			if (debug) perror("read()");		}		else {			if (debug) printf("read(): %d\n", bytes);		}						memcpy(&ip_hdr, &buf, sizeof(struct iphdr));		printf(">> src: %d ][ dst: %d <<\n", ip_hdr.saddr, ip_hdr.daddr );		if ( (bytes = sendto(foo->skfd, buf, bytes, 0, (struct sockaddr *)foo->addr, sizeof(struct sockaddr_in))) == -1) {			if (debug) perror("sendto()");		}		else {			if (debug) printf("send(): %d\n", bytes);		}		fflush(stdout);	}				return 0;}

⌨️ 快捷键说明

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