#include #include #include #include #include #include int main(int, char **argv) { sockaddr_in sa{}; sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr(argv[1]); sa.sin_port = htons(atoi(argv[2])); int udpSock = socket(AF_INET, SOCK_DGRAM, 0); sendto(udpSock, "READY", 5, 0, (sockaddr *)&sa, sizeof(sa)); char buf[256]{}; read(udpSock, buf, 255); close(udpSock); sa.sin_addr.s_addr = INADDR_ANY; sa.sin_port = htons(atoi(buf)); int tcpServ = socket(AF_INET, SOCK_STREAM, 0); int rv = bind(tcpServ, (sockaddr *)&sa, sizeof(sa)); if (rv == -1) { perror("bind failed"); return 1; } listen(tcpServ, 1); while (1) { int c1 = accept(tcpServ, 0, 0); int c2 = accept(tcpServ, 0, 0); while (1) { int cnt = read(c1, buf, 256); if (cnt <= 0) break; write(c2, buf, cnt); } shutdown(c1, SHUT_RDWR); shutdown(c2, SHUT_RDWR); close(c1); close(c2); } }