sockets(examples): remove unused register, replace bzero with memset

* Remove the redundant register keyword from the variables, as most
compilers (GCC/Clang) just ignore them, and they're mostly just noise.

* Replace bzero() with memset(). IEEE 1003.1-2008 removes the spec of
bzero(), replacing it with memset().

* Use socklen_t (unsigned type) for variable b in the socket bind
example.

* Add unistd.h header file in the socket connect example (it is used by
the write() and read() libc calls). Remove unnecessary (char *) cast
from argv[1].

Signed-off-by:	rilysh <nightquick@proton.me>
Reviewed-by:	emaste@
This commit is contained in:
rilysh 2024-09-04 10:29:11 +05:30 committed by Daniel Ebdrup Jensen
parent 652b15a387
commit 920aa82eb1
1 changed files with 10 additions and 14 deletions

View File

@ -562,8 +562,7 @@ We now know enough to write a very simple client, one that will get current time
#include <unistd.h> #include <unistd.h>
int main() { int main() {
register int s; int s, bytes;
register int bytes;
struct sockaddr_in sa; struct sockaddr_in sa;
char buffer[BUFSIZ+1]; char buffer[BUFSIZ+1];
@ -572,7 +571,7 @@ int main() {
return 1; return 1;
} }
bzero(&sa, sizeof sa); memset(&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET; sa.sin_family = AF_INET;
sa.sin_port = htons(13); sa.sin_port = htons(13);
@ -718,8 +717,8 @@ The child calls `listen`, then starts an endless loop, which accepts a connectio
#define BACKLOG 4 #define BACKLOG 4
int main() { int main() {
register int s, c; int s, c;
int b; socklen_t b;
struct sockaddr_in sa; struct sockaddr_in sa;
time_t t; time_t t;
struct tm *tm; struct tm *tm;
@ -730,7 +729,7 @@ int main() {
return 1; return 1;
} }
bzero(&sa, sizeof sa); memset(&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET; sa.sin_family = AF_INET;
sa.sin_port = htons(13); sa.sin_port = htons(13);
@ -747,11 +746,9 @@ int main() {
case -1: case -1:
perror("fork"); perror("fork");
return 3; return 3;
break;
default: default:
close(s); close(s);
return 0; return 0;
break;
case 0: case 0:
break; break;
} }
@ -773,7 +770,6 @@ int main() {
if ((t = time(NULL)) < 0) { if ((t = time(NULL)) < 0) {
perror("daytimed time"); perror("daytimed time");
return 6; return 6;
} }
@ -976,14 +972,14 @@ This allows us to create a much more flexible-and much more useful-version of ou
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h> #include <netdb.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
register int s; int s, bytes;
register int bytes;
struct sockaddr_in sa; struct sockaddr_in sa;
struct hostent *he; struct hostent *he;
char buf[BUFSIZ+1]; char buf[BUFSIZ+1];
@ -994,19 +990,19 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
bzero(&sa, sizeof sa); memset(&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET; sa.sin_family = AF_INET;
sa.sin_port = htons(13); sa.sin_port = htons(13);
host = (argc > 1) ? (char *)argv[1] : "time.nist.gov"; host = (argc > 1) ? argv[1] : "time.nist.gov";
if ((he = gethostbyname(host)) == NULL) { if ((he = gethostbyname(host)) == NULL) {
herror(host); herror(host);
return 2; return 2;
} }
bcopy(he->h_addr_list[0],&sa.sin_addr, he->h_length); memcpy(&sa.sin_addr, he->h_addr_list[0], he->h_length);
if (connect(s, (struct sockaddr *)&sa, sizeof sa) < 0) { if (connect(s, (struct sockaddr *)&sa, sizeof sa) < 0) {
perror("connect"); perror("connect");