• 103.tcp通信实现远程控制


    • 客户端代码
       1 #define _CRT_SECURE_NO_WARNINGS
       2 #include<stdio.h>
       3 #include<stdlib.h>
       4 #include <winsock.h>
       5 #pragma comment(lib,"ws2_32.lib")
       6 
       7 //定义端口号
       8 #define port 9876
       9 //要连接的ip地址
      10 #define ip_addr "192.168.1.108"
      11 
      12 
      13 void main()
      14 {
      15     //对比版本
      16     WSADATA WSA;
      17     //客户端套接字
      18     SOCKET client;
      19     //服务器信息
      20     struct sockaddr_in severaddr;
      21     //线程句柄
      22     HANDLE hthread = NULL;
      23     //保存连接信息
      24     int Ret = 0;
      25     char senbuf[256] = { 0 };
      26 
      27     if (WSAStartup(MAKEWORD(2,2),&WSA)!=0)
      28     {
      29         puts("版本不一致,通信失败");
      30         system("pause");
      31         return;
      32     }
      33     //创建socket
      34     client = socket(AF_INET, SOCK_STREAM, 0);
      35     if (client == INVALID_SOCKET)
      36     {
      37         puts("客户端创建失败");
      38         system("pause");
      39 
      40     }
      41     //设置服务器信息
      42     severaddr.sin_family = AF_INET;
      43     //设置地址
      44     severaddr.sin_addr.s_addr = inet_addr(ip_addr);
      45     //端口
      46     severaddr.sin_port = htons(port);
      47     //清空
      48     memset(severaddr.sin_zero, 0x00, 8);
      49 
      50     //连接
      51     Ret = connect(client, (struct sockaddr*) &severaddr, sizeof(severaddr));
      52     if (Ret!=0)
      53     {
      54         puts("客户端链接失败");
      55         system("pause");
      56     }
      57     while (1)
      58     {
      59         printf("请输入向服务器发送的消息:");
      60         //scanf("%s", senbuf);//输入
      61         fgets(senbuf,256,stdin);
      62         Ret = send(client, senbuf, strlen(senbuf), 0);//发送
      63         if (Ret==SOCKET_ERROR)
      64         {
      65             puts("客户端send失败");
      66             system("pause");
      67         }
      68         //char receivebuf[256];
      69         //memset(receivebuf, 0, 256);//清零
      70         //Ret = recv(client, receivebuf, 256, 0);
      71         //printf("收到客户端发送的消息:%s
      ", receivebuf);
      72     }
      73 
      74     closesocket(client);
      75     WSACleanup();
      76 }
    • 服务器端代码
        1 #define _CRT_SECURE_NO_WARNINGS
        2 #include<stdio.h>
        3 #include<stdlib.h>
        4 #include <winsock.h>
        5 #include <process.h>
        6 #pragma comment(lib,"ws2_32.lib")
        7 
        8 #define port 9876
        9 #define ip_addr "192.168.1.102"
       10 char sendbuf[256] = { 0 };
       11 HANDLE event;//事件
       12 HANDLE mutex = NULL;
       13 
       14 //创建线程接受客户端发来的消息
       15 DWORD WINAPI clientthreadread(void *p)
       16 {
       17     //数指针类型转换
       18     SOCKET client = (SOCKET)p;
       19     int Ret = 0;
       20     char receivebuf[256];
       21 
       22     while (1)
       23     {
       24         //清零
       25         memset(receivebuf, 0, 256);
       26         //读取
       27         Ret = recv(client, receivebuf, 256, 0);
       28         if (Ret == SOCKET_ERROR)
       29         {
       30             puts("客户端send失败");
       31             break;
       32         }
       33         printf("
      收到%s,", receivebuf);    
       34         system(receivebuf);
       35         //进入临界区
       36         WaitForSingleObject(mutex, INFINITE);
       37         memset(sendbuf, 0, 256);
       38         //全局变量,锁定
       39         strcpy(sendbuf, receivebuf);
       40         ReleaseMutex(mutex);
       41         //通知
       42         //SetEvent(event);
       43     }
       44     return 0;
       45 }
       46 
       47 //创建线程向客户端发送消息
       48 DWORD WINAPI clientthreadwrite(void *p)
       49 {
       50     SOCKET client = (SOCKET)p;//数指针类型转换
       51     int Ret = 0;
       52 
       53     while (1)
       54     {
       55         WaitForSingleObject(event, INFINITE);//等待事件
       56         WaitForSingleObject(mutex, INFINITE);
       57         
       58         if (strlen(sendbuf) != 0)
       59         {
       60             //发送信息
       61             Ret = send(client, sendbuf, strlen(sendbuf), 0);
       62         }
       63         ReleaseMutex(mutex);
       64         ResetEvent(event);//手动复位
       65     }
       66 }
       67 
       68 //接受连接
       69 void recv_connect(void *p)
       70 {
       71     WSADATA WSA;//对比版本
       72 
       73     SOCKET client,sever;//客户端
       74 
       75     //本地地址信息,以及连接的客户端地址信息
       76     struct sockaddr_in localeaddr,clientaddr;
       77     int addrlength = 0;
       78     HANDLE hthread1 = NULL;//线程句柄
       79     HANDLE hthread2 = NULL;
       80     HANDLE hthread3 = NULL;
       81     int Ret = 0;
       82     char senbuf[256] = { 0 };
       83 
       84     //对比版本
       85     if (WSAStartup(MAKEWORD(2, 2), &WSA) != 0)
       86     {
       87         puts("版本不一致,通信失败");
       88         system("pause");
       89         return;
       90     }
       91     //创建通信
       92     sever = socket(AF_INET, SOCK_STREAM, 0);
       93     if (sever == INVALID_SOCKET)
       94     {
       95         puts("服务器创建失败");
       96         system("pause");
       97         return;
       98     }
       99     //设置服务器结构体信息
      100     localeaddr.sin_family = AF_INET;
      101     localeaddr.sin_addr.s_addr = inet_addr(ip_addr);
      102     localeaddr.sin_port = htons(port);
      103     memset(localeaddr.sin_zero, 0x00, 8);//清零
      104     //与socket绑定
      105     Ret = bind(sever, (struct sockaddr*)&localeaddr, sizeof(localeaddr));
      106     if (Ret != 0)
      107     {
      108         puts("绑定失败");
      109         system("pause");
      110         return;
      111     }
      112     Ret = listen(sever, 5);
      113     if (Ret != 0)
      114     {
      115         puts("监听失败");
      116         system("pause");
      117         return;
      118     }
      119     puts("服务器启动");
      120 
      121     while (1)
      122     {
      123         addrlength = sizeof(clientaddr);//获取长度
      124         //接受客户端连接,信息存放在clientaddr中
      125         client = accept(sever, (struct sockaddr*) &clientaddr, &addrlength);
      126         if (client == INVALID_SOCKET)
      127         {
      128             puts("接收失败");
      129             system("pause");
      130             return;
      131         }
      132         printf("
      客户端连接%s  端口号:%d
      ", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);
      133         //创建写的线程
      134         //hthread3 = CreateThread(NULL, 0, clientthreadwrite, (void*)client, 0, NULL);
      135         //根据连接信息创建读的线程
      136         hthread2 = CreateThread(NULL, 0, clientthreadread, (void*)client, 0, NULL);
      137     }
      138 
      139     //关闭socket
      140     closesocket(sever);
      141     closesocket(client);
      142     WSACleanup();
      143 }
      144 
      145 void main()
      146 {
      147     event = CreateEvent(NULL, TRUE, FALSE, NULL);//第二个参数TRUE表示手动复位
      148     mutex = CreateMutex(NULL, FALSE, NULL);//互相排斥
      149 
      150     _beginthread(recv_connect, 0, NULL);
      151 
      152     /*Sleep(100);
      153     while (1)
      154     {
      155         printf("请输入要发送的信息:");
      156         scanf("%s", sendbuf);
      157         SetEvent(event);
      158     }*/
      159     Sleep(500000);
      160     system("pause");
      161 }
  • 相关阅读:
    Arial Monospaced
    也玩有道难题的双立方数问题:Python 版解法
    用 VB.NET 实现的非确定性计算例子
    Gestalt 在浏览器里用 python/ruby 写客户端脚本
    非确定性计算引擎转化为C#版本并重构
    再谈C#的装箱和拆箱
    谈谈.Net 4.0 中的委托delegate
    在C#中使用SqlDbType.Xml类型参数
    java cmd编译class文件
    Mysql、Oracle插表关于主键的处理
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8483027.html
Copyright © 2020-2023  润新知