-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_tcp.cpp
More file actions
58 lines (44 loc) · 1.34 KB
/
client_tcp.cpp
File metadata and controls
58 lines (44 loc) · 1.34 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
#include "common.h"
using namespace std;
int main(int argc, char *argv[])
{
if(argc < 3)
{
cout << "Expected execution : ./client host port" << endl;
return 0;
}
int sckt,server,bufferSize = 1024;
char buffer[bufferSize];
struct hostent *remoteHost;
struct sockaddr_in server_addr;
sckt = socket(AF_INET, SOCK_STREAM, 0);
if (sckt < 0)
{
perror("Error establishing a socket !!");
return 0;
}
remoteHost = gethostbyname(argv[1]);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
bcopy((char *)remoteHost->h_addr,(char *)&server_addr.sin_addr,remoteHost->h_length);
server = connect(sckt,(struct sockaddr *)&server_addr, sizeof(server_addr)) ;
if ( server < 0){
perror("Unable to connect to server !!");
return 0;
}
cout << "Successfully connected to the server..." << endl;
while(true)
{
memset(buffer,0,strlen(buffer));
cin.getline(buffer,bufferSize);
send(sckt, buffer, strlen(buffer), 0);
if(cin.fail())
{
close(sckt);
cout << "File read and passed out successfully..Exiting the connection..!!" <<endl ;
return 0;
}
}
cout << "Server disconnected !! Closing the socket..." << endl ;
close(sckt);
}