-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient2.c
More file actions
96 lines (75 loc) · 1.61 KB
/
client2.c
File metadata and controls
96 lines (75 loc) · 1.61 KB
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
91
92
93
94
95
96
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#define oops(msg) { perror(msg); exit(1); }
typedef struct info
{
int selection;
char input[BUFSIZ];
};
int sock_id;
void *receive();
int main(int ac, char *av[])
{
struct sockaddr_in servadd;
struct hostent *hp;
pthread_t t;
char str[BUFSIZ];
int num;
if(ac != 3)
{
fprintf(stderr, "usage: %s ip port", av[0]);
exit(1);
}
sock_id = socket(PF_INET, SOCK_STREAM, 0);
if(sock_id == -1)
oops("socket");
memset(&servadd, 0, sizeof(servadd));
servadd.sin_family = AF_INET;
servadd.sin_addr.s_addr = inet_addr(av[1]); // ip
servadd.sin_port = htons(atoi(av[2])); // portnum
if(connect(sock_id, (struct sockaddr *)&servadd, sizeof(servadd)) != 0)
oops("connect");
pthread_create(&t, NULL, receive, NULL);
while(1)
{
scanf("%d",&num);
gets(str);
if(strcmp(str,"break")==0)
break;
sprintf(str,"%d %s",num, str);
sprintf(str,"%s\n",str);
//write
write(sock_id,str,strlen(str)+1);
}
close(sock_id);
return 0;
}
void *receive()
{
int i = 0;
struct info data;
char *temp[3];
while(1)
{
char message[BUFSIZ];
recv(sock_id, message, sizeof(message), 0);
printf("read : %s\n",message);
temp[i] = strtok(message," ");
while(temp[i] != NULL)
{
printf("token %d: %s\n",i,temp[i]);
temp[++i]=strtok(NULL," ");
}
sprintf(data.input,"%s %s",temp[1],temp[2]);
data.selection = atoi(temp[0]);
printf("struct char: %s , selection: %d\n",data.input, data.selection);
i=0;
}
}