-
Notifications
You must be signed in to change notification settings - Fork 5
/
send_recieve.h
93 lines (61 loc) · 1.86 KB
/
send_recieve.h
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
//
// Abhinav Thakur
//
// Description: This header file contains the send_string and recieve_line
//
// This function is used as a replacement for recv() since recv() does not tell the accurate
// number of data bytes recieved (it includes the "\r\n" - End Of Line(EOL) charecters also)
// So where we want to take account of accurate number of bytes recieved we will use this
// function. This function returns number of bytes recieved (excluding "\r\n").
//
int recieve_line(int socket_fd, unsigned char *buffer)
{
unsigned char *ptr;
int CR_flag=0, NL_flag=0; // Carriag Return (CR) and NewLine(NL) flags
int recieved_length;
ptr = buffer;
// Read byte by byte at address pointed to by 'ptr'
while (recv(socket_fd, ptr, 1, 0) == 1)
{
// check the byte first for '\r' ODH carriage return
if (*ptr == '\r' || CR_flag == 1)
{
// set Carriage Return Flag to 1, found '\r'
CR_flag = 1;
// if newline feed '\n' is found
if (*ptr == '\n')
{
// Mark the place before '\n' as End of string (terminating by a NULL charecter)
*(ptr - 1) = '\0';
return strlen(buffer);
}
// If carriage return '\r' is found but newline feed '\n' is not yet found
if (CR_flag == 1 && NL_flag == 0)
++ptr;
}
// increment to next index
else
++ptr;
}
// EOL sequence not found
return 0;
}
// This string sends each and every byte from string pointed to by ptr.
// Returns 1 on success
int send_string(int sockfd, char *ptr)
{
int bytes_sent=0;
int bytes_to_send;
bytes_to_send = strlen(ptr);
// if there are still bytes left unsent
while(bytes_to_send > 0)
{
bytes_sent = send(sockfd, ptr, bytes_to_send, 0);
if (bytes_sent == 0)
return 0;
bytes_to_send -= bytes_sent;
ptr = ptr + bytes_sent;
}
return 1;
}