Mod aaa-12 本地Portal server配置
RCA:判断IP格式、端口号是否冲突,绑定IP和端口号 SOL:
This commit is contained in:
parent
6998611c5f
commit
ac2fe6783c
|
@ -0,0 +1,78 @@
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int _valid_ipv4_port(const char *str, int port)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
int fd;
|
||||||
|
int i;
|
||||||
|
volatile int local_errno;
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
fd = socket(AF_INET,SOCK_STREAM,0); //初始化socket
|
||||||
|
|
||||||
|
|
||||||
|
if(fd ==-1) //检查是否正常初始化socket
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
local_errno = errno;
|
||||||
|
|
||||||
|
ret = inet_pton(AF_INET, str ,&addr.sin_addr);
|
||||||
|
printf("the value of ret is:%d\n",ret);
|
||||||
|
if(ret > 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "\"%s\" is a vaild IPv4 address\n", str);
|
||||||
|
|
||||||
|
addr.sin_family = AF_INET; //地址结构的协议簇
|
||||||
|
addr.sin_port=htons(port); //地址结构的端口地址,网络字节序
|
||||||
|
printf("the value of str:%s\n", str);
|
||||||
|
i = (bind(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr)));
|
||||||
|
printf("the value of i:%d\n", i);
|
||||||
|
|
||||||
|
if( i < 0)
|
||||||
|
{
|
||||||
|
printf("port %d has been used. \n", port);
|
||||||
|
close(fd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("port %d is ok. \n", port);
|
||||||
|
close(fd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (ret < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "EAFNOSUPPORT: %s\n", strerror(local_errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "\"%s\" is not a vaild IPv4 address\n", str);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char a[] ="192.168.209.193";
|
||||||
|
char *pIP = a;
|
||||||
|
char *pNotIP = "192.168.0.256";
|
||||||
|
int port = 56298;
|
||||||
|
int port1 = 3369;
|
||||||
|
_valid_ipv4_port(pIP, port);
|
||||||
|
_valid_ipv4_port(pIP, port1);
|
||||||
|
_valid_ipv4_port(pNotIP, port1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
连接到服务器后,会不停循环,等待输入,
|
||||||
|
输入quit后,断开与服务器的连接
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
int clientfd;
|
||||||
|
struct sockaddr_in serverAddr;
|
||||||
|
char sendbuf[200];
|
||||||
|
char recvbuf[200];
|
||||||
|
int iDataNum;
|
||||||
|
char a[] = "192.168.209.193";
|
||||||
|
char *str = a;
|
||||||
|
int port = 61;
|
||||||
|
|
||||||
|
if((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||||
|
{
|
||||||
|
printf("socket error");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bzero(&serverAddr, sizeof(serverAddr));
|
||||||
|
serverAddr.sin_family = AF_INET;
|
||||||
|
serverAddr.sin_port = htons(port);
|
||||||
|
printf("the value of str:%s\n", str);
|
||||||
|
//将字符串的IP地址转化为网络字节序
|
||||||
|
int ret = inet_pton(AF_INET, str, &serverAddr.sin_addr);
|
||||||
|
printf("the value of ret is:%d\n",ret);
|
||||||
|
printf("inet_pton:0x%x\n", serverAddr.sin_addr.s_addr);
|
||||||
|
if(ret < 0)
|
||||||
|
{
|
||||||
|
printf("error");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = connect(clientfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
|
||||||
|
printf("the value of i is:%d\n", i);
|
||||||
|
|
||||||
|
if(i < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "socket connect error=%d(%s)\n", errno, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("连接到主机...\n");
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
printf("发送消息:");
|
||||||
|
scanf("%s", sendbuf);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
send(clientfd, sendbuf, strlen(sendbuf), 0);
|
||||||
|
if(strcmp(sendbuf, "quit") == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
printf("读取消息:");
|
||||||
|
recvbuf[0] = '\0';
|
||||||
|
iDataNum = recv(clientfd, recvbuf, 200, 0);
|
||||||
|
recvbuf[iDataNum] = '\0';
|
||||||
|
printf("%s\n", recvbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(clientfd);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,126 @@
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
监听后,一直处于accept阻塞状态,
|
||||||
|
直到有客户端连接,
|
||||||
|
当客户端如数quit后,断开与客户端的连接
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main(int args, char** argv)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
struct sockaddr_in server_addr;
|
||||||
|
struct sockaddr_in clientAddr;
|
||||||
|
int addr_len = sizeof(clientAddr);
|
||||||
|
int client;
|
||||||
|
char buffer[200];
|
||||||
|
int iDataNum;
|
||||||
|
|
||||||
|
char ip[] = "192.168.209.193";
|
||||||
|
char *str = ip;
|
||||||
|
uint16_t port = 61;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//建立socket
|
||||||
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if(fd == -1)
|
||||||
|
{
|
||||||
|
printf("socket failed");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bzero(&server_addr, sizeof(server_addr));
|
||||||
|
|
||||||
|
server_addr.sin_family = AF_INET;
|
||||||
|
server_addr.sin_port = htons(port);
|
||||||
|
printf("the value of str:%s\n", str);
|
||||||
|
//将IP地址转换成网络字节序
|
||||||
|
int ret = inet_pton(AF_INET, str, &server_addr.sin_addr);
|
||||||
|
printf("the value of ret is:%d\n",ret);
|
||||||
|
printf("inet_pton:0x%x\n", server_addr.sin_addr.s_addr);
|
||||||
|
if(ret < 0)
|
||||||
|
{
|
||||||
|
printf("error");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = bind(fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||||
|
printf("the value of i is :%d\n", i);
|
||||||
|
if(i < 0)
|
||||||
|
{
|
||||||
|
printf("bind error");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//开启监听,第二个参数是最大监听数
|
||||||
|
listen(fd,5);
|
||||||
|
if(listen(fd, 5) < 0)
|
||||||
|
{
|
||||||
|
printf("listen error");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
printf("监听端口: %d\n", port);
|
||||||
|
client = accept(fd, (struct sockaddr*)&clientAddr, (socklen_t*)&addr_len);
|
||||||
|
|
||||||
|
if(client < 0)
|
||||||
|
{
|
||||||
|
printf("accept error");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("等待消息...\n");
|
||||||
|
printf("IP is %s\n", inet_ntoa(clientAddr.sin_addr));
|
||||||
|
printf("Port is %d\n", htons(clientAddr.sin_port));
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
printf("读取消息:");
|
||||||
|
buffer[0] = '\0';
|
||||||
|
iDataNum = recv(client, buffer, 1024, 0);
|
||||||
|
if(iDataNum < 0)
|
||||||
|
{
|
||||||
|
printf("recv null");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
buffer[iDataNum] = '\0';
|
||||||
|
if(strcmp(buffer, "quit") == 0)
|
||||||
|
break;
|
||||||
|
printf("%s\n", buffer);
|
||||||
|
|
||||||
|
|
||||||
|
printf("发送消息:");
|
||||||
|
scanf("%s", buffer);
|
||||||
|
printf("\n");
|
||||||
|
send(client, buffer, strlen(buffer), 0);
|
||||||
|
if(strcmp(buffer, "quit") == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue