-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
148 lines (137 loc) · 3.82 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
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
144
145
146
147
/*
*This work is licensed under a Creative Commons Attribution 4.0 International License.
*Author: Benedetto Marco Serinelli
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 53000 //port number
#define BACKLOG 10 //number
#define BUFSIZE 256 //size of the message buffer
#define SERVER_IP "127.0.0.1" //server IP address
void openFileAndWriteReadRandomNumber();
void openAndPrintContent();
void saveAllFileContentInsideVariable();
int main(int argc, char *argv[]) {
int socket_fd;
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if( socket_fd == -1 )
perror("Socket creation failed");
else
printf("Socket created\n");
//define server address and port in order to establish connection
struct sockaddr_in addr;
//set address 0.0.0.0
memset(&addr, 0, sizeof(addr));
//set family address
addr.sin_family = AF_INET;
//set port
addr.sin_port = htons(PORT);
//convert IPv4 address from text to binary form
if(inet_pton(AF_INET, SERVER_IP, &(addr.sin_addr.s_addr)) < 0)
perror("inet_pton() failed");
socklen_t len = sizeof(addr);
//connect to server
int server_fd;
server_fd = socket(AF_INET, SOCK_STREAM, 0);
//create socket
if(server_fd < 0)
perror("Socket can't be created");
else
printf("Socket is created successfully\n");
//establish connection
if(connect(server_fd, (struct sockaddr *)&addr, len) < 0)
perror("Connection can't be established");
else
printf("Connection is established.\n");
//prepare message
char buff_message[BUFSIZE] = "";
ssize_t sent_byte = 0;
ssize_t receeive_byte = 0;
snprintf(buff_message, BUFSIZE - 1, "Hello server :)"); //format and store a series of characters and values in the array buffer.
//sleep one 1 second
sleep(1);
//send message
sent_byte = send(server_fd, buff_message, strlen(buff_message), 0);
if(sent_byte < 0)
perror("Message can't be sent");
else
printf("Message was sent. Message size in byte: %d\n", (int)sent_byte);
//Receive message from server
receeive_byte = recv(server_fd, buff_message, BUFSIZE - 1, 0);
if(receeive_byte < 0) {
perror("Message from server can't be received");
}
else {
if(receeive_byte == 0)
printf("Close connection with server\n");
else if (receeive_byte > 0)
printf("Message from server: %s\n", buff_message);
}
close(server_fd);
openFileAndWriteReadRandomNumber();
openAndPrintContent();
saveAllFileContentInsideVariable();
exit(0);
}
void openFileAndWriteReadRandomNumber() {
char str[10];
FILE *fp;
fp = fopen("random.txt", "w");
if (fp == NULL) {
printf("Impossible to open a file");
exit(1);
}
for (int i = 0; i < 10; ++i) {
fprintf(fp, "%d", rand() % 10);
fprintf(fp, "\n");
}
fclose(fp);
}
void openAndPrintContent() {
FILE *fp;
fp = fopen("random.txt", "r");
if (fp == NULL) {
printf("Impossible to open a file");
exit(1);
}
char content = fgetc(fp);
printf("*** FILE CONTENT ***\n");
while (content != EOF) {
printf("%c", content);
content = fgetc(fp);
}
printf("*** END FILE CONTENT ***\n");
fclose(fp);
}
void saveAllFileContentInsideVariable(){
printf("*** PRINT FILE CONTENT 2 ***\n");
char * buffer = 0;
long length;
FILE *fp;
fp = fopen("random.txt", "r");
if (fp == NULL) {
printf("Impossible to open a file");
exit(1);
}
if (fp){
fseek (fp, 0, SEEK_END);
length = ftell (fp);
fseek (fp, 0, SEEK_SET);
buffer = malloc (length);
if (buffer)
{
fread (buffer, 1, length, fp);
}
fclose (fp);
}
if (buffer)
{
// start to process your data / extract strings here...
printf("Content: %s\n", buffer);
}
}