我用的是ZLG的TinyARMT23的模块开发板,芯片LPC2387,上面已经移植好UCOS系统了。现在想做个以太网的TCP协议。我先用的例程比较简单的程序,了解下sokect的编程。但我用例程,模块板子作为服务器,PC作为客户端,两者却不能通信,只是PC的本地连接一会识别,一会断开。下面是我的任务程序,求大虾帮看看,谢谢了。
void TASK0(void *pdata)
{
struct sockaddr serveraddr, clientaddr;
SOCKET s;
int ei;
INT8U Temp; /* TCP状态顺序号 */
INT32U LenNet;
pdata = pdata;
serveraddr.sin_family = 0; /* 设置服务器端IP和端口 */
serveraddr.sin_addr[0] = GucMCU_Ip [0];
serveraddr.sin_addr[1] = GucMCU_Ip [1];
serveraddr.sin_addr[2] = GucMCU_Ip [2];
serveraddr.sin_addr[3] = GucMCU_Ip [3];
serveraddr.sin_port = GusMCU_Port;
while (1) {
s = socket(AF_INET, SOCK_STREAM, TCP_PROTOCOL); /* 建立TCP通信SOCKET */
if (s != SOCKET_ERROR) {
break;
}
OSTimeDly(OS_TICKS_PER_SEC / 10);
}
while (1) {
ei = bind(s,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
if (ei != SOCKET_ERROR) {
break;
}
OSTimeDly(OS_TICKS_PER_SEC / 10);
}
while (1) {
ei = listen(s, 2); /* 设定监听的连接数 */
if (ei != SOCKET_ERROR) {
break;
}
}
while (1) {
Temp = accept(s, (struct sockaddr *)&clientaddr, (int *)sizeof(clientaddr));
if (Temp != SOCKET_ERROR) {
/* 确保服务器处于连接状态: */
while ((getsocktcpsta(Temp) == 3)&&(PHY_Connect() == PHY_CONNECT_OK)) {
/*
* 以TCP通信方式接收客户机端的数据:
*/
LenNet = recv(Temp, TCP_rece_data, TCP_rece_length, 0);
if (LenNet == SOCKET_RCV_ERROR) {
break;
}
else if (LenNet > 0) {
send(Temp, TCP_rece_data, LenNet, 0); /* 把接收到的数据回发给客户机 */
}
OSTimeDly(OS_TICKS_PER_SEC);
}
close(Temp);
OSTimeDly(OS_TICKS_PER_SEC / 10);
TCP_Abort(Temp); /* 释放资源 */
OSTimeDly(OS_TICKS_PER_SEC / 10);
}
OSTimeDly(OS_TICKS_PER_SEC/10);
}
}
|