• C#TcpClient TcpListener客户端服务器程序


    服务器程序:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    using System.Threading;
    namespace SocketServer
    {
        class Program
        {
            public void ProcessConnection(Object client)
            {
                TcpClient tcpClient = (TcpClient)client;
                NetworkStream networkStream = tcpClient.GetStream();
                BinaryReader br = new BinaryReader(networkStream);
                BinaryWriter bw = new BinaryWriter(networkStream);
                while (true)
                {
                    try
                    {
                        string sReader = br.ReadString(); //接收消息
                        Console.WriteLine(sReader); //打印消息
                        string sWriter = "接收到消息";
                        bw.Write(sWriter);   //向对方发送消息
                    }
                    catch
                    {
                        break;
                    }
                }
            }
            static void Main()
            {
                Program p = new Program();
                IPAddress localAddress;      //IP地址
                int port = 51888;     //端口
                TcpListener tcpListener;  //监听套接字
                TcpClient tcpClient;     
                IPAddress[] listenIp = Dns.GetHostAddresses("127.0.0.1");
                localAddress = listenIp[0];
                tcpListener = new TcpListener(localAddress, port);
                tcpListener.Start(); //开始监听
                while (true)
                {
                    try
                    {
                        tcpClient = tcpListener.AcceptTcpClient();//每接受一个客户端则生成一个TcpClient
                        Thread thread = new Thread(p.ProcessConnection); //用一个线程单独处理这个连接
                        thread.Start(tcpClient);
                    }
                    catch
                    {
                        break;
                    }
                }
            }
        }
    }
    客户端程序:
    <pre name="code" class="csharp">using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    namespace SocketClient
    {
        class Program
        {
            static void Main()
            {
                TcpClient tcpClient;   
                tcpClient = new TcpClient();  //创建一个TcpClient对象,自动分配主机IP地址和端口号
                tcpClient.Connect("127.0.0.1", 51888);   //连接服务器,其IP和端口号为127.0.0.1和51888
                if (tcpClient != null)
                {
                    Console.WriteLine("连接服务器成功");
                    NetworkStream networkStream= tcpClient.GetStream();
                    BinaryReader br = new BinaryReader(networkStream);
                    BinaryWriter bw = new BinaryWriter(networkStream);
                    bw.Write("你好服务器,我是客户端");  //向服务器发送字符串
                    while (true)        
                    {
                        try
                        {
                            string brString = br.ReadString();     //接收服务器发送的数据
                            if (brString != null)
                            {
                                Console.WriteLine("接收到服务器发送的数据{0}", brString);
                            }
                        }
                        catch
                        {
                            break;        //接收过程中如果出现异常,将推出循环
                        }
                    }
                }
                Console.WriteLine("连接服务器失败");
            }
        }
    }


    
    

  • 相关阅读:
    Sencha touch 2 入门 -------- DataView 显示服务器端JSON文件数据
    Sencha touch API
    Android Intent详解
    物流配送中商品订货数量的控制技术
    multiset基础学习,可以有重复类型的多重集合容器
    人生总会遇到浑噩期,但是需要反思
    创建Sencha touch第一个应用
    How I Turned Down $300,000 from Microsoft to go Full-Time on GitHub
    c++ list 合并操作函数实例
    电子设计与制作100例(第3版)
  • 原文地址:https://www.cnblogs.com/zztong/p/6695188.html
Copyright © 2020-2023  润新知