1 Star 0 Fork 2

sina_woker/udptunnel

forked from longchun/udptunnel 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
socket.c 1.65 KB
一键复制 编辑 原始数据 按行查看 历史
Felipe Astroza 提交于 2013-08-10 03:27 . new source header
/*
* udptunnel
* Copyright © 2013 Felipe Astroza A.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int socket_create(unsigned short port)
{
struct sockaddr_in sa;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd == -1) {
perror("socket");
exit(1);
}
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = port;
sa.sin_family = AF_INET;
if(bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
perror("bind");
exit(1);
}
return fd;
}
void socket_put_packet(int fd, struct sockaddr_in *sa, socklen_t salen, char *buf, unsigned int buflen)
{
if(sendto(fd, buf, buflen, 0, (struct sockaddr *)sa, salen) == -1) {
perror("sendto");
exit(1);
}
}
unsigned int socket_get_packet(int fd, struct sockaddr_in *sa, socklen_t *salen, char *buf, unsigned int bufsize)
{
return recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)sa, salen);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/lin-binghua/udptunnel.git
[email protected]:lin-binghua/udptunnel.git
lin-binghua
udptunnel
udptunnel
master

搜索帮助