-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapleclient.c
More file actions
executable file
·97 lines (95 loc) · 2.16 KB
/
mapleclient.c
File metadata and controls
executable file
·97 lines (95 loc) · 2.16 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
97
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <sys/uio.h>
#include <unistd.h>
#define BUF_LEN 1024
#define COMM_LEN 4096
int main(int argc, char **argv){
int s;
struct hostent *servhost;
struct sockaddr_in server;
char hostname[BUF_LEN] = "localhost";
int port = 0;
char com[COMM_LEN];
int com_len;
int con_stat;
char buf[BUF_LEN];
int read_size;
char c;
int i = 0;
if(argc == 3){
sscanf(argv[1],"%s",hostname);
sscanf(argv[2],"%d",&port);
while(scanf("%c",&c) != EOF){
if(c != '\n'){
com[i] = c;
i++;
}
}
com[i] = '\0';
//printf(":%s:",com);
com_len = strlen(com);
servhost = gethostbyname(hostname);
bzero((char *)&server,sizeof(server));
server.sin_family = AF_INET;
bcopy(servhost->h_addr,(char *)&server.sin_addr,servhost->h_length);
server.sin_port = htons(port);
if((s = socket(AF_INET,SOCK_STREAM,0)) < 0){
printf("failed : generating socket.\n");
exit(1);
}
if((con_stat = connect(s,(struct sockaddr *)&server,sizeof(server))) == -1){
printf("failed : connect().\n");
exit(1);
}
write(s,com,com_len);
while(1){
read_size = read(s,buf,BUF_LEN);
if(read_size > 0){
write(1,buf,read_size);
}else{
break;
}
}
close(s);
}else if(argc == 4){
sscanf(argv[1],"%s",hostname);
sscanf(argv[2],"%d",&port);
sscanf(argv[3],"%s",com);
//printf(":%s:",com);
com_len = strlen(com);
servhost = gethostbyname(hostname);
bzero((char *)&server,sizeof(server));
server.sin_family = AF_INET;
bcopy(servhost->h_addr,(char *)&server.sin_addr,servhost->h_length);
server.sin_port = htons(port);
if((s = socket(AF_INET,SOCK_STREAM,0)) < 0){
printf("failed : generating socket.\n");
exit(1);
}
if((con_stat = connect(s,(struct sockaddr *)&server,sizeof(server))) == -1){
printf("failed : connect().\n");
exit(1);
}
write(s,com,com_len);
while(1){
read_size = read(s,buf,BUF_LEN);
if(read_size > 0){
write(1,buf,read_size);
}else{
break;
}
}
close(s);
}else{
printf("number of args: 4.\n");
exit(1);
}
return(0);
}