From 1a7322f562a9c28e353679127632342bfe7064ec Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Fri, 22 Apr 2022 16:12:14 +0800 Subject: [PATCH] update tcpclient/server templates --- windows/templates/tcpclient_c.txt | 6 +++--- windows/templates/tcpserver_c.txt | 13 ++++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/windows/templates/tcpclient_c.txt b/windows/templates/tcpclient_c.txt index 6fe72ad4..24c6b477 100644 --- a/windows/templates/tcpclient_c.txt +++ b/windows/templates/tcpclient_c.txt @@ -1,6 +1,6 @@ #include #include - +#include #include #define PORT 8888 #define SERVER_IP "127.0.0.1" @@ -23,7 +23,7 @@ int main() { return 2; } - sockaddr_in server_addr; + struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); server_addr.sin_addr.S_un.S_addr = inet_addr(SERVER_IP); @@ -52,7 +52,7 @@ int main() { printf("Receive data from server: \"%s\"\n",recv_data); - if (data == kExitFlag) { + if (strcmp(data,kExitFlag)==0) { printf("Exit!\n"); break; } diff --git a/windows/templates/tcpserver_c.txt b/windows/templates/tcpserver_c.txt index 4f3705ff..521cd7cc 100644 --- a/windows/templates/tcpserver_c.txt +++ b/windows/templates/tcpserver_c.txt @@ -1,6 +1,6 @@ #include #include - +#include #include #define PORT 8888 @@ -16,13 +16,13 @@ int main() { return 1; } - SOCKET server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + SOCKET server_socket= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (server_socket == INVALID_SOCKET) { printf("Failed to create server socket!\n"); return 2; } - sockaddr_in server_addr; + struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); server_addr.sin_addr.S_un.S_addr = INADDR_ANY; @@ -37,7 +37,7 @@ int main() { return 4; } - sockaddr_in client_addr; + struct sockaddr_in client_addr; int client_addr_len = sizeof(client_addr); printf("Wait for connecting...\n"); @@ -59,13 +59,12 @@ int main() { recv_buf[ret]=0; // correctly ends received string printf("Receive from Client: \"%s\" \n", recv_buf); - if (strcmp(kExitFlag,recv_buf)==0) { - send(client_socket, kExitFlag, sizeof(kExitFlag), 0); + if (strcmp(kExitFlag,recv_buf)==0) { printf("Exit!\n"); break; } - const char* send_data = "Hello, Tcp Client!\n"; + const char* send_data = "Hello, Tcp Client!"; send(client_socket, send_data, strlen(send_data), 0); }