标题:vxworks中的UDP通行求助!!!!
只看楼主
shuaifengyun
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-6-20
 问题点数:0 回复次数:0 
vxworks中的UDP通行求助!!!!
想使用UDP实现类似QQ的聊天程序,代码如下:
#include "vxWorks.h"
#include "sockLib.h"
#include "inetLib.h"
#include "stdioLib.h"
#include "strLib.h"
#include "hostLib.h"
#include "ioLib.h"

char buffer[1024];
char sendBuffer[1024];
char receiveBuffer[1024];
int SFD;
char *sendToname="192.168.8.203";
int port=5151;
struct sockaddr_in localAddr;
struct sockaddr_in remoteAddr;
int socketLength;
unsigned   long   flag   =   1;  
int flags=1;
int receiveId;
int sendId;
void sendMessage(void);
void receiveMessage(void);
void main()
{

      /*-------创建发送的套接字-------*/
    if((SFD=socket(AF_INET,SOCK_DGRAM,0))==ERROR)
            printf("socket failed\n");
        else
            printf("sock successful\n");
   
   
    /*-------------初始化remoteAddr-------------*/
   socketLength=sizeof(struct sockaddr_in);
   bzero((char*)&remoteAddr,socketLength);
   remoteAddr.sin_len=socketLength;
   remoteAddr.sin_family=AF_INET;
   remoteAddr.sin_port=htons(port);
        if((( remoteAddr.sin_addr.s_addr=inet_addr(sendToname)==ERROR)&&( remoteAddr.sin_addr.s_addr=hostGetByName(sendToname)))==ERROR)
        {
            printf("unknown sendto name");
            close(SFD);
        }   
        printf("fsdafasdfa\n");
        
        
        
   /*---------------初始化localAddr-----------------------*/
   bzero((char*)&remoteAddr,socketLength);
  localAddr.sin_addr.s_addr=htonl(INADDR_ANY);
  localAddr.sin_family=AF_INET;
  localAddr.sin_len=socketLength;
  localAddr.sin_port=htons(port);
  printf("set laocaAddr successful\n");
  
  
  /*---------------------绑定socket--------------------------------*/
  if(bind(SFD,(struct sockaddr *)&localAddr,socketLength)==ERROR)
                  printf("bind failed\n");
                  else
                      printf("bind successful\n");
  if(setsockopt(SFD,SOL_SOCKET,SO_REUSEADDR,(char *)&flags,sizeof(flags))==-1)
      printf("setsocketopt failed\n");
      

 receiveId=taskSpawn("receiveTask",100,VX_FP_TASK|VX_NO_STACK_FILL|VX_UNBREAKABLE,4000,(FUNCPTR)receiveMessage,0,
             0,0,0,0,0,0,0,0,0);
  sendId=taskSpawn("sendTask",100,VX_FP_TASK|VX_NO_STACK_FILL|VX_UNBREAKABLE,4000,(FUNCPTR)sendMessage,0,
              0,0,0,0,0,0,0,0,0);
}
/*------------------send message task实例-------------------*/
void sendMessage(void)
{
    extern struct sockaddr_in remoteAddr;
    extern int SFD;
    extern char sendBuffer[1024];
    extern int socketLength;
    strcpy(sendBuffer,"send the request");
    printf("send start1\n");
    for(;;)
    {
        printf("send start2\n");
    gets(buffer);
    if(strcmp(buffer,"send")==0)
    {
        printf("SENDMESSAGE:");
    gets(sendBuffer);
        
    if (sendto (SFD, sendBuffer, strlen(sendBuffer), 0,
                (struct sockaddr *) &remoteAddr, socketLength) == ERROR)
        {
             perror ("sendto");
        }
    }
    }

}

/*----------receive message task 实例-------------*/
void receiveMessage(void)
{
    extern int SFD;
    extern char receiveBuffer[1024];
    extern int socketLength;
   
    printf("receive start1\n");
    for(;;)
    {
        printf("receive start2\n");
    if (recvfrom (SFD, receiveBuffer, sizeof (receiveBuffer), 0,
                    (struct sockaddr *) &localAddr, &socketLength) != ERROR)
        {
        printf("GETMESSAGE:%s\n",receiveBuffer);
         
        }
   
    }

}
 程序能够正常接收数据,但在发送数据时出现错误:sendto: errno = 0x16
哪位高手帮解决下啊!!!!在此先谢过了!!!
搜索更多相关主题的帖子: void long include 
2011-06-20 13:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-343446-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.105063 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved