新手入门
- 积分
- 5
- 金钱
- 5
- 注册时间
- 2021-2-18
- 在线时间
- 10 小时
|
15金钱
本帖最后由 SFWR 于 2022-7-2 18:09 编辑
- #define DEST_IP_ADDR0 192
- #define DEST_IP_ADDR1 168
- #define DEST_IP_ADDR2 0
- #define DEST_IP_ADDR3 200
- IP4_ADDR(&server_ip, DEST_IP_ADDR0,DEST_IP_ADDR1,DEST_IP_ADDR2,<span style="background-color: transparent; color: var(--black-800); font-family: var(--ff-mono); font-size: var(--fs-body1); font-style: inherit; font-variant-ligatures: inherit; font-variant-caps: inherit; font-weight: inherit; white-space: inherit;">DEST_IP_ADDR3</span>);
复制代码 RT,在本来的代码中,只有主机静态ip为192.168.0.200才能成功连接,现在我想让任意ip不同的主机都可以连接该怎么实现呢,或者只开放一个ip段,只有静态ip为192.168.0.0-192.168.0.255的主机才能连接,如何修改代码才能实现呢
附TCPclient代码
- #include "lwip/netif.h"
- #include "lwip/ip.h"
- #include "lwip/tcp.h"
- #include "lwip/init.h"
- #include "netif/etharp.h"
- #include "lwip/udp.h"
- #include "lwip/pbuf.h"
- #include <stdio.h>
- #include <string.h>
- #include "main.h"
- #include "tcpclient.h"
- #define DEST_IP_ADDR0 192
- #define DEST_IP_ADDR1 168
- #define DEST_IP_ADDR2 0
- #define DEST_IP_ADDR3 200
- #define DEST_PORT 5001
- #define UDP_SERVER_PORT 5002
- #define UDP_CLIENT_PORT 5002
- #define LOCAL_PORT 5001
- char str_rev[100];
- static void client_err(void *arg, err_t err)
- {
- printf("wrong!!\r\n");
- printf("try again!!\r\n");
- printf("close connect\r\n");
- printf("again init\r\n");
- TCP_Client_Init();
-
- }
- static err_t client_send(void *arg, struct tcp_pcb *tpcb)
- {
- uint8_t send_buf[]= "c";
- tcp_write(tpcb, send_buf, sizeof(send_buf), 1);
- return ERR_OK;
- }
- static err_t client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
- {
- if (p != NULL)
- {
- tcp_recved(tpcb, p->tot_len);
- memset(str_rev, 0 , strlen(str_rev));
- memcpy(str_rev, p -> payload, p -> tot_len);
- memset(p->payload, 0 , p->tot_len);
- pbuf_free(p);
- }
- else if (err == ERR_OK)
- {
- printf("dis connect!\r\n");
- tcp_close(tpcb);
- TCP_Client_Init();
- }
- return ERR_OK;
- }
- static err_t client_connected(void *arg, struct tcp_pcb *pcb, err_t err)
- {
- printf("connected ok!\r\n");
- tcp_poll(pcb,client_send,2);
- tcp_recv(pcb,client_recv);
- return ERR_OK;
- }
- void TCP_Client_Init(void)
- {
- struct tcp_pcb *client_pcb = NULL; //这一句一定要放在里面,否则会没用
- ip4_addr_t server_ip; //因为客户端要主动去连接服务器,所以要知道服务器的IP地址
- client_pcb = tcp_new();
- IP4_ADDR(&server_ip, DEST_IP_ADDR0,DEST_IP_ADDR1,DEST_IP_ADDR2,<span style="background-color: transparent; color: var(--black-800); font-family: var(--ff-mono); font-size: var(--fs-body1); font-style: inherit; font-variant-ligatures: inherit; font-variant-caps: inherit; font-weight: inherit; white-space: inherit;">DEST_IP_ADDR3</span>);//合并IP地址
- printf("start connect!\r\n");
- tcp_connect(client_pcb, &server_ip, TCP_CLIENT_PORT, client_connected);
- ip_set_option(client_pcb, SOF_KEEPALIVE);
- printf("tcp_connect ok\r\n");
- tcp_err(client_pcb, client_err);
- printf("yes err f\r\n");
- }
复制代码
|
|