-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
90 lines (86 loc) · 2.29 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "secrets.h"
#include "client.h"
#include "CopyClipboard.h"
short clientSocketCreate(void)
{
short hSocket;
printf("Create the socket\n");
hSocket = socket(AF_INET,SOCK_STREAM,0);
return hSocket;
}
int clientSocketConnect(int hSocket,int target)
{
//1 is tower, 2 is mac, 3 is new laptop default to self for testing
int iRetval=-1;
int ServerPort = SERVERPORT;
struct sockaddr_in remote= {0};
if(target==1)
{
remote.sin_addr.s_addr = inet_addr(TDSIP);
remote.sin_family=AF_INET;
remote.sin_port = htons(ServerPort);
}
else if (target==2)
{
remote.sin_addr.s_addr = inet_addr(MACIP);
remote.sin_family=AF_INET;
remote.sin_port = htons(ServerPort);
}
else if (target==3)
{
remote.sin_addr.s_addr = inet_addr(TLSIP);
remote.sin_family=AF_INET;
remote.sin_port = htons(ServerPort);
}
else
{
remote.sin_addr.s_addr = inet_addr("198.168.1.13");
remote.sin_family=AF_INET;
remote.sin_port = htons(ServerPort);
}
iRetval = connect(hSocket, (struct sockaddr *)&remote,sizeof(struct sockaddr_in));
return iRetval;
}
int clientSocketSend(int hSocket, char* Rqst, short lenRqst)
{
int shortRetval = -1;
struct timeval tv;
tv.tv_sec = 20;
tv.tv_usec =0;
if(setsockopt(hSocket,SOL_SOCKET,SO_SNDTIMEO, (char *)&tv, sizeof(tv))<0)
{
printf("Time out\n");
return -1;
}
shortRetval=send(hSocket, Rqst, lenRqst, 0);
return shortRetval;
}
void *sendToClient(int selection)
{
int hSocket=0, read_size=9;
struct sockaddr_in server;
char server_reply[200] = {0};
hSocket = clientSocketCreate();
if(hSocket ==-1)
{
printf("Could not create socket\n");
return NULL;
}
printf("Socket is created\n");
if(clientSocketConnect(hSocket,selection)<0)
{
perror("connect failed.\n");
return NULL;
}
char *sendToServer = copyFromClipboard();
clientSocketSend(hSocket,sendToServer, strlen(sendToServer));
close(hSocket);
printf("Successfully transferred clipboard contents\n\n");
return NULL;
}