|  | 
 
| 大家好,我自己编写了一个uClinux的tcp客户端程序,在另外一台电脑,用VC++编写了一个服务器端程序,希望能建立目标板和上位机之间的网络通讯. 我的tcp客户端程序如下:
 //* tcpclient.c
 // * use TCP to send a message to a host,then waits for reply
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <stdio.h>
 #include <sys/time.h>
 #include <errno.h>
 #include <string.h>
 
 #define RECEIVER_PORT 8000  //signate the receive port
 #define SENDER_PORT 3400   // signate the send port
 #define BUFSIZE 10000  // signate the size of buffer
 
 extern int errno;
 //reporting the error when erroring,and terminate the programe
 void report_error(char *s)
 {
 printf("sender:error in %s,errno=%d\n",s,errno);
 exit(1);
 }
 
 int main(int argc,char *argv[])
 {
 unsigned int s,len;
 struct sockaddr_in sa={0},r_sa={0};  /*fill initially with zeros*/
 unsigned int sa_l=sizeof(r_sa);
 struct hostent *hp;
 char receiver_host[50]="210.34.53.219";
 
 float  data[3]={12.4545,34.3454};
 int ack_len=-1;
 char msg[1024];
 len =8;
 
 if(argc>=2) len=atol(argv[1]);  //read in Message Size
 if(argc>=3) strcpy(receiver_host,argv[2]); //read in receiver
 
 /*form the server socket */
 if((hp=gethostbyname(receiver_host))==NULL)
 report_error("gethostbyname");
 bcopy((char *)hp->h_addr,(char *)&r_sa.sin_addr,hp->h_length);
 r_sa.sin_family=AF_INET;
 r_sa.sin_port=htons(RECEIVER_PORT);
 printf("sending to %s :%d \n ",\
 inet_ntoa(*((struct in_addr *)hp->h_addr)),r_sa.sin_port);
 sa.sin_addr.s_addr=INADDR_ANY;
 sa.sin_family=AF_INET;
 sa.sin_port=htons(SENDER_PORT);
 
 /*create the socket */
 if((s=socket(AF_INET,SOCK_STREAM,PF_UNSPEC))==-1)
 report_error("socket");
 
 /*link to the server */
 if(connect(s,(struct sockaddr *)&r_sa,sa_l)==-1)
 report_error("connnect");
 printf("connect successful...\n");
 
 while(1)
 {
 if(send(s,data,len,0)==-1)
 report_error("send");
 if((ack_len=recv(s,msg,1000,0))==-1)
 report_error("recv");
 if(ack_len!=4)
 {
 printf("ack not correct ,%s",msg);
 exit(-1);
 }
 data[0]++;
 data[1]++;
 sleep(1);
 };
 
 close(s);
 return 0;
 }
 服务器端的程序使用了MFC的socket类CAsyncSocket,其中负责接收数据的程序如下:
 void CSock4Dlg::OnReceive()
 {
 float *pBuf=new float[10];  //double contains 8 Bytes
 //float contains 4 bytes
 int iBufSize=8;
 int iRcvd;
 iRcvd=m_sConnectSocket.Receive(pBuf,iBufSize);
 //Did we recerve anything?
 if(iRcvd==SOCKET_ERROR)
 {
 AfxMessageBox("Receive data Error... ...");
 }
 else
 {
 m_iCurrent=pBuf[0];
 m_iVoltage=pBuf[1];
 m_strStatic="Client IP1 got connected... ...";
 //Sync the varialbes with the controls
 UpdateData(FALSE);
 char *Ack="ack0";
 int iLen=4;
 m_sConnectSocket.Send(LPCTSTR(Ack),iLen);
 }
 }
 然后我把程序下载到板子上,开始运行,头20分钟都运行良好,但是20分钟之后服务器端接收数据停止,在客户机一段的程序,显示sender:error in recv,errno=104  ,然后终止运行.
 好像是在客户机端接收ack信号时出了问题,可是前20分钟都运行的好好的啊,这是怎么回事?按理说,客户机的程序应该无限制的执行下去才对啊...
 使用的是S3C4510B的开发板,不知道是硬件的问题,还是软件编程的问题,还请网络通讯达人指教,在下感激不尽... ...
 | 
 |