vxworks网口编程

VxWorks 网络编程
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_famil

你可能喜欢

  • 网口
  • 101远程教育网
  • 110法律咨询网
  • 千兆网
  • 千兆网口
  • 千兆网络方案

vxworks网口编程相关文档

最新文档

返回顶部