add templates for tcp client/server

This commit is contained in:
Roy Qu 2022-04-22 15:46:25 +08:00
parent ec17cfcb99
commit 331365b0c7
9 changed files with 187 additions and 4 deletions

BIN
windows/templates/mysql.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -4,8 +4,8 @@ Name=MySQL(MyriaDB)
Icon=mysql.ico
Description=A Sqlite3 API Example
Description[zh_CN]=MySQL数据库示例程序
Category=Utilities
Category[zh_CN]=工具
Category=Database
Category[zh_CN]=数据库
[Unit0]
CName=main.c

View File

@ -4,8 +4,8 @@ Name=Sqlite3
Icon=sqlite.ico
Description=A Sqlite3 API Example
Description[zh_CN]=Sqlite3 示例程序
Category=Utilities
Category[zh_CN]=工具
Category=Database
Category[zh_CN]=数据库
[Unit0]
CName=main.c

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

View File

@ -0,0 +1,20 @@
[Template]
ver=2
Name=TCP Client
Name[zh_CN]=TCP客户端
Icon=tcpclient.ico
Description=A winsock2 tcp client
Description[zh_CN]=基于Winsock2的TCP客户端程序示例
Category=Network
Category[zh_CN]=网络
[Unit0]
CName=main.c
C=tcpclient_c.txt
[Project]
UnitCount=1
Type=1
Compiler=
CppCompiler=
Linker=-lws2_32

View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#define PORT 8888
#define SERVER_IP "127.0.0.1"
#define BUFFER_SIZE 4196
const char* kExitFlag = "exit";
int main() {
// 初始化socket dll。
WORD winsock_version = MAKEWORD(2,2);
WSADATA wsa_data;
if (WSAStartup(winsock_version, &wsa_data) != 0) {
printf("Failed to init socket!\n");
return 1;
}
SOCKET client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client_socket == INVALID_SOCKET) {
printf("Failed to create server socket!\n");
return 2;
}
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);
if (connect(client_socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("Failed to connect server: %ld !\n", GetLastError());
return 3;
}
char recv_data[BUFFER_SIZE+1];
while (true) {
char data[BUFFER_SIZE+1];
printf("Input data: ");
scanf("%s",data);
if (send(client_socket, data, strlen(data), 0) < 0) {
printf("Failed to send data!\n");
break;
}
int ret = recv(client_socket, recv_data, BUFFER_SIZE, 0);
if (ret < 0) {
printf("Failed to receive data!\n");
break;
}
recv_data[ret]=0; // correctly ends received string
printf("Receive data from server: \"%s\"\n",recv_data);
if (data == kExitFlag) {
printf("Exit!\n");
break;
}
}
closesocket(client_socket);
WSACleanup();
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

View File

@ -0,0 +1,20 @@
[Template]
ver=2
Name=TCP Server
Name[zh_CN]=TCP服务器
Icon=tcpserver.ico
Description=A winsock2 tcp server demo
Description[zh_CN]=基于Winsock2的TCP服务器程序示例
Category=Network
Category[zh_CN]=网络
[Unit0]
CName=main.c
C=tcpserver_c.txt
[Project]
UnitCount=1
Type=1
Compiler=
CppCompiler=
Linker=-lws2_32

View File

@ -0,0 +1,78 @@
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#define PORT 8888
#define BUFFER_SIZE 4196
const char* kExitFlag = "exit";
int main() {
WORD winsock_version = MAKEWORD(2,2);
WSADATA wsa_data;
if (WSAStartup(winsock_version, &wsa_data) != 0) {
printf("Failed to init socket dll!\n");
return 1;
}
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;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(server_socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("Failed to bind port!\n");
return 3;
}
if (listen(server_socket, 10)) {
printf("Failed to listen!\n");
return 4;
}
sockaddr_in client_addr;
int client_addr_len = sizeof(client_addr);
printf("Wait for connecting...\n");
SOCKET client_socket = accept(server_socket, (SOCKADDR*)&client_addr, &client_addr_len);
if (client_socket == INVALID_SOCKET) {
printf("Failed to accept!\n");
return 5;
}
printf("Succeed to receive a connection: %s\n" , inet_ntoa(client_addr.sin_addr));
char recv_buf[BUFFER_SIZE+1];
while (true) {
int ret = recv(client_socket, recv_buf, BUFFER_SIZE, 0);
if (ret < 0) {
printf("Failed to receive data!\n");
break;
}
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);
printf("Exit!\n");
break;
}
const char* send_data = "Hello, Tcp Client!\n";
send(client_socket, send_data, strlen(send_data), 0);
}
closesocket(client_socket);
closesocket(server_socket);
WSACleanup();
return 0;
}