-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
65 lines (51 loc) · 2.14 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
/****************** CLIENT CODE ****************/
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc, char **argv){
int clientSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
if (argc != 3){
fprintf(stderr, "Usage: %s hostname portNumber\n", argv[0]);
exit(1);
}
struct hostent *server;
server = gethostbyname(argv[1]); //first argument is host name
printf("host ip: %s\n", inet_ntoa(*(struct in_addr*)server->h_addr_list[0]));
int portNumber = atoi(argv[2]); //second argument is the port number
/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(portNumber);
/* Set IP address for server */
//1. old code to set server address to localhost
//serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
//2. new code to set server address to the address of the hostname
//provided as an argument
//(e.g., when running the command (./client www.google.com))
//but when running the program on a local muchine you'll want
//to run (./client localhost) in order to communicate with the server
bcopy((char *)server->h_addr, //do man bcopy on the terminal
(char *)&serverAddr.sin_addr.s_addr,
server->h_length);
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*---- Connect the socket to the server using the address struct ----*/
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
/*---- Read the message from the server into the buffer ----*/
recv(clientSocket, buffer, 1024, 0);
/*---- Print the received message ----*/
printf("Data received: %s",buffer);
return 0;
}