VxWorks 网络编程

4843阅读 1评论2010-04-03 red_eyed_hare
分类:

tcpExample.h文件:
 
#define SERVER_PORT_NUM   5150 /*bind()绑定服务器端口号*/
#define SERVER_WORK_PRIORITY  100  /*服务器任务优先级*/
#define SERVER_STACK_SIZE   10000 /*服务器任务的协议栈大小*/
#define SERVER_MAX_CONNECTIONS  4  /*同时连接的最大客户端数*/
#define REQUEST_MSG_SIZE   1024 /*请求信息长度的最大值*/
#define REPLY_MSG_SIZE    500  /*返回信息长度最大值*/
struct request
{
 int reply;       /*TRUE=向服务器发送的请求*/
 int msgLen;       /*信息长度*/
 char message[REQUEST_MSG_SIZE];  /*信息缓冲区*/
};
 
tcpClient.c文件:
 
#include "vxWorks.h"
#include "sockLib.h"
#include "inetLib.h"
#include "stdioLib.h"
#include "strLib.h"
#include "hostLib.h"
#include "ioLib.h"
#include "tcpExample.h"
#include "string.h"
#include "stdio.h"
STATUS tcpClient(char *serverName)
{
 struct request myRequest;
 struct sockaddr_in serverAddr;
 char  replyBuf[REPLY_MSG_SIZE];
 char  reply;
 int   sockAddrSize;
 int  sFd;
 int  mlen;
 if((sFd=socket(AF_INET,SOCK_STREAM,0))==ERROR)
 {
  perror("socket");
  return (ERROR);
 }
 
 sockAddrSize=sizeof(struct sockaddr_in);
 bzero((char*)&serverAddr,sockAddrSize);
 serverAddr.sin_family=AF_INET;
 serverAddr.sin_len=(u_char)sockAddrSize;
 serverAddr.sin_port=htons(SERVER_PORT_NUM);
 if(((serverAddr.sin_addr.s_addr=inet_addr(serverName))==ERROR)
   &&((serverAddr.sin_addr.s_addr=hostGetByName(serverName))==ERROR))
 {
  perror("unknown server name");
  close (sFd);
  return (ERROR);
 }
 if (connect (sFd,(struct sockaddr*)&serverAddr,sockAddrSize)==ERROR)
 {
  perror("connect");
  close (sFd);
  return (ERROR);
 }
 
 printf("message to send: \n");
 mlen=read(STD_IN,myRequest.message,REQUEST_MSG_SIZE);
 myRequest.msgLen = mlen;
 myRequest.message[mlen-1]='\0';
 printf("would you like a reply (Y or N): \n");
 read(STD_IN,&reply,1);
 switch (reply)
 {
 case'Y':
 case'y':myRequest.reply=TRUE;
   break;
 default:myRequest.reply=FALSE;
   break;
 }
 if(write(sFd,myRequest.message,sizeof(myRequest.message))==ERROR)
 {
  perror("write");
  close(sFd);
  return (ERROR);
 }
 if(myRequest.reply)
 {
  if(read(sFd,replyBuf,REPLY_MSG_SIZE<0))
  {
   perror("read");
   close(sFd);
   return(ERROR);
  }
  printf("message from server:\n%s\n",replyBuf);
 }
 close(sFd);
 return (OK);
 }
tcpServer.c文件:
 
#include "vxWorks.h"
#include "sockLib.h"
#include "inetLib.h"
#include "stdioLib.h"
#include "strLib.h"
#include "hostLib.h"
#include "ioLib.h"
#include "tcpExample.h"
#include "string.h"
#include "stdio.h"
#include "taskLib.h"
#include "fioLib.h"
void tcpServerWorkTask(int sFd,char *address,u_short port);
STATUS tcpServer(void)
{
 struct  sockaddr_in serverAddr;
 struct sockaddr_in clientAddr;
 int  sockAddrSize;
 int  sFd;
 int  newFd;
 int  ix=0;
 char workName[16];
 
 sockAddrSize=sizeof(struct sockaddr_in);
 bzero((char*)&serverAddr,sockAddrSize);
 serverAddr.sin_family=AF_INET;
 serverAddr.sin_len=(u_char)sockAddrSize;
 serverAddr.sin_port=htons(SERVER_PORT_NUM);
 serverAddr.sin_addr.s_addr=htonl (INADDR_ANY);
 
 if((sFd=socket(AF_INET,SOCK_STREAM,0))==ERROR)
 {
  perror("socket");
  return (ERROR);
 }
 
 if (bind(sFd,(struct sockaddr*)&serverAddr,sizeof(serverAddr))==ERROR)
 {
  perror("bind");
  close(sFd);
  return (ERROR);
 }
 
 if(listen (sFd,SERVER_MAX_CONNECTIONS)==ERROR)
 {
  perror("listen");
  close(sFd);
  return (ERROR);
 }
 
 while(1)
 {
  if((newFd=accept (sFd,(struct sockaddr *)&clientAddr,&sockAddrSize))==ERROR)
  {
   perror("accept");
   close(sFd);
   return (ERROR); 
  }
  sprintf(workName,"tTcpWork&d",ix++);
  if(taskSpawn(workName,SERVER_WORK_PRIORITY,0,SERVER_STACK_SIZE,
    (FUNCPTR)tcpServerWorkTask,newFd,(int)inet_ntoa(clientAddr.sin_addr),
    ntohs(clientAddr.sin_port),0,0,0,0,0,0,0)==ERROR)
  {
   perror("taskSpawn");
   close(newFd);
  }
 }
}
void tcpServerWorkTask(int sFd,char*address,u_short port)
{
 struct request clientRequest;
 int nRead;
 static char replyMsg[]="server received your message!\n";
 
 while((nRead=fioRead(sFd,clientRequest.message,sizeof(clientRequest.message)))>0)
 {
  clientRequest.message[nRead]=0;
  printf("message from client (internet address %s,port %d):\n%s\n",
    address,port,clientRequest.message);
  free(address);
  if(write(sFd,replyMsg,sizeof(replyMsg))==ERROR)
   perror("write");
  if(nRead==ERROR)
   perror("read");
  close(sFd);
 }
}
上一篇:VxWorks下串口应用编程
下一篇:VxWorks下基于tffs文件操作

文章评论