zoukankan      html  css  js  c++  java
  • C#网络通信

    Server:

    using System.Net;
    using System.Net.Sockets;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ServerConsole
    {
        class Server
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Server is runing...");
                IPAddress ip = new IPAddress(new byte[]{127,0,0,1});
                TcpListener listener = new TcpListener(ip, 8500);
    
                listener.Start();//开始侦听
                while (true)
                {
                    //获取一个连接,中断方法
                    TcpClient remoteClient = listener.AcceptTcpClient();
                    //打印连接客户端的信息
                    Console.WriteLine("Client Connected!{0}---->{1}", remoteClient.Client.LocalEndPoint,remoteClient.Client.RemoteEndPoint);
                }
            }
        }
    }

    Client:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    
    namespace ClientConsole
    {
        class Client
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Client is running...");
                TcpClient client = null;
    
                for (int i = 0; i < 3; i++)
                {
                    client = new TcpClient();
                    try
                    {
                        //与服务器建立连接
                        client.Connect("localhost", 8500); ;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception occured.." + ex.Message);
                        return;
                    }
                
                    //输出已连接到服务器
                    Console.WriteLine("Server Connected!{0}-->{1}",
                    client.Client.LocalEndPoint, client.Client.RemoteEndPoint);
                }
                ConsoleKey key;
                do
                {
                    key = Console.ReadKey(true).Key; 
                } while (key != ConsoleKey.Q);
            }
        }
    }
  • 相关阅读:
    浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
    Spring中ClassPathXmlApplicationContext类的简单使用
    测试计划/系统风险 (设计方面、开发方面、测试本身 等风险)
    浏览器兼容
    4.0 爬虫
    2.1 IDEA
    2.3 接口测试
    1.1测试分类
    1.3 ODPS
    2.1 Word 插入 smartart、图表
  • 原文地址:https://www.cnblogs.com/cause/p/3912824.html
Copyright © 2011-2022 走看看