-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbankingClient.c
More file actions
169 lines (124 loc) · 3.02 KB
/
bankingClient.c
File metadata and controls
169 lines (124 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
# include <netinet/in.h>
#include <string.h>
#include <pthread.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <assert.h>
#define BUFF 1024
void* readServer(void*);
void* writeServer(void*);
pthread_t threads[2];
int end = 0;
typedef struct sockfd{
int sockNum;
}sockfd;
int main(int argc, char *argv[])
{
//invalid # of arguments
if(argc != 3){
fprintf(stderr,"Must call program with 2 arguments, found %d\n",argc);
return -1;
}
int PORT = 11111;
PORT = atoi(argv[2]);
int sock = 0,conn =0;
struct sockaddr_in serv_addr;
struct hostent * server_ip;
sock = socket(AF_INET, SOCK_STREAM, 0);
server_ip = gethostbyname(argv[1]);
if(server_ip == NULL){
fprintf(stderr,"Requested host not found. Errno set.\n");
return -1;
}
if (sock < 0)
{
printf("\n Socket creation error \n");
return -1;
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
bcopy((char *)server_ip->h_addr,(char*)&serv_addr.sin_addr.s_addr,server_ip->h_length);
conn = connect(sock,(struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (conn < 0)
{
printf("\nConnection Failed \n");
while(conn < 0){
// sleep(3);
conn = connect(sock,(struct sockaddr *)&serv_addr, sizeof(serv_addr));
}
}
else{
printf("connection made\n");
}
int err = 1;
err = pthread_create(&(threads[0]),NULL,&writeServer,(void *)&sock);
if(err != 0){
fprintf(stderr,"Can't make thread to write to server\n");
}
err = pthread_create(&(threads[1]),NULL,&readServer,(void *)&sock);
if(err != 0){
fprintf(stderr,"Can't make thread to read from server\n");
}
pthread_join(threads[0],NULL);
pthread_join(threads[1],NULL);
//int cont = 1;
/*while(cont!=0){
char buffer[BUFF];
char response[BUFF] ;
read(STDIN_FILENO, buffer, 1024);
printf("Send: %s",buffer);
int sent = write(sock, buffer,strlen(buffer));
if(sent < 0){
printf("Error: Message not sent.\n");
}
cont = strcmp(buffer,"quit");
int rec = read(sock,response,BUFF);
if(rec < 0){
printf("Error: No server response\n");
}
printf("Response: %s\n",response);
sleep(3);
}*/
close(sock);
return 0;
}
void* readServer(void* _arg){
struct sockfd* sockDes = (struct sockfd*) _arg;
int sock = sockDes->sockNum;
char buffer[BUFF];
while(end == 0){
memset(buffer,0,sizeof(buffer));
int resp = read(sock,buffer,BUFF);
if(resp < 0){
printf("Error: No server response.\n");
}
else{
printf("Response: %s\n",buffer);
}
if(strcmp(buffer,"quit")==0){
end = 1;
}
}
return 0;
}
void* writeServer(void* _arg){
struct sockfd* sockDes = (struct sockfd*) _arg;
int sock = sockDes->sockNum;
char buffer[BUFF];
while(end == 0){
read(STDIN_FILENO,buffer,BUFF);
printf("Send: %s\n",buffer);
int send = write(sock,buffer,BUFF);
memset(buffer,0,sizeof(buffer));
if(send < 0){
printf("Error: Message Not sent. Try again.\n");
}
sleep(2);
}
return 0;
}