PC端的服务器程序如下:
1 #include <stdio.h> 2 #include <winsock.h> 3 //#include <afxsock.h>; 4 5 #pragma comment(lib, "ws2_32.lib") 6 7 int main(void) 8 { 9 int a = 0; 10 SOCKET iSock = 0, tmpSock = 0; 11 int opt = 0; 12 int iRet = 0; 13 int iRes = 0; 14 struct sockaddr_in addrLocal, addrPeer; 15 WSADATA data; 16 WORD w = MAKEWORD(2, 2); 17 WSAStartup(w, &data); 18 19 iSock = socket(AF_INET, SOCK_STREAM, 0); 20 if (INVALID_SOCKET == iSock) 21 { 22 printf("Create socket error![%d]\n", GetLastError()); 23 return 0; 24 } 25 26 iRes = 1; 27 ioctlsocket(iSock, FIONBIO, (u_long FAR*)&iRes); 28 29 /* 设置本地的IP地址 */ 30 addrLocal.sin_family = AF_INET; 31 addrLocal.sin_port = htons(8960); 32 addrLocal.sin_addr.S_un.S_addr = htonl(INADDR_ANY); 33 //addrLocal.sin_addr.S_un.S_addr = inet_addr("10.10.22.100"); 34 35 iRet = bind(iSock, (SOCKADDR*)&addrLocal, sizeof(SOCKADDR_IN)); 36 if (iRet < 0) 37 { 38 printf("bind error\n"); 39 return 0; 40 } 41 42 iRet = listen(iSock, 5); 43 if (iRet < 0) 44 { 45 printf("listen error\n"); 46 return 0; 47 } 48 49 opt = sizeof(SOCKADDR); 50 while(1) 51 { 52 tmpSock = accept(iSock, (SOCKADDR *)&addrPeer, &opt); 53 printf("sleep 2s[%d].\n", tmpSock); 54 Sleep(1000); 55 } 56 57 return 0; 58 }
嵌入式设备的程序如下:
int main(void) { struct netif *pGB_netif, GB_netif; struct ip_addr IPaddr, NetMask, GateWay; u8 MacAddr[] = {0x00, 0x0a, 0x35, 0x00, 0x01, 0x02}; pGB_netif = &GB_netif; Debugout("\r\n\r\n"); Debugout("------------- Main vernsion 1.8-------------\r\n"); Debugout("-----lwIP RAW Mode Demo Application ------\r\n"); /* 1. Initialize the platform. */ if (init_platform() < 0) { ErrorInfo("Platform initailize fail!\n"); return -1; } /* 2. Set the IP */ IP4_ADDR(&IPaddr, 10, 10, 22, 10); IP4_ADDR(&NetMask, 255, 255, 255, 0); IP4_ADDR(&GateWay, 10, 10, 22, 1); print_ip_settings(&IPaddr, &NetMask, &GateWay); /* 3. Initialize the lwIP stack. */ lwip_init(); /* 4. Add IP to network interface. */ if (!xemac_add(pGB_netif, &IPaddr, &NetMask, &GateWay, MacAddr, PLATFORM_EMAC_BASEADDR)) { ErrorInfo("Not able to add IP to the network interface!\n"); return -1; } netif_set_default(pGB_netif); /* 5. Enable the interrupts. */ platform_enable_interrupts(); /* 6. Set up the network interface. */ netif_set_up(pGB_netif); /* 7. Create the TCP server. */ //GB_InitTCPSrv(); GB_InitTcpClnt(); /* 8. Dead cycle to receive data. */ while (1) { xemacif_input(pGB_netif); //GB_send(); } return 0; } err_t GB_CBF_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) { Debugout("Inside CBF_sent\n"); return ERR_OK; } err_t GB_CBF_connect(void *arg, struct tcp_pcb *pTCPpcb, err_t errNum) { //err_t errNum = 0; Debugout("Inside CBF for connect!\n"); //g_pClntPCB = pTCPpcb; tcp_arg(pTCPpcb, NULL); tcp_sent(pTCPpcb, GB_CBF_sent); return ERR_OK; } static int GB_InitTcpClnt(void) { struct tcp_pcb *pClntPcb = NULL; err_t errNum = 0; char cRet = 0; struct ip_addr RemoteIP; pClntPcb = tcp_new(); if (NULL == pClntPcb) { ErrorInfo("Create client PCB error!\n"); return RET_FAIL; } cRet = tcp_bind(pClntPcb, IPADDR_ANY, CONTROL_CHANNEL_PORT); if (ERR_OK != cRet) { ErrorInfo("Bind Error!\n"); return RET_FAIL; } //tcp_arg(pClntPcb, NULL); //tcp_setprio(pClntPcb, TCP_PRIO_NORMAL+1); IP4_ADDR(&RemoteIP, 10,10,22,100); errNum = tcp_connect(pClntPcb, &RemoteIP, (u16_t)8960, GB_CBF_connect); if (ERR_OK != errNum) { ErrorInfo("Client Connect Error![%d]\n", errNum); return RET_FAIL; } g_pClntPCB = pClntPcb; //tcp_output(pClntPcb); //tcp_sent(pClntPcb, ); //Debugout("Client TCP Created succussfully\n"); }
运行程序后,通过抓包工具,能看到使用lwIP的嵌入式设备发送给PC机的syn包,但是没有看到PC机的ack包,请问这个问题的原因是什么啊?
原因是TCP服务器程序所在的PC机的windows防火墙拒绝了lwIP stack的TCP连接请求。