This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhangyang-libzt/tests/zts/zts.tcpclient4.c

72 lines
1.9 KiB
C
Raw Normal View History

2016-12-16 13:04:01 -08:00
// TCP Client test program (IPV4)
2016-12-13 17:13:14 -08:00
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstdlib>
2017-03-08 17:06:36 -08:00
2016-12-13 17:13:14 -08:00
#include "sdk.h"
int atoi(const char *str);
int close(int filedes);
#define MSG_SZ 128
int main(int argc , char *argv[])
{
if(argc < 3) {
2016-12-16 13:04:01 -08:00
printf("usage: client <addr> <port> <netpath> <nwid>\n");
2016-12-13 17:13:14 -08:00
return 1;
}
2017-03-08 17:06:36 -08:00
/* Starts ZeroTier core service in separate thread, loads user-space TCP/IP stack
and sets up a private AF_UNIX socket between ZeroTier library and your app. Any
subsequent zts_* socket API calls (shown below) are mediated over this hidden AF_UNIX
socket and are spoofed to appear as AF_INET sockets. The implementation of this API
is in src/sockets.c */
2016-12-16 13:04:01 -08:00
zts_init_rpc(argv[3],argv[4]);
2017-03-08 17:06:36 -08:00
2016-12-13 17:13:14 -08:00
int sock, port = atoi(argv[2]);
struct sockaddr_in server;
char server_reply[MSG_SZ];
2016-12-13 17:13:14 -08:00
sock = zts_socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1) {
printf("could not create socket");
}
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_family = AF_INET;
server.sin_port = htons( port );
printf("connecting...\n");
if (zts_connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) {
perror("connect failed. Error");
return 1;
}
printf("connected\n");
char *msg = "welcome to the machine!";
while(1)
{
// TX
if(send(sock, msg, strlen(msg), 0) < 0) {
printf("send failed");
return 1;
}
else {
printf("TX: %s\n", msg);
printf("len = %ld\n", strlen(msg));
int bytes_read = read(sock, server_reply, MSG_SZ);
if(bytes_read < 0)
printf("\tRX: Nothing\n");
else
printf("\tRX = (%d bytes): %s\n", bytes_read, server_reply);
}
}
close(sock);
return 0;
}