-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
112 lines (97 loc) · 3.02 KB
/
server.cpp
File metadata and controls
112 lines (97 loc) · 3.02 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
#define llint long long int
const int BUF_SIZE = 1024;
void DieWithError(string messege)
{cerr << messege << endl;exit(1);}
string read_until_delim(int sock, char dolimiter)
{
string ans;
static char buf[BUF_SIZE];
int len_r = 0;
int index_letter = 0;
while (-1)
{
if(index_letter>=BUF_SIZE){
printf("too long recieve");
return string();
}
if ((len_r = recv(sock, buf + index_letter, 1, 0)) <= 0)
{
printf("接続が切れました");
return string();
}
if (buf[index_letter] == dolimiter){break;}
else{index_letter++;}
ans.push_back(buf[index_letter]);
}
return ans;
}
llint nowbalance;
llint get_current_balance(){return nowbalance;}
void set_current_balance(llint new_balance){nowbalance=new_balance;return;}
// 待ち受け用ソケットの生成
int prepare_server_socket(int port)
{
// ソケット生成
int servSock = socket(PF_INET, SOCK_STREAM, 0);
if (servSock < 0)
DieWithError("socket() failed");
// サーバの情報を設定
struct sockaddr_in servAddress;
servAddress.sin_family = AF_INET;
servAddress.sin_addr.s_addr = htonl(INADDR_ANY);
servAddress.sin_port = htons(port);
// ソケットにサーバの情報を登録
bind(servSock, (struct sockaddr *)&servAddress, sizeof(servAddress));
return servSock;
}
int commun(int sock){
char buf[BUF_SIZE]; // 通信用バッファ
llint balance = get_current_balance(); // 預金残高
/*
// 区切り文字が出るまでソケットから受信→預け入れ金額をプラス
string azuke=read_until_delim(sock, '_');
balance += std::stoll(azuke);
// 区切り文字が出るまでソケットから受信→引き出し金額をマイナス
string hiki=read_until_delim(sock, '_');
balance -= stol(hiki);
*/
llint kingaku;
recv(sock,&kingaku,8,0);
// データベースの預金残高を更新
balance+=kingaku;
set_current_balance(balance);
if(send(sock,&balance,8,0)!=8){DieWithError("send() missed");}
// クライアントへ残高を送信
}
int main(int argc, char *argv[])
{
nowbalance=10000;
struct sockaddr_in cliantAddress;
int szClientAddr;
int clisock;
int servSock = prepare_server_socket(1001);
listen(servSock, 5);
while (1)
{
szClientAddr = sizeof(cliantAddress);
// 接続要求受け入れ
clisock = accept(servSock, (struct sockaddr *)&cliantAddress, &szClientAddr);
// クライアントとの通信
commun(clisock);
// 通信終了
close(clisock);
}
// 待ち受け用ソケットを閉じる
close(servSock);
return 0;
}