Skip to content

Commit

Permalink
refactor(methods-types): bzero was replaced by memset, because bzero …
Browse files Browse the repository at this point in the history
…is outdated
  • Loading branch information
Allan28818 committed Nov 6, 2024
1 parent 60abb8f commit 86aa229
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 47 deletions.
14 changes: 7 additions & 7 deletions client_server/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include <winsock2.h>
#define bzero(b, len) (memset((b), '\0', (len)), (void)0)
#define read(a, b, c) recv(a, b, c, 0)
#define write(a, b, c) send(a, b, c, 0)
#define close closesocket
Expand All @@ -27,15 +26,14 @@ void func(int sockfd)
int n;
for (;;)
{
bzero(buff, sizeof(buff));
memset(buff, 0, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
{
;
}
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
memset(buff, 0, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0)
Expand Down Expand Up @@ -63,8 +61,10 @@ int main()
atexit(cleanup);
#endif

int sockfd, connfd;
struct sockaddr_in servaddr, cli;
int sockfd;
int connfd;
struct sockaddr_in servaddr;
struct sockaddr_in cli;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
Expand All @@ -76,7 +76,7 @@ int main()
{
printf("Socket successfully created..\n");
}
bzero(&servaddr, sizeof(servaddr));
memset(&servaddr, 0, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
Expand Down
6 changes: 3 additions & 3 deletions client_server/remote_command_exec_udp_client.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifdef _WIN32
#define bzero(b, len) (memset((b), '\0', (len)), (void)0)
#define close _close
#include <Ws2tcpip.h>
#include <io.h>
Expand Down Expand Up @@ -29,7 +28,8 @@ void error()
int main()
{
uint32_t sockfd;
char send_msg[1024], recv_msg[1024];
char send_msg[1024];
char recv_msg[1024];

struct sockaddr_in server_addr;

Expand All @@ -40,7 +40,7 @@ int main()
error();
}

bzero(&server_addr, sizeof(server_addr));
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
Expand Down
11 changes: 6 additions & 5 deletions client_server/remote_command_exec_udp_server.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifdef _WIN32
#define bzero(b, len) (memset((b), '\0', (len)), (void)0)
#define close _close
#include <Ws2tcpip.h>
#include <io.h>
Expand Down Expand Up @@ -29,9 +28,11 @@ void error()
int main()
{
uint32_t sockfd;
char recv_msg[1024], success_message[] = "Command Executed Successfully!\n";
char recv_msg[1024];
char success_message[] = "Command Executed Successfully!\n";

struct sockaddr_in server_addr, client_addr;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;

socklen_t clientLength = sizeof(client_addr);

Expand All @@ -40,7 +41,7 @@ int main()
error();
}

bzero(&server_addr, sizeof(server_addr));
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
Expand All @@ -54,7 +55,7 @@ int main()

while (1)
{
bzero(recv_msg, sizeof(recv_msg));
memset(recv_msg, 0, sizeof(recv_msg));
recvfrom(sockfd, recv_msg, sizeof(recv_msg), 0,
(struct sockaddr *)&client_addr, &clientLength);
printf("Command Output: \n");
Expand Down
14 changes: 7 additions & 7 deletions client_server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include <winsock2.h>
#define bzero(b, len) (memset((b), '\0', (len)), (void)0)
#define read(a, b, c) recv(a, b, c, 0)
#define write(a, b, c) send(a, b, c, 0)
#define close closesocket
Expand Down Expand Up @@ -34,16 +33,15 @@ void func(int sockfd)

for (;;)
{
bzero(buff, MAX);
memset(buff, 0, MAX);

read(sockfd, buff, sizeof(buff));

printf("From client: %s\t To client : ", buff);
bzero(buff, MAX);
memset(buff, 0, MAX);
n = 0;
while ((buff[n++] = getchar()) != '\n')
{
;
}

write(sockfd, buff, sizeof(buff));
Expand All @@ -69,9 +67,11 @@ int main()
atexit(cleanup);
#endif

int sockfd, connfd;
int sockfd;
int connfd;
unsigned int len;
struct sockaddr_in servaddr, cli;
struct sockaddr_in servaddr;
struct sockaddr_in cli;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
Expand All @@ -83,7 +83,7 @@ int main()
{
printf("Socket successfully created..\n");
}
bzero(&servaddr, sizeof(servaddr));
memset(&servaddr, 0, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
Expand Down
10 changes: 5 additions & 5 deletions client_server/tcp_full_duplex_client.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifdef _WIN32
#define bzero(b, len) (memset((b), '\0', (len)), (void)0)
#define pid_t int
#define close _close
#include <Ws2tcpip.h>
Expand Down Expand Up @@ -33,7 +32,8 @@ void error()
int main()
{
uint32_t sockfd;
char sendbuff[1024], recvbuff[1024];
char sendbuff[1024];
char recvbuff[1024];

struct sockaddr_in server_addr;

Expand All @@ -42,7 +42,7 @@ int main()
error();
}

bzero(&server_addr, sizeof(server_addr));
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
Expand All @@ -60,7 +60,7 @@ int main()
{
while (1)
{
bzero(&sendbuff, sizeof(sendbuff));
memset(&sendbuff, 0, sizeof(sendbuff));
printf("\nType message here: ");
fgets(sendbuff, 1024, stdin);
send(sockfd, sendbuff, strlen(sendbuff) + 1, 0);
Expand All @@ -72,7 +72,7 @@ int main()
{
while (1)
{
bzero(&recvbuff, sizeof(recvbuff));
memset(&recvbuff, 0, sizeof(recvbuff));
recv(sockfd, recvbuff, sizeof(recvbuff), 0);
printf("\nSERVER: %s\n", recvbuff);
sleep(5);
Expand Down
16 changes: 9 additions & 7 deletions client_server/tcp_full_duplex_server.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifdef _WIN32
#define bzero(b, len) (memset((b), '\0', (len)), (void)0)
#define pid_t int
#define close _close
#include <Ws2tcpip.h>
Expand Down Expand Up @@ -32,10 +31,13 @@ void error()

int main()
{
uint32_t sockfd, conn;
char recvbuff[1024], sendbuff[1024];
uint32_t sockfd;
uint32_t conn;
char recvbuff[1024];
char sendbuff[1024];

struct sockaddr_in server_addr, client_addr;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;

socklen_t ClientLen;

Expand All @@ -44,7 +46,7 @@ int main()
error();
}

bzero(&server_addr, sizeof(server_addr));
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
Expand Down Expand Up @@ -78,7 +80,7 @@ int main()
{
while (1)
{
bzero(&recvbuff, sizeof(recvbuff));
memset(&recvbuff, 0, sizeof(recvbuff));
recv(conn, recvbuff, sizeof(recvbuff), 0);
printf("\nCLIENT : %s\n", recvbuff);
sleep(5);
Expand All @@ -88,7 +90,7 @@ int main()
{
while (1)
{
bzero(&sendbuff, sizeof(sendbuff));
memset(&sendbuff, 0, sizeof(sendbuff));
printf("\nType message here: ");
fgets(sendbuff, 1024, stdin);
send(conn, sendbuff, strlen(sendbuff) + 1, 0);
Expand Down
10 changes: 5 additions & 5 deletions client_server/tcp_half_duplex_client.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifdef _WIN32
#define bzero(b, len) (memset((b), '\0', (len)), (void)0)
#define close _close
#include <Ws2tcpip.h>
#include <io.h>
Expand Down Expand Up @@ -28,14 +27,15 @@ int main()
uint32_t sockfd;
struct sockaddr_in server_addr;

char serverResponse[10000], clientResponse[10000];
char serverResponse[10000];
char clientResponse[10000];

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
error();
}

bzero(&server_addr, sizeof(server_addr));
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
Expand All @@ -48,8 +48,8 @@ int main()

while (1)
{
bzero(&serverResponse, sizeof(serverResponse));
bzero(&clientResponse, sizeof(clientResponse));
memset(&serverResponse, 0, sizeof(serverResponse));
memset(&clientResponse, 0, sizeof(clientResponse));

recv(sockfd, serverResponse, sizeof(serverResponse), 0);
printf("\nServer message: %s \n", serverResponse);
Expand Down
16 changes: 9 additions & 7 deletions client_server/tcp_half_duplex_server.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifdef _WIN32
#define bzero(b, len) (memset((b), '\0', (len)), (void)0)
#define close _close
#include <Ws2tcpip.h>
#include <io.h>
Expand All @@ -25,17 +24,20 @@ void error()

int main()
{
uint32_t sockfd, conn;
char server_msg[10000], client_msg[10000];
uint32_t sockfd;
uint32_t conn;
char server_msg[10000];
char client_msg[10000];

struct sockaddr_in server_addr, client_addr;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
error();
}

bzero(&server_addr, sizeof(server_addr));
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
Expand All @@ -57,8 +59,8 @@ int main()

while (1)
{
bzero(&server_msg, sizeof(server_msg));
bzero(&client_msg, sizeof(client_msg));
memset(&server_msg, 0, sizeof(server_msg));
memset(&client_msg, 0, sizeof(client_msg));

printf("\nEnter message here: ");
fgets(server_msg, 10000, stdin);
Expand Down
3 changes: 2 additions & 1 deletion client_server/udp_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ int main()
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
struct sockaddr_in servaddr;
struct sockaddr_in cliaddr;

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
Expand Down

0 comments on commit 86aa229

Please sign in to comment.