-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
109 lines (95 loc) · 2.76 KB
/
client.c
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
/****************** CLIENT CODE TCP CONNECTION ****************/
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <stdbool.h>
#define SERVER_TCP_PORT 3002
#define BUFLEN 10000
int main(int argc, char **argv){
int sd, port;
int n, bytes_to_read;
struct hostent *hp;
struct sockaddr_in server;
char *host, *bp, rbuf[BUFLEN], sbuf[BUFLEN];
bool ifexit = false;
switch(argc){
case 2:
host=argv[1];
port=SERVER_TCP_PORT; // assigning port number 3002
break;
case 3:
host=argv[1];
port=atoi(argv[1]);
break;
default:
printf("Usage: %s [port]\n", argv[0]);
exit(1);
}
// a) int family: Address family as AF_INET for Internet connection using TCP/IP b) int type: Type as SOCK_STREAM for connection oriented service c) int protocol: '0' indicates default protocol which is TCP for AF_INET and SOCK_STREAM
if((sd = socket(AF_INET, SOCK_STREAM, 0))== -1){ // sd = socket descriptor returned by socket call
printf("cant create socket\n");
exit(1);
}
// Bind an address to socket
server.sin_family= AF_INET; // family identifier : AF_INET
server.sin_port = htons(port); // convert unsigned_short form host to network byte order(big endian)
printf("server port : %d port in network byte order: %d \n", port, server.sin_port);
if((hp = gethostbyname(host))==NULL){
printf("cant get server address\n");
exit(1);
}
bcopy(hp->h_addr, (char*) &server.sin_addr, hp->h_length);
if(connect(sd, (struct sockaddr *) &server, sizeof(server))==-1){
printf("cant connect\n");
exit(1);
}
printf("connected: server address: %s \n", hp->h_name);
printf("Enter '*' to switch \n");
while(ifexit == false){ // connected until '#' entered by client or server
do{
bp=sbuf; // send buffer
printf("client:\n");
fgets(sbuf, sizeof(sbuf), stdin);
write(sd, sbuf, BUFLEN); // writing/ sending to client
if(*sbuf == '*'){ // '*' for switching between server and client
*sbuf='*';
*rbuf='*';
}
if(*sbuf == '#'){ // '#' for closing the connection
ifexit = true;
printf("closed by you \n");
goto cls;
}
}while(*sbuf != 42);
do{
bp=rbuf; // receive buffer
bytes_to_read=BUFLEN;
while ((n= read(sd, bp, bytes_to_read)) > 0){ // read from server in 'receive buffer'
bp+=n;
bytes_to_read-= n;
}
printf("server:\n");
printf("%s\n", rbuf);
if(*rbuf == '*'){
*rbuf='*';
*sbuf='*';
}
if(*rbuf == '#'){
ifexit = true;
printf("closed by server\n");
goto cls;
}
}while(*rbuf != 42);
cls: if(ifexit == true){
close(sd); // closing socket
}
}
close(sd);
return(0);
}