⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c-netapi2.html

📁 Vxworks网络编程指南
💻 HTML
📖 第 1 页 / 共 4 页
字号:
* * RETURNS: OK, or ERROR if the message could not be sent to the server. */  STATUS tcpClient     (     char *              serverName     /* name or IP address of server */     )     {     struct request      myRequest;     /* request to send to server */     struct sockaddr_in  serverAddr;    /* server's socket address */     char                replyBuf[REPLY_MSG_SIZE]; /* buffer for reply */     char                reply;         /* if TRUE, expect reply back */     int                 sockAddrSize;  /* size of socket address structure */     int                 sFd;           /* socket file descriptor */     int                 mlen;          /* length of message */      /* create client's socket */     if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR)         {         perror ("socket");         return (ERROR);         }      /* bind not required - port number is dynamic */     /* build server socket address */     sockAddrSize = sizeof (struct sockaddr_in);     bzero ((char *) &amp;serverAddr, sockAddrSize);     serverAddr.sin_family = AF_INET;     serverAddr.sa_len = (u_char) sockAddrSize;     serverAddr.sin_port = htons (SERVER_PORT_NUM);      if (((serverAddr.sin_addr.s_addr = inet_addr (serverName)) == ERROR) &amp;&amp;         ((serverAddr.sin_addr.s_addr = hostGetByName (serverName)) == ERROR))         {         perror ("unknown server name");         close (sFd);         return (ERROR);         }      /* connect to server */     if (connect (sFd, (struct sockaddr *) &amp;serverAddr, sockAddrSize) == ERROR)         {         perror ("connect");         close (sFd);         return (ERROR);         }      /* build request, prompting user for message */     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, &amp;reply, 1);     switch (reply)         {         case 'y':         case 'Y': myRequest.reply = TRUE;             break;         default: myRequest.reply = FALSE;             break;         }      /* send request to server */      if (write (sFd, (char *) &amp;myRequest, sizeof (myRequest)) == ERROR)         {         perror ("write");         close (sFd);         return (ERROR);         }      if (myRequest.reply)        /* if expecting reply, read and display it */          {         if (read (sFd, replyBuf, REPLY_MSG_SIZE) &lt; 0)             {             perror ("read");             close (sFd);             return (ERROR);             }</a></b><dd> <b><a name="88874">        printf ("MESSAGE FROM SERVER:\n%s\n", replyBuf);         }</a></b><dd> <b><a name="88877">    close (sFd);     return (OK);     }</a></b></pre></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="88881">/* tcpServer.c - TCP server example */</a></b><dd> <b><a name="88883">/* DESCRIPTION This file contains the server-side of the VxWorks TCP example code. The example code demonstrates the useage of several BSD 4.4-style socket routine calls. */</a></b><dd> <b><a name="88890">/* includes */ #include "vxWorks.h" #include "sockLib.h" #include "inetLib.h" #include "taskLib.h" #include "stdioLib.h" #include "strLib.h" #include "ioLib.h" #include "fioLib.h" #include "tcpExample.h"</a></b><dd> <b><a name="88901">/* function declarations */</a></b><dd> <b><a name="88903">VOID tcpServerWorkTask (int sFd, char * address, u_short port);</a></b><dd> <b><a name="88905">/**************************************************************************** * * tcpServer - accept and process requests over a TCP socket * * This routine creates a TCP socket, and accepts connections over the socket * from clients.  Each client connection is handled by spawning a separate * task to handle client requests. * * This routine may be invoked as follows: *       -&gt; sp tcpServer *       task spawned: id = 0x3a6f1c, name = t1 *       value = 3829532 = 0x3a6f1c *       -&gt; MESSAGE FROM CLIENT (Internet Address 150.12.0.10, port 1027): *       Hello out there * * RETURNS: Never, or ERROR if a resources could not be allocated. */</a></b><dd> <b><a name="88923">STATUS tcpServer (void)     {     struct sockaddr_in  serverAddr;    /* server's socket address */     struct sockaddr_in  clientAddr;    /* client's socket address */     int                 sockAddrSize;  /* size of socket address structure */     int                 sFd;           /* socket file descriptor */     int                 newFd;         /* socket descriptor from accept */     int                 ix = 0;        /* counter for work task names */     char                workName[16];  /* name of work task */</a></b><dd> <b><a name="88933">    /* set up the local address */</a></b><dd> <b><a name="88935">    sockAddrSize = sizeof (struct sockaddr_in);     bzero ((char *) &amp;serverAddr, sockAddrSize);     serverAddr.sin_family = AF_INET;     serverAddr.sa_len = (u_char) sockAddrSize;     serverAddr.sin_port = htons (SERVER_PORT_NUM);     serverAddr.sin_addr.s_addr = htonl (INADDR_ANY);</a></b><dd> <b><a name="88941">    /* create a TCP-based socket */</a></b><dd> <b><a name="92396">    if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR)         {         perror ("socket");         return (ERROR);         }</a></b><dd> <b><a name="92397">    /* bind socket to local address */</a></b><dd> <b><a name="92399">    if (bind (sFd, (struct sockaddr *) &amp;serverAddr, sockAddrSize) == ERROR)         {         perror ("bind");         close (sFd);         return (ERROR);         }</a></b><dd> <b><a name="88958">    /* create queue for client connection requests */</a></b><dd> <b><a name="88960">    if (listen (sFd, SERVER_MAX_CONNECTIONS) == ERROR)         {         perror ("listen");         close (sFd);         return (ERROR);         }</a></b><dd> <b><a name="88967">    /* accept new connect requests and spawn tasks to process them */</a></b><dd> <b><a name="88969">    FOREVER         {         if ((newFd = accept (sFd, (struct sockaddr *) &amp;clientAddr,             &amp;sockAddrSize)) == ERROR)             {             perror ("accept");             close (sFd);             return (ERROR);             }</a></b><dd> <b><a name="88979">        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)             {             /* if taskSpawn fails, close fd and return to top of loop */              perror ("taskSpawn");             close (newFd);             }         }     }  /**************************************************************************** * * tcpServerWorkTask - process client requests * * This routine reads from the server's socket, and processes client * requests.  If the client requests a reply message, this routine * will send a reply to the client. * * RETURNS: N/A. */  VOID tcpServerWorkTask     (     int                 sFd,            /* server's socket fd */     char *              address,        /* client's socket address */     u_short             port            /* client's socket port */     )     {     struct request      clientRequest;  /* request/message from client */     int                 nRead;          /* number of bytes read */     static char         replyMsg[] = "Server received your message";      /* read client request, display message */      while ((nRead = fioRead (sFd, (char *) &amp;clientRequest,         sizeof (clientRequest))) &gt; 0)         {         printf ("MESSAGE FROM CLIENT (Internet Address %s, port %d):\n%s\n",                  address, port, clientRequest.message);          free (address);                 /* free malloc from inet_ntoa() */          if (clientRequest.reply)             if (write (sFd, replyMsg, sizeof (replyMsg)) == ERROR)                 perror ("write");         }      if (nRead == ERROR)                 /* error from read() */         perror ("read");      close (sFd);                        /* close server socket connection */     }</a></b></pre></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="89036">7.2.2  &nbsp;&nbsp;Datagram Sockets (UDP)</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="89038"> </a>You can use datagram (UDP) sockets to implement a simple client-server communication system. You can also use UDP sockets to handle multicasting. </p></dl></dl><dl class="margin"><dd><font face="Helvetica, sans-serif" size="-1" class="sans"><h5 class="HU"><i><a name="89039">Using a Datagram Socket to Implement a Client-Server Communication System</a></i></h5></font><dl class="margin"><dd><p class="Body"><a name="89041"> </a>The following code example uses a client-server communication model.  The server communicates with clients using datagram-oriented (UDP) sockets.  The main server loop, in <b class="routine"><i class="routine">udpServer</i></b><b>(</b>&nbsp;<b>)</b>, reads requests and optionally displays the client's message.  The client builds the request by prompting the user for input.  Note that this code assumes that it executes on machines that have the same data sizes and alignment.</p></dl></dl><h4 class="EntityTitle"><a name="89042"><font face="Helvetica, sans-serif" size="-1" class="sans">Example 7-2:&nbsp;&nbsp;Datagram Sockets (UDP)</font></a></h4><dl class="margin"><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="89044">/* udpExample.h - header used by both UDP server and client examples */  #define SERVER_PORT_NUM         5002    /* server's port number for bind() */ #define REQUEST_MSG_SIZE        1024    /* max size of request message */  /* structure used for client's request */  struct request     {     int  display;                       /* TRUE = display message */     char message[REQUEST_MSG_SIZE];     /* message buffer */     };</a></b></pre></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="89057">/* udpClient.c - UDP client example */  /* DESCRIPTION This file contains the client-side of the VxWorks UDP example code. The example code demonstrates the useage of several BSD 4.4-style socket routine calls. */   /* includes */  #include "vxWorks.h" #include "sockLib.h" #include "inetLib.h" #include "stdioLib.h" #include "strLib.h" #include "hostLib.h" #include "ioLib.h" #include "udpExample.h"  /**************************************************************************** * * udpClient - send a message to a server over a UDP socket * * This routine sends a user-provided message to a server over a UDP socket. * Optionally, this routine can request that the server display the message. * This routine may be invoked as follows: *       -&gt; udpClient "remoteSystem" *       Message to send: *       Greetings from UDP client *       Would you like server to display your message (Y or N):  *       y *       value = 0 = 0x0 * * RETURNS: OK, or ERROR if the message could not be sent to the server. */  STATUS udpClient     (     char *              serverName     /* name or IP address of server */     )     {     struct request      myRequest;     /* request to send to server */     struct sockaddr_in  serverAddr;    /* server's socket address */     char                display;       /* if TRUE, server prints message */     int                 sockAddrSize;  /* size of socket address structure */     int                 sFd;           /* socket file descriptor */      int                 mlen;          /* length of message */       /* create client's socket */      if ((sFd = socket (AF_INET, SOCK_DGRAM, 0)) == ERROR)         {         perror ("socket");         return (ERROR);         }      /* bind not required - port number is dynamic */      /* build server socket address */      sockAddrSize = sizeof (struct sockaddr_in);     bzero ((char *) &amp;serverAddr, sockAddrSize);     serverAddr.sa_len = (u_char) sockAddrSize;     serverAddr.sin_family = AF_INET;     serverAddr.sin_port = htons (SERVER_PORT_NUM);      if (((serverAddr.sin_addr.s_addr = inet_addr (serverName)) == ERROR) &amp;&amp;         ((serverAddr.sin_addr.s_addr = hostGetByName (serverName)) == ERROR))         {         perror ("unknown server name");         close (sFd);         return (ERROR);         }      /* build request, prompting user for message */      printf ("Message to send: \n");     mlen = read (STD_IN, myRequest.message, REQUEST_MSG_SIZE);     myRequest.message[mlen - 1] = '\0';      printf ("Would you like the server to display your message (Y or N): \n");     read (STD_IN, &amp;display, 1);     switch (display)         {         case 'y':         case 'Y': myRequest.display = TRUE;             break;         default: myRey = FALSE;             break;         }      /* send request to server */      if (sendto (sFd, (caddr_t) &amp;myRequest, sizeof (myRequest), 0,         (struct sockaddr *) &amp;serverAddr, sockAddrSize) == ERROR) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -