[comp.lang.c] AppleTalk / C Programming Problem

peter@boulder.Colorado.EDU (Peter A. Steinauer) (01/10/91)

Ok everyone, I have a problem that I'm hoping someone can help me out with...
	I'm trying to write a simple program which will compose a packet 
	and send it from one socket to another on the same machine (or to
	a socket on a different machine)... I'm able to open both the source
	and desination sockets but when I try to compose and send the packet
	DDPWrite returns a -91 in errCode which is supposed to say that the 
	source socket isn't open... I don't understand... Could some one 
	take a look at the following code and let me know what I'm missing.
	I would really appreciate it...

		Pete Steinauer
			University of Colorado Boulder

**********


/*
 * Simple appletalk tester... This program should open up two ddp sockets
 *	one which it will use to send a packet from and another one to send
 * 	a packet to.
 */

#include <MemoryMgr.h>
#include <AppleTalk.h>
#include <nAppleTalk.h>
#include <stdio.h>
#include <string.h>

#define	FALSE	0
#define TRUE	1

ABRecHandle		myABRecHandle;
ABRecPtr		myABRecPtr;
ABusRecord		myABRecord;
unsigned char		mySocketsource, mySocketdest;
char			myBuffer[600];
char			someText[255] = "This is a sample datagram";
int 			errCode, dataLen, index;
unsigned int		async, retCksumErrs, doChecksum;

main()
{
	int	myNode, myNet;

/* Open up AppleTalk */	
errCode = MPPOpen();

/* if there was an opening error then print an error... Otherwise go on to other work */ 
if (errCode != noErr)
	printf("Error in opening AppleTalk\n");
  else
  	{
	/* Get the node number and net number */
  	errCode = GetNodeAddress(&myNode, &myNet);
  	if (errCode != noErr)
  		printf("Error in getting the node address \n");
  	else
  		printf("Node Number: %d   Net Number: %d\n", myNode, myNet);
  		
	/* Open up necessary sockets */
  	mySocketsource = 77;
  	mySocketdest = 78;
  	errCode = DDPOpenSocket(&mySocketsource, NULL);
  	printf("Source Socket number is %d\n",mySocketsource);
  	printf("errCode is %d\n",errCode);
  	errCode = DDPOpenSocket(&mySocketdest, NULL);
  	printf("Destination Socket number is %d\n",mySocketdest);
  	printf("errCode is %d\n",errCode);
  
  	if (errCode != noErr)
  		printf("Error opening socket...\n");
  	else
  		{
		/* Prepare packet to be sent */

  		myABRecPtr = &myABRecord;
  		myABRecHandle = & myABRecPtr;
  	
  		dataLen = strlen(someText);
  		strcpy(myBuffer, someText);
  		
  		async = FALSE;
  		
  		myABRecord.ddpProto.ddpType = 5;
  		myABRecord.ddpProto.ddpSocket = mySocketsource;
  		myABRecord.ddpProto.ddpAddress.aNet=myNet;
  		myABRecord.ddpProto.ddpAddress.aNode=myNode;
  		myABRecord.ddpProto.ddpAddress.aSocket = mySocketdest;
  		myABRecord.ddpProto.ddpReqCount = dataLen;
  		myABRecord.ddpProto.ddpDataPtr = myBuffer;
  		
  		doChecksum = FALSE;
  		
		/* Try to send packet....*/
  		errCode = DDPWrite(myABRecHandle, doChecksum, async);
  		
		/* Print out resultant error code */
  		printf("errCode = %d\n",errCode);
		}
	}
}

Bruce.Hoult@bbs.actrix.gen.nz (01/11/91)

Peter A. Steinauer writes:
>	I'm trying to write a simple program which will compose a packet
>	and send it from one socket to another on the same machine (or to
>	a socket on a different machine)... I'm able to open both the source
>	and desination sockets but when I try to compose and send the packet
>	DDPWrite returns a -91 in errCode which is supposed to say that the 
>	source socket isn't open... I don't understand... Could some one 
>	take a look at the following code and let me know what I'm missing.
>	I would really appreciate it...

Without actually trying to get the code running (OK, Ok, I'm lazy), I see
several possible problems:
 
1) you didn't use PSetSelfSend to allow you to send to yourself (what else?)

2) You need to have an outstanding read on the receiving socket when you do
   the send, otherwise it will miss the packet.


... and now the friendly advice, that you're free to ignore ...

3) It's a waste of time using the ABusRecord interface -- do as IM vol 5
   tells you and use the paramBlock interface.  It's much easier.  Really.

4) Unless you've got a good reason to use DPP (i.e. you're doing something
   *really* clever), don't bother and just use ATP (possibly with an empty
   response packet) instead.  It's easier still -- even I got something
   working using ATP.  This last point may be just prejudice on my part,
   because I confess that I haven't actually tried to get DDP working.
   ATP with the paramBlock interface really *was* easy though...

-- Bruce
-- 
Bruce.Hoult@bbs.actrix.gen.nz   Twisted pair: +64 4 772 116
BIX: brucehoult                 Last Resort:  PO Box 4145 Wellington, NZ

schaefer@telematik.informatik.uni-karlsruhe.de (01/26/91)

If you look at "Inside Macintosh Vol II" Page 283 you come to know
that the destination-socket must not be in the same node as the source-
socket. 
Try out to run your programm on two different Computers.
To do this you'll have to get the node-adress on each computer using
the GetNodeAdress() function (Inside Mac II, Page 303).
Then send from one node to the other one. There is just one problem 
with it : The node address may change, if you restart your programm.
To avoid this complication use the NBP Protocoll to look up the destination-
nodes address or enter the dest-node adress during running time via 
scanf().

(Guenter Schaefer, Karlsruhe)

peirce@outpost.UUCP (Michael Peirce) (01/27/91)

In article <1991Jan26.151519.1@telematik.informatik.uni-karlsruhe.de>, schaefer@telematik.informatik.uni-karlsruhe.de writes:
> 
> If you look at "Inside Macintosh Vol II" Page 283 you come to know
> that the destination-socket must not be in the same node as the source-
> socket. 

Not necessarily true.

There is a call called SetSelfSend that allows AppleTalk to work intranode
in addition to the normal internode.

Many programs and inits turn this on (Public Folder for example turns
on self send).  There's even an init that someone wrote that does
nothing els, but turn self send on.

It's very helpful when debugging network code without having to transfer
the programs around.  (You do have to make sure you are doing things
asynchronously though, or it hangs).

-- michael


--  Michael Peirce         --   outpost!peirce@claris.com
--  Peirce Software        --   Suite 301, 719 Hibiscus Place
--  Macintosh Programming  --   San Jose, California 95117
--           & Consulting  --   (408) 244-6554, AppleLink: PEIRCE