-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcliant.cpp
More file actions
84 lines (79 loc) · 1.97 KB
/
cliant.cpp
File metadata and controls
84 lines (79 loc) · 1.97 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
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define llint long long int
void DieWithError(string messege)
{
cerr << messege << endl;
exit(1);
}
void commun(int sock)
{
string cmd;
cout << "0:引き出し 1:預入 2:残高照会 そのた:終了";
cout << " 何をしますか?";
cin >> cmd;
llint hikigaku;
llint aaa=0;
llint azugaku;
switch (cmd[0])
{
case '0':
//引き出し処理
cout << "いくら" << endl;
cin >> hikigaku;
if (hikigaku <= 0){cout << "金額エラー" << endl;}
hikigaku*=-1;
if(send(sock,&hikigaku,8,0)!=8){DieWithError("send() missed");}
break;
case '1':
//預入処理
cout << "いくら" << endl;
cin >> azugaku;
if (azugaku <= 0){cout << "金額エラー" << endl;}
if(send(sock,&azugaku,8,0)!=8){DieWithError("send() missed");}
break;
case '2':
//残高紹介
if(send(sock,&aaa,8,0)!=8){DieWithError("send() missed");}
break;
default:
//エラー
cerr<<"error"<<endl;
}
llint zandaka;
recv(sock,&zandaka,8,0);
// 表示処理
printf("残高は%lld円になりました", zandaka);
}
int prepare_client_socket(char *ipaddr, int port) {
// ソケット生成
int sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0)
DieWithError("socket() failed");
// サーバの情報を設定
struct sockaddr_in target;
target.sin_family = AF_INET;
target.sin_addr.s_addr = inet_addr(ipaddr);
target.sin_port = htons(port);
// サーバへ接続
if (connect(sock, (struct sockaddr*)&target, sizeof(target)) < 0)
DieWithError("connect() failed");
return sock;
}
int main(int argc,char **argv)
{
if (argc < 3)
{
DieWithError("usage: ./cliant ip_address port");
}
int sock = prepare_client_socket(argv[1], atoi(argv[2]));
commun(sock);
}
// 10.13.64.26