问题陈述:我必须做一个示例练习来取得更大的成就。为此,我必须创建一个涉及创建套接字的C程序。此套接字任务是读取来自VpnService的所有数据包,对其进行处理,并将其写回到tun接口,该接口进一步写入tun0,如下所示。此C套接字仅在Android上运行。
C服务器代码如下所示:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
int tun_socket()
{
int sockfd, clisockfd, portno;
socklen_t clilen;
char buffer[BUFSIZ];
struct sockaddr_in serv_addr, cli_addr;
int n;
//int optval;
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
return(1);
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5000;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
int opt_val = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof opt_val);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
perror("ERROR on binding");
return(1);
}
listen(sockfd,5);
clilen = (socklen_t) sizeof(cli_addr);
clisockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if (clisockfd < 0)
{
perror("ERROR on accept");
return(1);
}
bzero(buffer,BUFSIZ);
while((n = recv(clisockfd, buffer, BUFSIZ, 0)) > 0)
{
/* If connection is established then start communicating */
if (n < 0)
{
perror("ERROR reading from socket");
return(1);
}
printf("Proxy: %s\n",buffer);
n = send(clisockfd, buffer, BUFSIZ, 0);
bzero(buffer,BUFSIZ);
}
if (n < 0)
{
perror("ERROR writing to socket");
return(1);
}
close(sockfd);
return 0;
}
VPN服务java文件如下,该文件取自LocalVpn Hexene [Localvpn service](https://github.com/hexene/LocalVPN/blob/master/app/src/main/java/xyz/hexene/localvpn/LocalVPNService.java)
我应该在LocalVpn服务中添加什么代码,以便此隧道可以将数据发送到上述套接字。
TIA