-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
143 lines (129 loc) · 3.63 KB
/
Copy pathclient.cpp
File metadata and controls
143 lines (129 loc) · 3.63 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
#include <string.h>
#include <cstring>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <strings.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
//#include <winsock2.h>
using namespace std;
void *ftch(void *);
void *snd(void *);
static int client_socket;
static int get_size;
int main(int argc,char *argv[])
{
int port;
struct sockaddr_in server_address;
struct hostent *server;
pthread_t procThread[3];
//TO CHECK WETHER VALID ARGUMENTS ARE PASSED OR NOT
if(argc<3)
{
cerr<<"Not Enough Arguments:( "<<endl;
return 0;
}
port=atoi(argv[2]);
//IF THE PORT ENTERED IS VALID OR NOT
if(port<1024 || port>65535)
{
cerr<<"Enter the port number in the range (1024 - 65535)";
return 0;
}
//CREATING A SOCKET...
client_socket = socket(AF_INET,SOCK_STREAM,0);
if(client_socket<0)
{
cerr<<"Unable to open socket"<<endl;
return 0;
}
server=gethostbyname(argv[1]); //returns a pointer to the hostent structure containing the host's IP address and other information
if(server==NULL)
{
cerr<<"Unable to find the host"<<endl;
return 0;
}
//INITIALIZING A SOCKET
memset(&server_address, 0, sizeof server_address); // if not work then use bzero((char*) &server_address, sizeof(server_address));
server_address.sin_family=AF_INET;
bcopy((char *) server -> h_addr, (char *) &server_address.sin_addr.s_addr, server -> h_length); //A NULL-terminated list of addresses for the host
//Addresses are returned in network byte order
// The macro h_addr is defined to be h_addr_list[0] for compatibility
server_address.sin_port =htons(port);
//COnNECTIONG TO THE SERVER...
int connection=connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address));
if(connection < 0)
{
cerr<<"Connection Error! Unable to connect"<<endl;
return 0;
}
int thread_no=0; // using as iterator for procthread array
while(thread_no<3)
{
//Creating a thread for sending and receiving msg and updating the terminating condition
pthread_create(&procThread[thread_no], NULL, ftch, NULL);
thread_no++;
pthread_create(&procThread[thread_no], NULL, snd, NULL);
thread_no++;
}
// WAITING FOR A THREAD TO TERMINATE...SOMETHING LIKE A QUEUE :)
for(int idx=0;idx<3;idx++)
{
pthread_join(procThread[idx],NULL);
}
}
void *snd(void *dummy)
{
while(true)
{
char msg[350];
cout<<"\rME> ";
memset(&msg,0,sizeof msg); //IF NOT WORK USE bzero(msg,350)
cin.getline(msg,350);
send(client_socket,msg, strlen(msg),0);
string message(msg);
if(message=="quit")
break;
}
cout<<"Closing connection..."<<endl;
cout<<"Closed!"<<endl;
close(client_socket);
exit(0);
}
void *ftch(void *dummy)
{
char msgg[350];
memset(&msgg,0,sizeof msgg);
while(true)
{
memset(&msgg,0,sizeof msgg);
get_size=read(client_socket, msgg, 350);
string ftchmsg (msgg);
cout << "\rOtherUser> " << ftchmsg << endl;
cout << "Me> ";
fflush(stdout);
if ( ftchmsg =="quit") break;
else if ( get_size == 0 ){
cout << "\rServer Diconnected" << endl;
break;
}
else if ( get_size == -1 ) {
cout << "\rRecieve Failed" << endl;
break;
}
}
cout << "\nClosing thread and connection..." << endl;
cout << "Closed!" << endl;
close(client_socket);
exit(0);
}