使用STM32F107做TCP客户端,与上位机TCP服务器程序连接问题

zhang898907 2019-03-26 02:48:52
服务器与客户端连接不能正常连接,偶尔能连上。
麻烦各位高手看看,程序哪里有问题。
上位机TCP服务器程序,用VB写的使用WinSock控件
Private Sub Form_Load()
'将 LocalPort 属性设置为一个整数。
'然后调用 Listen 方法。

TcpServer.Protocol = sckTCPProtocol
TcpServer.LocalPort = 4000
TcpServer.Listen
End Sub

Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
'检查控件的 State 属性是否为关闭的。
'如果不是,
'在接受新的连接之前先关闭此连接。
If TcpServer.State <> sckClosed Then TcpServer.Close
'接受具有 requestID 参数的
'连接。

TcpServer.Accept requestID
End Sub

Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
'为进入的数据声明一个变量。
'调用 GetData 方法,并将数据赋予名为 txtOutput
'的 TextBox 的 Text 属性。

Dim strData As String
TcpServer.GetData strData
TxtOutput.Text = strData
End Sub


下位机使用STM32F107, TCP客户端程序
int main(void)
{
....................
LwIP_Init();
//UDP_rx_Init();
TCP_Client_init();
....................
while(1){
................
}

}

void TCP_Client_init(void)
{
struct tcp_pcb *pcb;
struct ip_addr ipaddr;
//int localPort;
//localPort = 1000 + (int)ip_address_last;
IP4_ADDR(&ipaddr,192,168,0,2); //服务器IP192.168.0.2
pcb = tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, 1001);
tcp_connect(pcb, &ipaddr, 4000, tcp_client_connected);
}

err_t tcp_client_connected(void *arg, struct tcp_pcb *pcb, err_t err)
{
//TcpPCB = pcb;
tcp_write(pcb, GREETING, strlen(GREETING), 0); //输出Str
tcp_recv(pcb,tcp_Receive);
return ERR_OK;
}

static err_t tcp_Receive(void *arg, struct tcp_pcb *pcb, struct pbuf *p_1, err_t err)
{
unsigned char *temp;
unsigned int i;

if(p_1 != NULL) /* 如果收到的数据不为空 */
{ rec_data_size = (*p_1).tot_len;
temp = p_1->payload;
for(i=0;i<rec_data_size;i++)
{
eth_rec_data[i] = *temp;
temp++;
}
pbuf_free(p_1); /* 释放缓冲区数据 */
rec_ethernet_flag = 1;
}
return ERR_OK;
}
...全文
152 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

27,377

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧