Пример #1
0
int main(int argc, char **argv)
{
	int 					sock;
	char 					string[512];
	size_t					len;

	if (argc != 2 ) {
		printf("i need a bdaddr! (%d)\n", argc);
		return 0;
	}

	const bdaddr_t bdaddr = bdaddrUtils::FromString(argv[1]);

	sock = make_named_socket(&bdaddr);

	while (strcmp(string,"bye")!= 0 )	{

		fscanf(stdin, "%s", string );
	//  len = send(sock, string, strlen(string), 0);
		len = sendto(sock, string, strlen(string) + 1 /*\0*/, 0, (struct sockaddr*) &l2sa, sizeof(struct sockaddr_l2cap) );

		printf("Sent %ld bytes\n",len);
		//recvfrom(sock, buff, 4096-1, 0, (struct sockaddr *)  &l2, &len);
	}

	printf("Transmission done ...\n");
	getchar();
	close(sock);
}
Пример #2
0
int
main (void)
{
  extern int make_named_socket (const char *name);
  int sock;
  char message[MAXMSG];
  struct sockaddr_un name;
  size_t size;
  int nbytes;

  /* Make the socket. */
  sock = make_named_socket (CLIENT);

  /* Initialize the server socket address. */
  name.sun_family = AF_LOCAL;
  strcpy (name.sun_path, SERVER);
  size = strlen (name.sun_path) + sizeof (name.sun_family);

  /* Send the datagram. */
  nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
		   (struct sockaddr *) & name, size);
  if (nbytes < 0)
    {
      perror ("sendto (client)");
      exit (EXIT_FAILURE);
    }

  /* Wait for a reply. */
  nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
  if (nbytes < 0)
    {
      perror ("recfrom (client)");
      exit (EXIT_FAILURE);
    }

  /* Print a diagnostic message. */
  fprintf (stderr, "Client: got message: %s\n", message);

  /* Clean up. */
  remove (CLIENT);
  close (sock);
}
Пример #3
0
int
main (void)
{
  int sock;
  char message[MAXMSG];
  struct sockaddr_un name;
  size_t size;
  int nbytes;

  /* Remove the filename first, it's ok if the call fails */
  unlink (SERVER);

  /* Make the socket, then loop endlessly. */
  sock = make_named_socket (SERVER);
  while (1)
    {
      /* Wait for a datagram. */
      size = sizeof (name);
      nbytes = recvfrom (sock, message, MAXMSG, 0,
			 (struct sockaddr *) & name, &size);
      if (nbytes < 0)
	{
	  perror ("recfrom (server)");
	  exit (EXIT_FAILURE);
	}

      /* Give a diagnostic message. */
      fprintf (stderr, "Server: got message: %s\n", message);

      /* Bounce the message back to the sender. */
      nbytes = sendto (sock, message, nbytes, 0,
		       (struct sockaddr *) & name, size);
      if (nbytes < 0)
	{
	  perror ("sendto (server)");
	  exit (EXIT_FAILURE);
	}
    }
}