Chinaunix首页 | 论坛 | 博客
  • 博客访问: 554461
  • 博文数量: 104
  • 博客积分: 4131
  • 博客等级: 上校
  • 技术积分: 1137
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-31 15:05
文章分类

全部博文(104)

文章存档

2011年(13)

2010年(23)

2009年(68)

我的朋友

分类:

2010-04-03 18:34:54

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);
 }
}
阅读(4712) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

leehomwanglj2012-05-27 22:11:15

thank you!一直在找VXWorks网络编程资料,问你个问题哈,你是在虚拟机上运行Vxworks系统吗?