• P2P聊天程序,学习TcpListener/TcpClient/Socket用法


    点击此处下载源文件

       服务器端创建 Socket后,在客户端,你将可以通过Connect方法连接到指定的服务器,客户端也可以通过Write方法向远程服务器发送数据,而后可以通过 Receive从服务端接收数据;而在服务器端,你需要使用Bind方法绑定所指定的接口使Socket与一个本地终结点相联,并通过Listen方法侦听该接口上的请求,当侦听到用户端的连接时,调用Accept完成连接的操作,创建新的Socket以处理传入的连接请求。

       Socket完成后,使用Close方法关闭Socket。


    服务器端程序:
    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Net.Sockets;

    using System.Threading;

    using System.Net;

     

    namespace tcpchater

    {

        class tcpserver

        {

            static void Main(string[] args)

            {

                try

                {

                     //初始化监听,端口为

                     TcpListener myList = new TcpListener(IPAddress.Parse("127.0.0.1"), 8001);

     

                     //开始监听服务器端口

                     myList.Start();

     

                     Console.WriteLine("启动服务器端,端口服务...");

                     Console.WriteLine("本地节点为:" + myList.LocalEndpoint);//LocalEndpoint属性标识正用于侦听传入客户端连接请求的本地网络接口和端口号

                     Console.WriteLine("等待客户端连接...");

     

                     //等待处理接入连接请求

                     Socket s = myList.AcceptSocket();

     

                     //新建立的连接用套接字s表示

                     Console.WriteLine("客户端连接来自" + s.RemoteEndPoint + " 已上线.");

     

                     System.Text.UTF8Encoding utf8 = new UTF8Encoding(); //可以处理中文

                     //接收客户信息

                     byte[] b = new byte[2048];

                     int k = s.Receive(b);

                     Console.Write("客户端说:" + utf8.GetString(b, 0, k));

                     Console.WriteLine();

                     Thread.Sleep(3000);//停秒

     

     

                     Console.Write("服务器端说:");

                     //处理客户端请求,给客户端回应

                     string str = Console.ReadLine();

                     s.Send(utf8.GetBytes(str));

                     Console.WriteLine("已发送回应信息");

     

                    //释放资源,结束监听

                     s.Close();

                     myList.Stop();

                 }

                 catch (Exception e)

                 {

                     Console.WriteLine("错误..." + e.StackTrace);

                 }

            }

        }

    }



    客户端程序

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Net.Sockets;

    using System.IO;

     

    namespace tcpclient

    {

       class tcpclient

        {

            static void Main(string[] args)

            {

                try

                {

     

                    //建立客户端套接字

                    TcpClient tcpclnt = new TcpClient();

                    Console.WriteLine("正在连接服务器...");

     

                    //连接服务器

                    tcpclnt.Connect("127.0.0.1", 8001);

     

                    //得到客户端的流

                    NetworkStream stm = tcpclnt.GetStream();

     

                    Console.WriteLine("已连接到服务器");

                    Console.Write("客户端说:");

                    string str = Console.ReadLine();//输入说话内容

                    //发送字符串

                    System.Text.UTF8Encoding utf8 = new UTF8Encoding(); //可以处理中文

                    byte[] ba = utf8.GetBytes(str);

                    Console.WriteLine("数据已经传送到服务器,等待服务器回应...");

                    stm.Write(ba, 0, ba.Length);

                    //接收从服务器返回的信息

                    byte[] bb = new byte[2048];

                    int k = stm.Read(bb, 0, 100);

                    //输出服务器端返回的信息

                    Console.WriteLine("服务器说:"+ utf8.GetString(bb, 0, k));

                    tcpclnt.Close();

                }

                catch (Exception e)

                {

                    Console.WriteLine("错误..." + e.StackTrace);

                }

            }

        }

    }

  • 相关阅读:
    Encoding
    F Takio与Blue的人生赢家之战
    D FFF团的怒火
    C Golden gun的巧克力
    B 倒不了的塔
    A jubeat
    17230 计算轴承半径
    10686 DeathGod不知道的事情
    10688 XYM-AC之路
    10692 XYM-入门之道
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/1578946.html
Copyright © 2020-2023  润新知