|
|
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 using SPX connect. ! 8: * ! 9: ****************************************************************************/ ! 10: #include <windows.h> ! 11: #include <winsock.h> ! 12: #include <stdio.h> ! 13: #include <stdlib.h> ! 14: #include <string.h> ! 15: #include <malloc.h> ! 16: #include <wsipx.h> ! 17: #include "../testlib/testlib.h" ! 18: ! 19: SOCKADDR_IPX addr; ! 20: SOCKADDR_IPX baddr; ! 21: SOCKADDR_IPX caddr; ! 22: ! 23: UCHAR ch = 0; ! 24: ! 25: /* ! 26: * Function Prototypes ! 27: */ ! 28: ! 29: extern int main(int, char **); ! 30: extern int net_init(SOCKET *); ! 31: extern int make_connection(SOCKET); ! 32: extern int do_send_receive(SOCKET); ! 33: ! 34: /**************************************************************************** ! 35: * ! 36: * FUNCTION: main( int argc, char **argv ) ! 37: * ! 38: * PURPOSE: This is the main entry for the program ! 39: * ! 40: * ! 41: * ARGUMENTS: argc = Number of arguments ! 42: * argv = Array of ptrs to cmd line args ! 43: * ! 44: * ! 45: * RETURNS: Exit code for the program ! 46: * ! 47: *\***************************************************************************/ ! 48: int main(int argc, char **argv) ! 49: { ! 50: SOCKET s; ! 51: ! 52: /* ! 53: * Set up our default values before checking command line ! 54: */ ! 55: ! 56: memcpy(Remote_Node_Number, "\x08\x00\x2B\x2E\x98\xA8", 6); ! 57: memcpy(Remote_Socket_Number, "\x05\x00", 2); ! 58: Socket_Type = SOCK_STREAM; ! 59: Protocol = NSPROTO_SPX; ! 60: Remote_Address_Family = AF_NS; ! 61: Sleep_Time = 250; ! 62: ! 63: /* ! 64: * Get any command line options ! 65: */ ! 66: ! 67: parse_cmd_line(argc, argv); ! 68: ! 69: /* ! 70: * Initialize the network and set up the socket ! 71: */ ! 72: ! 73: if (net_init(&s)) ! 74: return 1; ! 75: ! 76: /* ! 77: * Try to connect to our server ! 78: */ ! 79: ! 80: if (make_connection(s)) ! 81: return 1; ! 82: ! 83: /* ! 84: * Send/receive data to/from server ! 85: */ ! 86: ! 87: do_send_receive(s); ! 88: ! 89: /* ! 90: * All done - close up ! 91: */ ! 92: ! 93: if (verbose) ! 94: printf("calling closesocket(socket = %d)\n", s); ! 95: ! 96: closesocket(s); ! 97: return 0; ! 98: } ! 99: ! 100: /**************************************************************************** ! 101: * ! 102: * FUNCTION: net_init( SOCKET *skt ) ! 103: * ! 104: * PURPOSE: Initializes the WinSock stuff and sets up our socket. ! 105: * ! 106: * ! 107: * ARGUMENTS: SOCKET * => struct to fill in with opened socket. ! 108: * ! 109: * RETURNS: 0 if ok ! 110: * 1 if error ! 111: * ! 112: *\***************************************************************************/ ! 113: int net_init(SOCKET *skt) ! 114: { ! 115: SOCKET s; ! 116: int rc, addrlen; ! 117: WSADATA wsdata; ! 118: WORD wVersionRequested; ! 119: ! 120: wVersionRequested = MAKEWORD(1,1); ! 121: ! 122: ! 123: /* ! 124: * Register with the socket library ! 125: */ ! 126: ! 127: rc = WSAStartup(wVersionRequested, &wsdata); ! 128: ! 129: if (verbose) ! 130: printf("WSAStartup returned 0x%X\n", rc); ! 131: ! 132: if (rc) { ! 133: printf("WSAStartup failed: error = %d\n", rc); ! 134: return 1; ! 135: } ! 136: ! 137: if (verbose) { ! 138: printf("contents of wsdata structure: \n"); ! 139: print_wsa(&wsdata); ! 140: printf("calling socket(address family = %d, socket type = %d, protocol = %d)\n", Local_Address_Family, Socket_Type, Protocol); ! 141: } ! 142: ! 143: /* ! 144: * Open a STREAMING socket ! 145: */ ! 146: ! 147: s = socket(Local_Address_Family, Socket_Type, Protocol); ! 148: ! 149: if (verbose) ! 150: printf("socket() returned 0x%X (%d)\n", s, s); ! 151: ! 152: if (s == INVALID_SOCKET) { ! 153: dos_net_perror("socket call failed"); ! 154: return 1; ! 155: } ! 156: ! 157: /* ! 158: * Bind to any address ! 159: */ ! 160: ! 161: addr.sa_family = Local_Address_Family; ! 162: memcpy(&addr.sa_netnum, Local_Network_Number, 4); ! 163: memcpy(&addr.sa_nodenum, Local_Node_Number, 6); ! 164: memcpy(&addr.sa_socket, Local_Socket_Number, 2); ! 165: ! 166: rc = bind(s, (const struct sockaddr FAR *)&addr, 16); ! 167: ! 168: if (verbose) ! 169: printf("bind() returned 0x%X\n", rc); ! 170: ! 171: if (rc == SOCKET_ERROR) { ! 172: dos_net_perror("bind call failed"); ! 173: return 1; ! 174: } ! 175: ! 176: if (verbose) ! 177: printf("calling getsockname(socket = %d)\n", s); ! 178: ! 179: addrlen = 16; ! 180: rc = getsockname(s, (struct sockaddr *) &addr, &addrlen); ! 181: ! 182: if (verbose) ! 183: printf("getsockname() returned 0x%X\n", rc); ! 184: ! 185: if (rc == SOCKET_ERROR) { ! 186: dos_net_perror("Error binding to socket"); ! 187: closesocket(s); ! 188: return 1; ! 189: } ! 190: ! 191: if (verbose) { ! 192: printf("addrlen = %d\n", addrlen); ! 193: print_netaddr(addr.sa_netnum, "Bound address = ", "\n"); ! 194: } ! 195: ! 196: ! 197: /* ! 198: * Build the address of the node to connect to ! 199: */ ! 200: ! 201: memcpy(&caddr.sa_netnum, Remote_Network_Number, 4); ! 202: memcpy(&caddr.sa_nodenum, Remote_Node_Number, 6); ! 203: memcpy(&caddr.sa_socket, Remote_Socket_Number, 2); ! 204: caddr.sa_family = AF_NS; ! 205: ! 206: /* ! 207: * Set up socket for return ! 208: */ ! 209: ! 210: *skt = s; ! 211: ! 212: return 0; ! 213: } ! 214: ! 215: /**************************************************************************** ! 216: * ! 217: * FUNCTION: make_connection( SOCKET s ) ! 218: * ! 219: * PURPOSE: Establishes a connection with our server. ! 220: * ! 221: * ARGUMENTS: SOCKET socket to use for connection ! 222: * ! 223: * RETURNS: 0 if ok ! 224: * 1 if error ! 225: * ! 226: *\***************************************************************************/ ! 227: int make_connection(SOCKET s) ! 228: { ! 229: int rc, addrlen; ! 230: ! 231: /* ! 232: * Connect ! 233: */ ! 234: ! 235: if (verbose) ! 236: printf("calling connect(socket = %d)\n", s); ! 237: ! 238: rc = connect(s, (const struct sockaddr FAR *)&caddr, 16); ! 239: ! 240: if (verbose) ! 241: printf("connect() returned 0x%X\n", rc); ! 242: ! 243: if (rc == SOCKET_ERROR) { ! 244: dos_net_perror("connect call failed"); ! 245: return 1; ! 246: } ! 247: ! 248: printf("Connect OK\n"); ! 249: ! 250: /* ! 251: * Print out address we connected to ! 252: */ ! 253: ! 254: if (verbose) ! 255: printf("calling getpeername(socket = %d)\n", s); ! 256: ! 257: addrlen = 16; ! 258: rc = getpeername(s, (struct sockaddr *) &caddr, &addrlen); ! 259: ! 260: if (verbose) ! 261: printf("getpeername() returned 0x%X\n", rc); ! 262: ! 263: if (verbose) { ! 264: printf("addrlen = %d\n", addrlen); ! 265: print_netaddr(caddr.sa_netnum, "Remote Address = ", "\n"); ! 266: } ! 267: ! 268: return 0; ! 269: } ! 270: /**************************************************************************** ! 271: * ! 272: * FUNCTION: do_send_receive( SOCKET *s ) ! 273: * ! 274: * PURPOSE: Alternately sends/receives data to/from the server. ! 275: * ! 276: * ARGUMENTS: SOCKET socket to transmit on ! 277: * ! 278: * RETURNS: 0 if ok ! 279: * 1 if error ! 280: * ! 281: *\***************************************************************************/ ! 282: int do_send_receive(SOCKET s) ! 283: { ! 284: int rc, errflag = 0; ! 285: int sndpkts = 0, rcvpkts = 0; ! 286: LPSTR sendbuf; ! 287: ! 288: /* ! 289: * Allocate a send buffer ! 290: */ ! 291: ! 292: if (verbose) ! 293: printf("Allocating %d bytes for send buffer \n"); ! 294: ! 295: sendbuf = malloc(Send_Length); ! 296: ! 297: if (!sendbuf) { ! 298: printf("Error allocating %d bytes for send buffer\n"); ! 299: return 1; ! 300: } ! 301: ! 302: /* ! 303: * Send data and recv it back ! 304: */ ! 305: ! 306: while (1) { ! 307: ! 308: /* ! 309: * Fill the buffer with our current character ! 310: */ ! 311: ! 312: memset(sendbuf, ch, Send_Length); ! 313: ! 314: /* ! 315: * Send data ! 316: */ ! 317: ! 318: if (verbose) ! 319: printf("calling send(socket = %d, length = %d)\n", s, Send_Length); ! 320: ! 321: rc = send(s, sendbuf, Send_Length, 0); ! 322: ! 323: if (verbose) ! 324: printf("send() returned 0x%X\n", rc); ! 325: ! 326: if (rc == SOCKET_ERROR) { ! 327: dos_net_perror("\nsend() call failed"); ! 328: errflag++; ! 329: break; ! 330: } ! 331: ! 332: if (verbose) ! 333: printf("Sent packet %d: sent %d bytes\n", sndpkts++, rc); ! 334: else ! 335: printf("\rSent packet %d: sent %d bytes... ", sndpkts++, rc); ! 336: ! 337: /* ! 338: * Receive the data back ! 339: */ ! 340: ! 341: if (verbose) ! 342: printf("calling recv(socket = %d, length = %d)\n", s, Send_Length); ! 343: ! 344: rc = recv(s, sendbuf, Send_Length, 0); ! 345: ! 346: if (verbose) ! 347: printf("recv() returned %d\n", rc); ! 348: ! 349: if (rc == SOCKET_ERROR) { ! 350: dos_net_perror("\nrecv() call failed"); ! 351: errflag++; ! 352: break; ! 353: } ! 354: ! 355: if (!rc) { ! 356: printf("connection has been lost\n"); ! 357: break; ! 358: } ! 359: ! 360: printf("Received %d bytes", rc); ! 361: ! 362: if (verbose) ! 363: printf("\n"); ! 364: ! 365: /* ! 366: * See if our buffer has the same data in it ! 367: */ ! 368: ! 369: rc = mem_check(sendbuf, ch++, Send_Length); ! 370: ! 371: if (rc) ! 372: printf("Data compare error: packet = %d, offset = %d\n", (sndpkts-1), rc); ! 373: else if (verbose) ! 374: printf("Data compares okay\n"); ! 375: ! 376: /* ! 377: * If we are to send just 1, break out ! 378: */ ! 379: ! 380: if (No_Loop) ! 381: break; ! 382: ! 383: /* ! 384: * Pause a little while (don't trash the network) ! 385: */ ! 386: ! 387: Sleep(Sleep_Time); ! 388: } ! 389: ! 390: if (verbose) ! 391: printf("\nFreeing send buffer\n"); ! 392: ! 393: free(sendbuf); ! 394: ! 395: return errflag; ! 396: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.