Example #1
0
long SDMmessage::SendTCP(long ip_addr,long port)
{
	int sock;
	long i;
	long result;
	char buf[3*BUFSIZE];
	char ack[16];
	short error = 0;

	//fill buffer
	i = Marshal(buf);
	if(i < 0)
		return SDM_MESSAGE_SEND_ERROR;
	//send message
	sock = TCPconnect(ip_addr,port);
  if (sock != IP_SOCK_INVALID)
  {
	  result = TCPsend(sock,buf,i);
	  if(buf[0] == SDM_xTEDS || buf[0] == SDM_CancelxTEDS)
	  {
		  TCPrecv(sock,ack,13);
		  error = GET_SHORT(&ack[HEADER_SIZE]);
		  if(error < 0)
      {
			  result = error;
      }
	  }
	  TCPclose (sock);
#ifdef BUILD_WITH_MESSAGE_LOGGING
	  Logger.MessageSent(*this);
#endif
  }
	return result;
}
Example #2
0
int
TCPecho(char *destination, int portN)
{
	char	buf[LINELEN+1];		/* buffer for one line of text	*/
	int	sock;				/* socket descriptor, read count*/


	int	outchars, inchars;	/* characters sent and received	*/

	if ((sock = clientTCPsock(destination, portN)) < 0)
		errmesg("fail to obtain TCP socket");

	while (fgets(buf, sizeof(buf), stdin)) 
	{
		buf[LINELEN] = '\0';	/* insure line null-terminated	*/
		outchars = strlen(buf);
		(void) write(sock, buf, outchars);

#ifdef DEBUG
		printf("\tTCPecho(%s, %d): sent %d bytes to echod: `%s`\n", 
			   destination, portN, outchars, buf);
#endif /* DEBUG */

		/* read it back */
		inchars = TCPrecv(sock, buf, LINELEN-1, 0);
		if (inchars < 0)
				errmesg("socket read failed\n");
	
		fputs(buf, stdout);
	}

	return 0;
}