Annotation of mstools/samples/nwlink/dgrecv/dgrecv.c, revision 1.1.1.1

1.1       root        1: /****************************************************************************\
                      2: *  dgrecv.c -- sample program demonstrating NWLink.
                      3: *
                      4: *       Microsoft Developer Support
                      5: *       Copyright (c) 1992, 1993 Microsoft Corporation
                      6: *
                      7: *  This program is a simple example of opening a socket,
                      8: *  binding to the socket, and waiting for a datagram packet.
                      9: * 
                     10: ****************************************************************************/
                     11: #include <windows.h>
                     12: #include <winsock.h>
                     13: #include <stdio.h>
                     14: #include <stdlib.h>
                     15: #include <string.h>
                     16: #include <malloc.h>
                     17: #include <wsipx.h>
                     18: #include <wsnwlink.h>
                     19: #include "../testlib/testlib.h"
                     20: 
                     21: /*
                     22: *   Sockaddr structures 
                     23: */
                     24: 
                     25: SOCKADDR_IPX addr;
                     26: SOCKADDR_IPX baddr;
                     27: SOCKADDR_IPX raddr;
                     28: 
                     29: /*
                     30: *   Function Prototypes 
                     31: */
                     32: 
                     33: extern int main(int, char **);
                     34: extern int net_init(SOCKET *);
                     35: extern int dg_recv(SOCKET);
                     36: 
                     37: /****************************************************************************
                     38: *
                     39: *    FUNCTION:  main( int argc, char **argv )
                     40: *
                     41: *    PURPOSE:   This is the main entry for the program
                     42: *                  
                     43: *
                     44: *    ARGUMENTS:        argc = Number of arguments
                     45: *               argv = Array of ptrs to cmd line args
                     46: *                
                     47: *
                     48: *       RETURNS:   Exit code for the program
                     49: *                              
                     50: *\***************************************************************************/
                     51: int main(int argc, char **argv)
                     52: {
                     53:     SOCKET s;
                     54: 
                     55:     /*
                     56:     *   Initialize our default values before checking the command line 
                     57:     */
                     58: 
                     59:     *Local_Socket_Number = 0x06;
                     60:     *(Local_Socket_Number+1) = 0x00;
                     61: 
                     62:     /*
                     63:     *   Parse the command line to set up any command line options 
                     64:     */
                     65: 
                     66:     parse_cmd_line(argc, argv);
                     67: 
                     68:     /*
                     69:     *   Initialize the network and set up our socket 
                     70:     */
                     71: 
                     72:     if (net_init(&s))
                     73:         return 1;
                     74: 
                     75:     /*
                     76:     *   Do the receive thing 
                     77:     */
                     78: 
                     79:     if (dg_recv(s))
                     80:         return 1;
                     81: 
                     82:     /*
                     83:     *   All Done - Close up the socket and exit 
                     84:     */
                     85: 
                     86:     if (verbose)
                     87:        printf("Calling closesocket()\n");
                     88: 
                     89:     closesocket(s);
                     90: 
                     91:     return 0;
                     92: }
                     93: 
                     94: /****************************************************************************
                     95: *
                     96: *    FUNCTION:  net_init( SOCKET *skt )
                     97: *
                     98: *    PURPOSE:   Initializes the WinSock stuff and sets up our socket.
                     99: *                  
                    100: *
                    101: *    ARGUMENTS:        SOCKET * => struct to receive our socket info   
                    102: *
                    103: *       RETURNS:   0 if ok
                    104: *                              1 if error
                    105: *
                    106: *\***************************************************************************/
                    107: int net_init(SOCKET *skt)
                    108: {
                    109:     SOCKET s;
                    110:     int rc, addrlen = 16;
                    111:     WSADATA wsdata;
                    112:     WORD    wVersionRequested;
                    113: 
                    114:     if (verbose)
                    115:         printf("Calling WSAStartup(), ");
                    116: 
                    117:     /*
                    118:     *   Initialize with the WINSOCK library 
                    119:     */
                    120: 
                    121:     wVersionRequested = MAKEWORD(1,1);
                    122:     rc = WSAStartup(wVersionRequested, &wsdata);
                    123: 
                    124:     if (verbose)
                    125:         printf("return = 0x%X (%d)\n", rc, rc);
                    126: 
                    127:     if (rc) {
                    128:         printf("WSAStartup failed: error code = %d\n", rc);
                    129:         return 1;
                    130:     }
                    131: 
                    132:     if (verbose) {
                    133:         printf("Contents of wsadata struct:\n");
                    134:         print_wsa(&wsdata);
                    135:     }
                    136: 
                    137:     /*
                    138:     *   Open a DATAGRAM socket with IPX 
                    139:     */
                    140: 
                    141:     if (verbose)
                    142:         printf("Calling socket(address family = %d, socket type = %d, protocol = %d)\n", Local_Address_Family, Socket_Type, Protocol);
                    143: 
                    144:     s = socket(Local_Address_Family, Socket_Type, Protocol);
                    145: 
                    146:     if (verbose)
                    147:        printf("socket() returned 0x%X (%d)\n", s, s);
                    148: 
                    149:     if (s == INVALID_SOCKET) {
                    150:         dos_net_perror("Socket call failed");
                    151:         exit(1);
                    152:     }
                    153: 
                    154:     /*
                    155:     *    Bind to a socket.  We dont care what socket we bind to,
                    156:     *    so we will send down all 0's
                    157:     */
                    158: 
                    159:     addr.sa_family = Local_Address_Family;
                    160: 
                    161:     memcpy(&addr.sa_netnum, Local_Network_Number, 4);
                    162:     memcpy(&addr.sa_nodenum, Local_Node_Number, 6);
                    163:     memcpy(&addr.sa_socket, Local_Socket_Number, 2);
                    164: 
                    165:     if (verbose) {
                    166:         printf("calling bind(), local address =\n  ");
                    167:         print_saddr(&addr);
                    168:     }
                    169: 
                    170:     rc = bind(s, (struct sockaddr *) &addr, 16);
                    171: 
                    172:     if (verbose)
                    173:         printf("\nbind() returned 0x%X (%d)\n", rc, rc);
                    174: 
                    175:     if (rc == SOCKET_ERROR) {
                    176:         dos_net_perror("Error binding to socket");
                    177:         closesocket(s);
                    178:         return 1;
                    179:     }
                    180: 
                    181:     /*
                    182:     *   Get the address we bound to and print it out 
                    183:     */
                    184: 
                    185:     if (verbose)
                    186:         printf("calling getsockname(socket = %d), ", s);
                    187: 
                    188:     rc = getsockname(s, (struct sockaddr *) &baddr, &addrlen);
                    189: 
                    190:     if (verbose)
                    191:         printf("return = 0x%lX (%d)\n", rc, rc);
                    192: 
                    193:     if (rc == SOCKET_ERROR) {
                    194:         dos_net_perror("Error getting socket name");
                    195:         closesocket(s);
                    196:         return 1;
                    197:     }
                    198: 
                    199:     /*
                    200:     *   Set the packet type to send for this socket 
                    201:     */
                    202: 
                    203:     if (verbose)
                    204:         printf("Calling setsockopt for packet type %d\n", Local_Packet_Type);
                    205: 
                    206:     rc = setsockopt(s, NSPROTO_IPX, IPX_PTYPE, (const char *) &Local_Packet_Type, 4);
                    207: 
                    208:     if (rc == SOCKET_ERROR)
                    209:         dos_net_perror("setsockopt() call failed");
                    210: 
                    211: 
                    212:     if (Filter_Packet_Type) {
                    213: 
                    214:         /*
                    215:         *   Set the packet type for this socket 
                    216:         */
                    217: 
                    218:         if (verbose)
                    219:             printf("Calling setsockopt to filter packet type %d\n", Filter_Packet_Type);
                    220: 
                    221:        rc = setsockopt(s, NSPROTO_IPX, IPX_FILTERPTYPE, (const char *) &Filter_Packet_Type, sizeof(int));
                    222:        printf("RC from FILTER SOCKOPT = %d\n", rc);
                    223: 
                    224:         if (rc == SOCKET_ERROR)
                    225:             dos_net_perror("setsockopt() call failed setting filter packet type");
                    226:     }
                    227:     /*
                    228:     *   Print out the network address 
                    229:     */
                    230: 
                    231:     if (verbose) {
                    232:         printf("addrlen = %d\n", addrlen);
                    233:         print_netaddr(baddr.sa_netnum, "  Bound to address ", "\n");
                    234:     }
                    235: 
                    236:     *skt = s;
                    237: 
                    238:     return 0;
                    239: }
                    240: 
                    241: /****************************************************************************
                    242: *
                    243: *    FUNCTION:  dg_recv( SOCKET s )
                    244: *
                    245: *    PURPOSE:   Receives datagrams.
                    246: *
                    247: *    ARGUMENTS:        SOCKET   socket to receive on
                    248: *
                    249: *       RETURNS:   0 if ok
                    250: *               1 if error
                    251: *
                    252: *\***************************************************************************/
                    253: int dg_recv(SOCKET s)
                    254: {
                    255:     int rc, errflag = 0;
                    256:     int addrlen = 16;
                    257:     UINT dgrams = 0;
                    258:     LPSTR recvbuf;
                    259: 
                    260: 
                    261:     if (verbose)
                    262:         printf("allocating %d bytes for receive buffer\n", Receive_Length);
                    263: 
                    264:     /*
                    265:     *   Set up the data buffer to send 
                    266:     */
                    267: 
                    268:     recvbuf = (LPSTR)malloc(Send_Length);
                    269: 
                    270:     if (!recvbuf) {
                    271:         printf("Error allocating %d bytes for receive buffer\n", Receive_Length);
                    272:         return 1;
                    273:     }
                    274: 
                    275:     if (verbose) {
                    276:         printf("calling recvfrom(socket = %d, length = %d),\n", s, Receive_Length);
                    277:     }
                    278:     else
                    279:         printf("Waiting for call...\n");
                    280: 
                    281:     while (1) {
                    282:         /*
                    283:         *   Receive a packet from anyone 
                    284:         */
                    285: 
                    286:         rc = recvfrom(s, recvbuf, Receive_Length, 0, (struct sockaddr *)&raddr, &addrlen);
                    287: 
                    288:         if (rc == SOCKET_ERROR) {
                    289:             dos_net_perror("recvfrom() failed");
                    290:             errflag++;
                    291:             break;
                    292:         }
                    293: 
                    294:         if (verbose) {
                    295:             printf("Received %d bytes from \n  ", rc);
                    296:             print_saddr(&raddr);
                    297:             printf("\n");
                    298:         }
                    299:         else
                    300:             printf("\rReceived packet %d, length = %d bytes", ++dgrams, rc);
                    301: 
                    302: 
                    303:         if (No_Loop)
                    304:             break;
                    305:     }
                    306: 
                    307:     if (verbose)
                    308:         printf("Freeing receive buffer\n");
                    309: 
                    310:     free(recvbuf);
                    311: 
                    312:     return errflag;
                    313: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.