• 在C#中初遇Socket


    后期项目实战:多人在线聊天室

    源码位置:https://git.oschina.net/z13qu/BlogProjects

    前言

    第一篇主要对Socket有个基本认识,实现初始化,发送、接受消息;本篇主要用Socekt实现服务端和客户端的对话功能。

    服务端代码

    static void Main(string[] args)
            {
                IPAddress ip = IPAddress.Parse("127.0.0.1");
                IPEndPoint ipe = new IPEndPoint(ip, 5001);
                //1.建立一个Socket对象
                Socket socketServer = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
                //2.Socket关联到这个IP上面
                socketServer.Bind(ipe);
                socketServer.Listen(8);
                Console.WriteLine("正在监听...");
                //接收发送连接信息的客户端(一直等待客户端连接,不继续执行)
                Socket acceptSocket = socketServer.Accept();
                Console.WriteLine("连接成功!!");
                //接受输入信息
                string inputMsg = string.Empty;
                //保存接受到的信息
                string message = string.Empty;
                while (true)
                {
                    byte[] receiveBytes = new byte[2048];
                    //服务端会一直等待客户端发送消息过来,否则不继续执行
                    int byteLen = acceptSocket.Receive(receiveBytes, receiveBytes.Length, 0);
                    message = Encoding.UTF8.GetString(receiveBytes, 0, byteLen);
                    Console.WriteLine("客户端说:" + message);
    
                    Console.WriteLine("请您输入信息给客户端(输入exit退出对话):");
                    inputMsg = Console.ReadLine();
                    if (inputMsg.ToLower() == "exit")//退出对话
                    {
                        break;
                    }
                    byte[] sendBytes = Encoding.UTF8.GetBytes(inputMsg);
                    acceptSocket.Send(sendBytes, sendBytes.Length, 0);
                }
                //释放Socket占用的资源
                acceptSocket.Dispose();
                socketServer.Dispose();
    
                Console.ReadKey();
            }
    

    客户端代码

    static void Main(string[] args)
            {
                IPAddress ip = IPAddress.Parse("127.0.0.1");
                IPEndPoint ipe = new IPEndPoint(ip, 5001);
                //1.建立一个Socket对象
                Socket socketClient = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
    
                //2.去连接这个IP(没有Socket监听这个端口,就会报错)
                socketClient.Connect(ipe);
                Console.WriteLine("连接服务端" + ipe + "成功");
    
                //输入信息
                string inputMsg = string.Empty;
                //接受服务端发送过来的信息
                string message = string.Empty;
                while (true)
                {
                    Console.WriteLine("请您输入信息给服务端(输入exit退出对话):");
                    inputMsg = Console.ReadLine();
                    if (inputMsg.ToLower() == "exit")//退出对话
                    {
                        break;
                    }
                    
                    byte[] sendBytes = Encoding.UTF8.GetBytes(inputMsg);
                    socketClient.Send(sendBytes);
    
                    byte[] receiveBytes = new byte[2048];
                    //客户端会一直等待服务器发送消息,否则不继续执行
                    int byteLen = socketClient.Receive(receiveBytes,
                    receiveBytes.Length, 0);
                    message = Encoding.UTF8.GetString(receiveBytes, 0, byteLen);
                    Console.WriteLine("服务器说:" + message);
                }
                //释放Socket占用的资源
                socketClient.Dispose();
                Console.ReadKey();
            }
    

    总结

    注意:先运行服务端,再运行客户端。

    至此已经完成一个简单的Socket对话功能,
    但是你会发现,客户端发送一句话,服务端才能发送一句话。
    只能实现你说一句,我答一句的机制,离真实的对话场景还差一点。
    那么让我们下篇来实现这个功能吧。


    【原文链接】http://www.cnblogs.com/z13qu/p/6995420.html

  • 相关阅读:
    VS2010中的单元测试
    GSM局数据制作2(Erision)
    WPF的BitmapImage的文件无法释放及内存泄露的问题
    跨库查询推荐使用的方法
    我们能做什么呢?
    长尾理论:Windows Vista
    Blackberry阻碍因素
    RIM终于想通了RIM开放新的API
    Prototype库终于有了文档了
    自己编写的MSN历史记录合并工具
  • 原文地址:https://www.cnblogs.com/z13qu/p/6995420.html
Copyright © 2020-2023  润新知