欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#使用Socket发送和接收TCP数据实例

程序员文章站 2023-12-15 17:34:46
本文实例讲述了asp.net中c#使用socket发送和接收tcp数据的方法,分享给大家供大家参考。具体实现方法如下: 具体程序代码如下: 复制代码 代码如下:usin...

本文实例讲述了asp.net中c#使用socket发送和接收tcp数据的方法,分享给大家供大家参考。具体实现方法如下:

具体程序代码如下:

复制代码 代码如下:
using system;
using system.collections.generic;
using system.net;
using system.net.sockets;
using system.text;
namespace consoleapplication1
{
    public static class sockettest
    {
        private static encoding encode = encoding.default;
        /// <summary>
        /// 监听请求
        /// </summary>
        /// <param name="port"></param>
        public static void listen(int port)
        {
            socket listensocket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);
            listensocket.bind(new ipendpoint(ipaddress.any, port));
            listensocket.listen(100);
            console.writeline("listen " + port + " ...");
            while (true)
            {
                socket acceptsocket = listensocket.accept();
                string receivedata = receive(acceptsocket, 5000); //5 seconds timeout.
                console.writeline("receive:" + receivedata);
                acceptsocket.send(encode.getbytes("ok"));
                destroysocket(acceptsocket); //import
            }
        }
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string send(string host, int port, string data)
        {
            string result = string.empty;
            socket clientsocket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);
            clientsocket.connect(host, port);
            clientsocket.send(encode.getbytes(data));
            console.writeline("send:" + data);
            result = receive(clientsocket, 5000 * 2); //5*2 seconds timeout.
            console.writeline("receive:" + result);
            destroysocket(clientsocket);
            return result;
        }
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        private static string receive(socket socket, int timeout)
        {
            string result = string.empty;
            socket.receivetimeout = timeout;
            list<byte> data = new list<byte>();
            byte[] buffer = new byte[1024];
            int length = 0;
            try
            {
                while ((length = socket.receive(buffer)) > 0)
                {
                    for (int j = 0; j < length; j++)
                    {
                        data.add(buffer[j]);
                    }
                    if (length < buffer.length)
                    {
                        break;
                    }
                }
            }
            catch { }
            if (data.count > 0)
            {
                result = encode.getstring(data.toarray(), 0, data.count);
            }
            return result;
        }
        /// <summary>
        /// 销毁socket对象
        /// </summary>
        /// <param name="socket"></param>
        private static void destroysocket(socket socket)
        {
            if (socket.connected)
            {
                socket.shutdown(socketshutdown.both);
            }
            socket.close();
        }
    }
}

运行服务器监听数据:

复制代码 代码如下:
sockettest.listen(8888);

发送和接收tcp数据:

复制代码 代码如下:
sockettest.send("127.0.0.1", 8888, "www.jb51.net");

运行效果如下图所示:

C#使用Socket发送和接收TCP数据实例

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: