-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient1.c
More file actions
100 lines (83 loc) · 3.29 KB
/
client1.c
File metadata and controls
100 lines (83 loc) · 3.29 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
//Client side code for single server multiple clients
//Compile as : gcc 180001064_client.c -o client
//Execute after server as started as : ./client CLIENTPORTNUMBER
//Change the IP address used in both codes to your system's IP address
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros
#define SERV_PORT 11000 //server port - same as in server code
#define STDIN 0 // stdin fd is 0
int main(int argc, char *argv[])
{
// buffer are for message communication
char inputBuffer[256], outputBuffer[256], desnPortBuf[256];
struct sockaddr_in server_addr, client_addr;
//set of socket descriptors
fd_set readfds;
if(argc!=2){ // require client port number as argument
printf("Pass Client Port Number as an argument.");
exit(0);
}
int clientPort = atoi(argv[1]);
int clientfd = socket(AF_INET, SOCK_STREAM, 0); // create client socket
if (clientfd < 0)
printf("Error in client creation\n");
else
printf("Client Created successfully.\n");
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_addr.s_addr = inet_addr("sysIP"); // enter system's machine IP
server_addr.sin_port = htons(SERV_PORT);
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = INADDR_ANY;
client_addr.sin_addr.s_addr = inet_addr("sysIP"); // enter system's machine IP
client_addr.sin_port = htons(clientPort); // port number is assigned after input from user
if (bind(clientfd, (struct sockaddr*) &client_addr, sizeof(struct sockaddr_in)) == 0)
printf("Port number binded Correctly\n");
else
printf("Unable to bind. Port number is either in use or inacesible.\n");
int con = connect(clientfd, (struct sockaddr*) &server_addr, sizeof server_addr);
if (con == 0)
printf("Client Connected successfully\n");
else
printf("Error in Connection with server\n");
recv(clientfd, outputBuffer, 256, 0);
printf("Server Welcome Message: %s", outputBuffer);
printf("Enter message at any time to send to other client.\n\n");
while(1){
memset(inputBuffer, 0, sizeof inputBuffer); // resetting all buffers
memset(desnPortBuf, 0, sizeof desnPortBuf);
memset(outputBuffer, 0, sizeof outputBuffer);
FD_ZERO(&readfds);
FD_SET(clientfd, &readfds);
FD_SET(STDIN, &readfds);
int max_sd = clientfd; //Always > 0
printf("Waiting for some activity.\n\n");
//wait indefinitely for activity on one of the sockets
int activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
if ((activity < 0) && (errno!=EINTR))
printf("error in selecting activity.\n");
if (FD_ISSET(STDIN, &readfds)){ // if stdin input is detected
fgets(inputBuffer, 256, stdin);
printf("Enter Destination port number.\n");
fgets(desnPortBuf, 256, stdin);
inputBuffer[strcspn(inputBuffer, "\n")] = 0; //removing \n from end of line
desnPortBuf[strcspn(desnPortBuf, "\n")] = 0;
send(clientfd, inputBuffer, 256, 0);
send(clientfd, desnPortBuf, 256, 0);
}
else if (FD_ISSET(clientfd, &readfds)){ // if message recieved from server
recv(clientfd, outputBuffer, 256, 0);
printf("Server sent : %s", outputBuffer);
}
}
close(clientfd);
return 0;
}