static int ptcp_open(struct protocol_ctx *sc, const char *url) { struct ptcp_ctx *c = sc->priv; int len; char *p; char *tag = ":"; p = strstr(url, tag); if (!p) { printf("ptcp url is invalid\n"); return -1; } len = p - url; strncpy(c->src_ip, url, len); p += strlen(tag); c->src_port = atoi(p); ptcp_socket_t *ps = ptcp_socket(); if (ps == NULL) { printf("error!\n"); return -1; } c->ps = ps; struct sockaddr_in si; si.sin_family = AF_INET; si.sin_addr.s_addr = inet_addr(c->src_ip); si.sin_port = htons(c->src_port); ptcp_bind(ps, (struct sockaddr*)&si, sizeof(si)); printf("url: \"ptcp://%s:%d\"\n", c->src_ip, c->src_port); ptcp_listen(ps, 0); return 0; }
//=================ptcp======================= void *ptcp_server_init(const char *host, uint16_t port) { ptcp_socket_t *ps = ptcp_socket(); if (ps == NULL) { printf("error!\n"); return NULL; } struct sockaddr_in si; si.sin_family = AF_INET; si.sin_addr.s_addr = host ? inet_addr(host) : INADDR_ANY; si.sin_port = htons(port); ptcp_bind(ps, (struct sockaddr*)&si, sizeof(si)); ptcp_listen(ps, 0); return ps; }
void *ptcp_client_init(const char *host, uint16_t port) { ptcp_socket_t *ps = ptcp_socket(); if (ps == NULL) { printf("error!\n"); return NULL; } struct sockaddr_in si; si.sin_family = AF_INET; si.sin_addr.s_addr = inet_addr(host); si.sin_port = htons(port); if (0 != ptcp_connect(ps, (struct sockaddr*)&si, sizeof(si))) { printf("ptcp_connect failed!\n"); } else { printf("ptcp_connect success\n"); } return ps; }