Skip to content

Instantly share code, notes, and snippets.

@rbtylee
Created February 24, 2019 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbtylee/e32ffab5acd929f3966528c3eebd3636 to your computer and use it in GitHub Desktop.
Save rbtylee/e32ffab5acd929f3966528c3eebd3636 to your computer and use it in GitHub Desktop.
/* This sample code is a client for clipster
* illustrating how to use sockets to 'talk' to another program
*
* Copyright 2019 ylee@bodhilinux.com
*
* This work is free. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License, Version 2,
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
*
* See https://github.com/mrichar1/clipster
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h> // for close
/* Utility fn to make life easy */
#define IF_FAIL_BREAK(x) if (x < 0){perror("socket() failed"); break;}
/* Constants used by this program */
#define SERVER_PATH "/home/ylee/.local/share/clipster/clipster_sock"
#define BUFFER_LENGTH 1024
#define FALSE 0
void
main()
{
int sd = -1, rc;
unsigned char buffer[BUFFER_LENGTH];
struct sockaddr_un serveraddr;
do
{
sd = socket(AF_UNIX, SOCK_STREAM, 0);
IF_FAIL_BREAK(sd);
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX;
strcpy(serveraddr.sun_path, SERVER_PATH);
/* establish a connection to the server. */
rc = connect(sd, (struct sockaddr *) &serveraddr, SUN_LEN(&serveraddr));
IF_FAIL_BREAK(rc);
/* Send clipboard data */
sprintf(buffer, "SEND:CLIPBOARD:0");
rc = send(sd, buffer, strlen(buffer), 0);
IF_FAIL_BREAK(rc);
sprintf(buffer,":");
rc = send(sd, buffer, strlen(buffer), 0);
IF_FAIL_BREAK(rc);
sprintf(buffer, "THIS IS A TEST");
rc = send(sd, buffer, strlen(buffer), 0);
IF_FAIL_BREAK(rc);
} while (FALSE);
/* Close down any open socket descriptors */
if (sd != -1)
close(sd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment