1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #define _WINSOCK_DEPRECATED_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <WinSock2.h> #pragma comment(lib, "ws2_32.lib")
#define REMOTE_IP "127.0.0.1" #define REMOTE_PORT 8080
int main() { WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in svraddr; svraddr.sin_family = AF_INET; svraddr.sin_port = htons(55443); svraddr.sin_addr.s_addr = inet_addr("192.168.100.100");
if (connect(sock, (struct sockaddr*)&svraddr, sizeof(svraddr)) != -1) { Sleep(1000); int sw = 0; char sendbuf1[] = "{\"id\":1,\"method\":\"set_bright\",\"params\":[100,\"smooth\",500]}\r\n"; char sendbuf2[] = "{\"id\":1,\"method\":\"set_bright\",\"params\":[10,\"smooth\",500]}\r\n"; char buf[1024] = { 0 }; while (scanf("%d",&sw)!=0) { switch (sw) { case 1: printf("%s", sendbuf1); if (send(sock, sendbuf1, strlen(sendbuf1), 0) != -1) { if (recv(sock, buf, 1024, 0) > 0) { printf("recv server: \r\n %s\r\n", buf); } memset(buf, 0, 1024); } break; case 2: printf("%s", sendbuf2); if (send(sock, sendbuf2, strlen(sendbuf2), 0) != -1) { if (recv(sock, buf, 1024, 0) > 0) { printf("recv server: \n %s\n", buf); } memset(buf, 0, 1024); } break;
} if (sw == 3) break; } } else { printf("can not connect server\n"); }
closesocket(sock);
WSACleanup(); getchar(); return 0; }
|