Skip to content

Commit

Permalink
Minor update to C examples
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-henry committed May 6, 2021
1 parent 2c66689 commit 613c7e8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
6 changes: 3 additions & 3 deletions examples/c/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ int main(int argc, char** argv)
// Data I/O

printf("Sending message string to server...\n");
if ((bytes = zts_bsd_write(fd, msgStr, strlen(msgStr))) < 0) {
if ((bytes = zts_write(fd, msgStr, strlen(msgStr))) < 0) {
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
exit(1);
}
printf("Sent %d bytes: %s\n", bytes, msgStr);
printf("Reading message string from server...\n");
if ((bytes = zts_bsd_read(fd, recvBuf, sizeof(recvBuf))) < 0) {
if ((bytes = zts_read(fd, recvBuf, sizeof(recvBuf))) < 0) {
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
exit(1);
}
Expand All @@ -101,6 +101,6 @@ int main(int argc, char** argv)
// Close

printf("Closing sockets\n");
zts_bsd_close(fd);
zts_close(fd);
return zts_node_stop();
}
8 changes: 4 additions & 4 deletions examples/c/nonblockingclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int main(int argc, char** argv)

// Create socket

if ((fd = zts_bsd_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
printf("Error (fd=%d, zts_errno=%d). Exiting.\n", fd, zts_errno);
exit(1);
}
Expand All @@ -83,14 +83,14 @@ int main(int argc, char** argv)
// Wait random intervals to send a message to the server
// The non-blocking aspect of this example is server-side
while (1) {
if ((bytes = zts_bsd_send(fd, msgStr, strlen(msgStr), 0)) < 0) {
if ((bytes = zts_send(fd, msgStr, strlen(msgStr), 0)) < 0) {
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
exit(1);
}
printf("zts_bsd_send()=%d\n", bytes);
printf("zts_send()=%d\n", bytes);
zts_util_delay((rand() % 100) * 50);
}

zts_bsd_close(fd);
zts_close(fd);
return zts_node_stop();
}
24 changes: 11 additions & 13 deletions examples/c/nonblockingserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int main(int argc, char** argv)
// Sockets

printf("Creating socket...\n");
if ((fd = zts_bsd_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
exit(1);
}
Expand All @@ -82,27 +82,25 @@ int main(int argc, char** argv)
}
printf("Listening...\n");
int backlog = 100;
if ((err = zts_bsd_listen(fd, backlog)) < 0) {
if ((err = zts_listen(fd, backlog)) < 0) {
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
exit(1);
}

int bytes = 0;
char recvBuf[128] = { 0 };

while (1) {
// Accept
// Can also use
// zts_bsd_accept(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen)
// Accept
// Can also use
// zts_bsd_accept(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen)

char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 };
unsigned int port = 0;
printf("Accepting on listening socket...\n");
if ((accfd = zts_accept(fd, ipstr, ZTS_INET6_ADDRSTRLEN, &port)) < 0) {
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
}
printf("Accepted connection from %s:%d\n", ipstr, port);
char remote_ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 };
unsigned int port = 0;
printf("Accepting on listening socket...\n");
if ((accfd = zts_accept(fd, remote_ipstr, ZTS_INET6_ADDRSTRLEN, &port)) < 0) {
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
}
printf("Accepted connection from %s:%d\n", remote_ipstr, port);

// Data I/O

Expand Down
8 changes: 4 additions & 4 deletions examples/c/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ int main(int argc, char** argv)
char recvBuf[128] = { 0 };

printf("Reading message string from client...\n");
if ((bytes = zts_bsd_read(accfd, recvBuf, sizeof(recvBuf))) < 0) {
if ((bytes = zts_read(accfd, recvBuf, sizeof(recvBuf))) < 0) {
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
exit(1);
}
printf("Read %d bytes: %s\n", bytes, recvBuf);
printf("Sending message string to client...\n");
if ((bytes = zts_bsd_write(accfd, recvBuf, bytes)) < 0) {
if ((bytes = zts_write(accfd, recvBuf, bytes)) < 0) {
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
exit(1);
}
Expand All @@ -104,7 +104,7 @@ int main(int argc, char** argv)
// Close

printf("Closing sockets\n");
err = zts_bsd_close(accfd);
err = zts_bsd_close(fd);
err = zts_close(accfd);
err = zts_close(fd);
return zts_node_stop();
}

0 comments on commit 613c7e8

Please sign in to comment.