-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketSendRecvTools.c
More file actions
155 lines (129 loc) · 4.23 KB
/
SocketSendRecvTools.c
File metadata and controls
155 lines (129 loc) · 4.23 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
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
148
149
150
151
152
153
154
155
/*
Authors: Nave Assaf 308044809, Roi Elad 305625402
Project: ex4
Description: This module cares for the actual transfer of the messages betwwen the client and server ports. It does so over TCP.
*/
#include "SocketSendRecvTools.h"
#include <stdio.h>
#include <string.h>
/*
Function: SendBuffer
Inputs: Buffer - buffer in use.
BytesToSend - the requested bytes needed to be send.
sd - socket uses for connection.
Outputs: TRNS_SUCCEEDED/TRNS_FAILED.
Functionality: auxiliary function to be use in SendString() in order to send buffer.
*/
TransferResult_t SendBuffer(const char* Buffer, int BytesToSend, SOCKET sd)
{
const char* CurPlacePtr = Buffer;
int BytesTransferred;
int RemainingBytesToSend = BytesToSend;
while (RemainingBytesToSend > 0)
{
/* send does not guarantee that the entire message is sent */
BytesTransferred = send(sd, CurPlacePtr, RemainingBytesToSend, 0);
if (BytesTransferred == SOCKET_ERROR)
{
return TRNS_FAILED;
}
RemainingBytesToSend -= BytesTransferred;
CurPlacePtr += BytesTransferred; // <ISP> pointer arithmetic
}
return TRNS_SUCCEEDED;
}
/*
Function: SendString
Inputs: Str - request sent to server.
sd - socket uses for connection.
Outputs: SendRes - the send result.
Functionality: The function sends the the request to the server on socket sd.
*/
TransferResult_t SendString(const char *Str, SOCKET sd)
{
/* Send the the request to the server on socket sd */
int TotalStringSizeInBytes;
TransferResult_t SendRes;
/* The request is sent in two parts. First the Length of the string (stored in
an int variable ), then the string itself. */
TotalStringSizeInBytes = (int)(strlen(Str) + 1); // terminating zero also sent
SendRes = SendBuffer(
(const char *)(&TotalStringSizeInBytes),
(int)(sizeof(TotalStringSizeInBytes)), // sizeof(int)
sd);
if (SendRes != TRNS_SUCCEEDED) return SendRes;
SendRes = SendBuffer(
(const char *)(Str),
(int)(TotalStringSizeInBytes),
sd);
return SendRes;
}
/*
Function: ReceiveBuffer
Inputs: OutputBuffer-buffer in use.
BytesToSend - the requested bytes needed to be send.
sd - socket uses for connection.
Outputs: TRNS_SUCCEEDED/TRNS_FAILED.
Functionality: auxiliary function to be use in ReceiveString() in order to receive buffer.
*/
TransferResult_t ReceiveBuffer(char* OutputBuffer, int BytesToReceive, SOCKET sd)
{
char* CurPlacePtr = OutputBuffer;
int BytesJustTransferred;
int RemainingBytesToReceive = BytesToReceive;
while (RemainingBytesToReceive > 0)
{
/* send does not guarantee that the entire message is sent */
BytesJustTransferred = recv(sd, CurPlacePtr, RemainingBytesToReceive, 0);
if (BytesJustTransferred == SOCKET_ERROR)
{
return TRNS_FAILED;
}
else if (BytesJustTransferred == 0)
return TRNS_DISCONNECTED; // recv() returns zero if connection was gracefully disconnected.
RemainingBytesToReceive -= BytesJustTransferred;
CurPlacePtr += BytesJustTransferred; // <ISP> pointer arithmetic
}
return TRNS_SUCCEEDED;
}
/*
Function: ReceiveString
Inputs: OutputStrPtr - request received to server.
sd - socket uses for connection.
Outputs: RecvRes - the received result.
Functionality: The function recveives the the request to the server on socket sd.
*/
TransferResult_t ReceiveString(char** OutputStrPtr, SOCKET sd)
{
/* Recv the the request to the server on socket sd */
int TotalStringSizeInBytes;
TransferResult_t RecvRes;
char* StrBuffer = NULL;
if ((OutputStrPtr == NULL) || (*OutputStrPtr != NULL))
{
return TRNS_FAILED;
}
/* The request is received in two parts. First the Length of the string (stored in
an int variable ), then the string itself. */
RecvRes = ReceiveBuffer(
(char *)(&TotalStringSizeInBytes),
(int)(sizeof(TotalStringSizeInBytes)), // 4 bytes
sd);
if (RecvRes != TRNS_SUCCEEDED) return RecvRes;
StrBuffer = (char*)malloc(TotalStringSizeInBytes * sizeof(char));
if (StrBuffer == NULL)
return TRNS_FAILED;
RecvRes = ReceiveBuffer(
(char *)(StrBuffer),
(int)(TotalStringSizeInBytes),
sd);
if (RecvRes == TRNS_SUCCEEDED)
{
*OutputStrPtr = StrBuffer;
}
else
{
free(StrBuffer);
}
return RecvRes;
}