• C#套接字和windowsAPI套接字


    C#服务器端

    第一步:用指定的端口号和服务器的ip建立一个EndPoint对像;
    第二步:建立一个Socket对像;
    第三步:用socket对像的Bind()方法绑定EndPoint;
    第四步:用socket对像的Listen()方法开始监听;
    第五步:接受到客户端的连接,用socket对像的Accept()方法创建新的socket对像用于和请求的客户端进行通信;
    第六步:通信结束后一定记得关闭socket

                int port = 8889;
                string host = "127.0.0.1";
    
                ///创建终结点(EndPoint)
                IPAddress ip = IPAddress.Parse(host);//把ip地址字符串转换为IPAddress类型的实例
                IPEndPoint ipe = new IPEndPoint(ip, port);//用指定的端口和ip初始化IPEndPoint类的新实例
    
                ///创建socket并开始监听
                /// 
                Socket s = new Socket(SocketType.Stream, ProtocolType.Tcp);//创建一个socket对像,如果用udp协议,则要用SocketType.Dgram类型的套接字
                s.Bind(ipe);//绑定EndPoint对像(2000端口和ip地址)
                s.Listen(0);//开始监听
                Console.WriteLine("等待客户端连接服务器为{0}:{1}",host,port);
    
                ///接受到client连接,为此连接建立新的socket,并接受信息
                Socket temp = s.Accept();//为新建连接创建新的socket
                Console.WriteLine("建立连接");
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes= temp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接受信息
                recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
    
                ///给client端返回信息
                Console.WriteLine("server get message:{0}", recvStr);//把客户端传来的信息显示出来
                string sendStr = "ok!Client send message successful!";
                byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                temp.Send(bs, bs.Length, 0);//返回信息给客户端
                temp.Close();
                s.Close();
                Console.ReadLine();

    C#客户端

    第一步:用指定的端口号和服务器的ip建立一个EndPoint对像;
    第二步:建立一个Socket对像;
    第三步:用socket对像的Connect()方法以上面建立的EndPoint对像做为参数,向服务器发出连接请求;
    第四步:如果连接成功,就用socket对像的Send()方法向服务器发送信息;
    第五步:用socket对像的Receive()方法接受服务器发来的信息 ;
    第六步:通信结束后一定记得关闭socket;

              int port = 8889;
                    string host = "127.0.0.1";
                    ///创建终结点EndPoint
                    IPAddress ip = IPAddress.Parse(host);
                    IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndpoint实例
    
                    ///创建socket并连接到服务器
                    
                    Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建Socket
                    Console.WriteLine("Conneting…");
                    c.Connect(ipe);//连接到服务器
    
                    ///向服务器发送信息
                    string sendStr = "hello!This is a socket test";
                    byte[] bs = Encoding.ASCII.GetBytes(sendStr);//把字符串编码为字节
                    Console.WriteLine("Send Message");
                    c.Send(bs, bs.Length, 0);//发送信息
    
                    ///接受从服务器返回的信息 
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
                   string recvStr = Encoding.ASCII.GetString(recvBytes, 0, bytes);
                    Console.WriteLine("client get message:{0}", recvStr);//显示服务器返回信息
                                                                         ///一定记着用完socket后要关闭
                    c.Close();

     windows API服务器端

    //Server.cpp
    #include <iostream>
    #include <winsock2.h>
    
    using namespace std;
    
    #pragma comment(lib, "ws2_32.lib")
    
    #define PORT 8889
    #define IP_ADDRESS "127.0.0.1"
    
    
    DWORD WINAPI ClientThread(LPVOID lpParameter)
    {
        SOCKET CientSocket = (SOCKET)lpParameter;
        int Ret = 0;
        char RecvBuffer[MAX_PATH];
    
        while (true)
        {
            memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
            Ret = recv(CientSocket, RecvBuffer, MAX_PATH, 0);
            if (Ret == 0 || Ret == SOCKET_ERROR)
            {
                cout << "客户端退出!" << endl;
                break;
            }
            cout << "接收到客户信息为:" << RecvBuffer << endl;
        }
    
        return 0;
    }
    
    int main(int argc, char* argv[])
    {
        WSADATA  Ws;
        SOCKET ServerSocket, ClientSocket;
        struct sockaddr_in LocalAddr, ClientAddr;
        int Ret = 0;
        int AddrLen = 0;
        HANDLE hThread = NULL;
    
        //Init Windows Socket
        if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0)
        {
            cout << "Init Windows Socket Failed::" << GetLastError() << endl;
            return -1;
        }
    
        //Create Socket
        ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (ServerSocket == INVALID_SOCKET)
        {
            cout << "Create Socket Failed::" << GetLastError() << endl;
            return -1;
        }
    
        LocalAddr.sin_family = AF_INET;
        LocalAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
        LocalAddr.sin_port = htons(PORT);
        memset(LocalAddr.sin_zero, 0x00, 8);
    
        //Bind Socket
        Ret = bind(ServerSocket, (struct sockaddr*)&LocalAddr, sizeof(LocalAddr));
        if (Ret != 0)
        {
            cout << "Bind Socket Failed::" << GetLastError() << endl;
            return -1;
        }
        //listen
        Ret = listen(ServerSocket, 10);
        if (Ret != 0)
        {
            cout << "listen Socket Failed::" << GetLastError() << endl;
            return -1;
        }
    
        cout << "服务端已经启动" << endl;
    
        while (true)
        {
            AddrLen = sizeof(ClientAddr);
            ClientSocket = accept(ServerSocket, (struct sockaddr*)&ClientAddr, &AddrLen);
            if (ClientSocket == INVALID_SOCKET)
            {
                cout << "Accept Failed::" << GetLastError() << endl;
                break;
            }
    
            cout << "客户端连接::" << inet_ntoa(ClientAddr.sin_addr) << ":" << ClientAddr.sin_port << endl;
    
            hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)ClientSocket, 0, NULL);
            if (hThread == NULL)
            {
                cout << "Create Thread Failed!" << endl;
                break;
            }
    
            CloseHandle(hThread);
        }
    
        closesocket(ServerSocket);
        closesocket(ClientSocket);
        WSACleanup();
    
        return 0;
    }

    windows API客户端

    // Client.cpp
    #include <iostream>
    #include <winsock2.h>
    using namespace std;
    
    #pragma comment(lib, "ws2_32.lib")
    
    #define PORT 8889
    #define IP_ADDRESS "127.0.0.1"
    
    int main(int argc, char * argv[])
    {
        WSADATA Ws;
        SOCKET ClientSocket;
        struct sockaddr_in ServerAddr;
        int Ret = 0;
        int AddrLen = 0;
        HANDLE hThread = NULL;
        char SendBuffer[MAX_PATH];
    
        //Init Windows Socket
        if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0)
        {
            cout << "Init Windows Socket Failed::" << GetLastError() << endl;
            return -1;
        }
        //Create Socket
        ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (ClientSocket == INVALID_SOCKET)
        {
            cout << "Create Socket Failed::" << GetLastError() << endl;
            return -1;
        }
    
        ServerAddr.sin_family = AF_INET;
        ServerAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
        ServerAddr.sin_port = htons(PORT);
        memset(ServerAddr.sin_zero, 0x00, 8);
    
        Ret = connect(ClientSocket, (struct sockaddr*)&ServerAddr, sizeof(ServerAddr));
        if (Ret == SOCKET_ERROR)
        {
            cout << "Connect Error::" << GetLastError() << endl;
            return -1;
        }
        else
        {
            cout << "连接成功!" << endl;
        }
    
        while (true)
        {
            cin.getline(SendBuffer, sizeof(SendBuffer));
            Ret = send(ClientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
            if (Ret == SOCKET_ERROR)
            {
                cout << "Send Info Error::" << GetLastError() << endl;
                break;
            }
        }
    
        closesocket(ClientSocket);
        WSACleanup();
    
        return 0;
    }
    网络字节序与主机字节序转换,有如下4个函数:
      htons(),htonl(),ntohs(),ntohl()
    h表示host字节序,n表示net字节序,s表示short,l表示long。
    htons是将整型变量从主机字节顺序转变成网络字节顺序, 就是整数在地址空间存储方式变为:高位字节存放在内存的低地址处。
    网络字节顺序是TCP/IP中规定好的一种数据表示格式,它与具体的CPU类型、操作系统等无关,从而可以保证数据在不同主机之间传输时能够被正确解释,网络字节顺序采用big-endian排序方式。
  • 相关阅读:
    Github.Git
    android 环境的配置
    如何在Virtual box 下安装Mac os
    如何下载google play商店里面的app?
    神奇的网站
    pycharm使用总结
    软件测试 -- SQL注入测试
    win10 下mysql环境变量配置
    NStask
    在pycharm 中使用 GitHub:
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/5349271.html
Copyright © 2020-2023  润新知