From: bluecow+@pitt.edu (Tobish E Smith)
Newsgroups: comp.sys.mac.programmer
Subject: MacTCP programming help needed
Date: 23 Jul 92 15:34:23 GMT
Organization: University of Pittsburgh

I'm taking a first stab at MacTCP programming, and I'm at my
first impasse.  With the code I've included below, I'm able to
successfully establish a connection with another machine (I can
do a netstat command while this program is running and see the
new socket connection, and it disappears when this program quits).
However, in the little loop below where I attempt to read data
from this connection, I just get garbage.  This is driving me
batty.  Please help me out if you can.  E-mail responses are
appreciated since I have about 600 unread messages in this
group...

Thanks,
Tob
bluecow@unix.cis.pitt.edu


P.S.  Does anyone know what constants to use with the serial
      driver for 14400 and 38400 baud?

P.P.S.  I'm using THINK C 5.0.2.


---------  My program snippet --------------

#define kBufSize 16384
#define kTelnetPort 23
#define kTimeOut 20

unsigned long stream, ipAddress;
long myWait;
OSErr err;
int count;
Str255 theStr;

  if (OpenTCPDriver() != noErr)
    ExitToShell();
        
  err = ConvertStringToAddr("goofy",&ipAddress);
  if (err!=noErr)
    return err;

  err = CreateStream(&stream,kBufSize);
  if (err!=noErr)
    return err;
        
  err = OpenConnection(stream,ipAddress,kTelnetPort,kTimeOut);
  if (err!=noErr) 
    return err;
        
  while (TRUE)
    {
      err = MyTCPStatus(stream,&mywait);
      if (err!=noErr)
        return err;
                
      if (mywait > 0)
        {
          count = (int)mywait;
          j = MyTCPGetChars(stream, count, (Ptr)theStr);
          PrintItOut(theStr);
	}
    }



--------- Relevant Routines -------------

OSErr OpenTCPDriver()
{
  OSErr   err;
        
  err = OpenDriver("\p.IPP",&refNum);
  return(err);
}


OSErr NewBlock(TCPiopb **pBlock)
{
  *pBlock = (TCPiopb *)NewPtr(sizeof(TCPiopb));
  if (MemError() != noErr)
    return MemError();
  (*pBlock)->ioCompletion = 0L;
  (*pBlock)->ioCRefNum = refNum;
  return noErr;
}


OSErr LowTCPCreateStream(StreamPtr *streamPtr,
                         Ptr connectionBuffer,
                         unsigned long connBufferLen,
                         TCPNotifyProc notifPtr)
{
  OSErr err;
  TCPiopb *pBlock;
        
  if ((err = NewBlock(&pBlock)) != noErr)
    return err;
                
  pBlock->csCode = TCPCreate;
  pBlock->ioResult = 1;
  pBlock->csParam.create.rcvBuff = connectionBuffer;
  pBlock->csParam.create.rcvBuffLen = connBufferLen;
  pBlock->csParam.create.notifyProc = notifPtr;
  PBControl((ParmBlkPtr)pBlock,true);
  while (pBlock->ioResult > 0 && GiveTime(cSleepTime))
    ;
  if (gCancel)
    return -1;
                
  *streamPtr = pBlock->tcpStream;
  err = pBlock->ioResult;
  DisposPtr((Ptr)pBlock);
  return err;
}


OSErr LowTCPOpenConnection(StreamPtr streamPtr,
                           byte timeout,
                           ip_addr remoteHost,
                           tcp_port remotePort,
                           ip_addr *localHost,
                           tcp_port *localPort)
{
  OSErr err;
  TCPiopb *pBlock;
        
  if ((err = NewBlock(&pBlock)) != noErr)
    return err;
        
  pBlock->csCode = TCPActiveOpen;
  pBlock->ioResult = 1;
  pBlock->tcpStream = streamPtr;
  pBlock->csParam.open.ulpTimeoutValue = timeout;
  pBlock->csParam.open.ulpTimeoutAction = 1;
  pBlock->csParam.open.validityFlags = 0xC0;
  pBlock->csParam.open.commandTimeoutValue = timeout;
  pBlock->csParam.open.remoteHost = remoteHost;
  pBlock->csParam.open.remotePort = remotePort;
  pBlock->csParam.open.localPort = *localPort;
  pBlock->csParam.open.tosFlags = 0;
  pBlock->csParam.open.precedence = 0;
  pBlock->csParam.open.dontFrag = 0;
  pBlock->csParam.open.timeToLive = 0;
  pBlock->csParam.open.security = 0;
  pBlock->csParam.open.optionCnt = 0;
  PBControl((ParmBlkPtr)pBlock,true);
  while (pBlock->ioResult > 0 && GiveTime(cSleepTime))
    ;
  if (gCancel)
  return -1;
  *localHost = pBlock->csParam.open.localHost;
  *localPort = pBlock->csParam.open.localPort;
  err = pBlock->ioResult;
  DisposPtr((Ptr)pBlock);
  return err;
}


OSErr CreateStream(unsigned long *stream,unsigned long recvLen)
{
  Ptr recvPtr;
  OSErr err;
        
  recvPtr = NewPtr(recvLen);
  err = MemError();
  if (err==noErr)
    err = LowTCPCreateStream(stream,
                             recvPtr,
                             recvLen,
                             (TCPNotifyProc)nil);
  return err;
}


OSErr OpenConnection(unsigned long stream,
                     long remoteHost,
                     short remotePort,
                     Byte timeout)
{
  ip_addr localHost;
  tcp_port localPort = 0;
        
  return LowTCPOpenConnection(stream,
                              timeout,
                              remoteHost,
                              remotePort, 
                              &localHost,
                              &localPort);
}


OSErr MyTCPStatus(StreamPtr streamPtr,long *waiting)
{
  OSErr err;
  TCPiopb *pBlock;
        
  if ((err = NewBlock(&pBlock)) != noErr)
  return err;
        
  pBlock->csCode = TCPStatus;
  pBlock->ioResult = 1;
  pBlock->tcpStream = streamPtr;
  PBControl((ParmBlkPtr)pBlock,true);
  while (pBlock->ioResult > 0 && GiveTime(cSleepTime))
    ;

  if (gCancel)
  return -1;
  *waiting = pBlock->csParam.status.amtUnreadData;
  err = pBlock->ioResult;
  DisposPtr((Ptr)pBlock);
  return err;
}


int MyTCPGetChars(StreamPtr streamPtr, int readCount, Ptr returnPtr) 
{
  OSErr err;
  TCPiopb *pBlock;
        
  if ((err = NewBlock(&pBlock)) != noErr)
    return err;
        
  pBlock->csCode = TCPRcv;
  pBlock->ioResult = 1;
  pBlock->tcpStream = streamPtr;
  pBlock->csParam.receive.rcvBuff = returnPtr;
  pBlock->csParam.receive.rcvBuffLen = readCount;
  PBControl((ParmBlkPtr)pBlock,true);
        
  return(pBlock->csParam.receive.rcvBuffLen);
}
Xref: sparky comp.sys.mac.programmer:13195 comp.sources.wanted:3793