-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_client.h
89 lines (75 loc) · 1.72 KB
/
my_client.h
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
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
int connection()
{
int sock = -1;
struct sockaddr_in address;
struct hostent * host;
int len;
/* create socket */
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock <= 0)
{
printf("cannot create socket\n");
return -1;
}
/* connect to server */
address.sin_family = AF_INET;
address.sin_port = htons(5001);
host = gethostbyname("localhost");
if (!host)
{
printf("error: unknown host\n");
return -1;
}
memcpy(&address.sin_addr, host->h_addr_list[0], host->h_length);
if (connect(sock, (struct sockaddr *)&address, sizeof(address)))
{
printf("error: cannot connect to host\n");
return -1;
}
printf("connection created\n");
return sock;
}
double extract_prob(char str[], double *prob, int sock, int number)
{
char sendBuff[50];
switch (number)
{
case 1:
strcpy(sendBuff,"mode unigram");
break;
case 2:
strcpy(sendBuff,"mode bigram");
break;
case 3:
strcpy(sendBuff,"mode trigram");
break;
case 4:
strcpy(sendBuff,"mode quadragram");
break;
default:
printf("WRONG NUMBER\n");
break;
}
int len=strlen(sendBuff);
write(sock,sendBuff,len);
char buffer[1024]="NULL";
sleep(2);
len=strlen(str);
//write(sock,&len,sizeof(int));
write(sock,str,len);
read(sock,buffer,1024);
double probability = atof(buffer);
//printf("PROBABILITY RECEIVED !!! : %lf\n",probability);
return probability;
}
void close_socket(int sock)
{
close(sock);
}