当前位置:编程学习 > 网站相关 >>

濡備綍杩愮敤 C#璁剧疆缃戠粶閫氫俊鐨勬敹鍙戝姛鑳?/span> [闂鐐规暟锛?0鍒哴

杩愮敤C#璁剧疆缃戠粶閫氫俊鐨勬敹鍙戝姛鑳斤紝鍙鑳界畝鍗曠殑涓庣浉搴旂殑IP鍦板潃鑱旂綉锛屽彂鍑虹畝鍗曚俊鎭紝瀵规柟鑳藉鎺ユ敹鍒板氨鍙互 --------------------编程问答-------------------- Service

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDPServer
{
    class Program
    {

        private static Socket newsock;
        private static IPEndPoint iPEndPoint;
        private static EndPoint Remote;
        static void Main(string[] args)
        {
            int i = 0;
            try
            {
                while (true)
                {
                    int port = 2001;
                    string host = "192.168.10.9";
                    IPAddress ip = IPAddress.Parse(host);
                    IPEndPoint ipe = new IPEndPoint(ip, port);
                    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//鍒涘缓涓€涓猄ocket绫?br />                     s.Bind(ipe);//缁戝畾2000绔彛
                    
                    //s.Listen(0);//寮€濮嬬洃鍚?br />                     //Console.WriteLine("Wait for connect");
                    //Socket temp = s.Accept();//涓烘柊寤鸿繛鎺ュ垱寤烘柊鐨凷ocket銆?br />                     //Console.WriteLine("Get a connect");
                    string recvStr = "";
                    byte[] recvBytes = new byte[4096];
                    int bytes;
                    EndPoint enp = (EndPoint)ipe;
                    bytes = s.ReceiveFrom(recvBytes, ref enp);//浠庡鎴风鎺ュ彈淇℃伅
                    recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);

                    Console.WriteLine("Server Get Message:{0}", recvStr);//鎶婂鎴风浼犳潵鐨勪俊鎭樉绀哄嚭鏉?br />
                    string sendStr = "  Ok! Client Send Message Sucessful!";
                    byte[] bs = Encoding.ASCII.GetBytes(sendStr);


                    s.SendTo(bs, enp);//杩斿洖瀹㈡埛绔垚鍔熶俊鎭?br />                     Console.Write("test :{0} times", i++);
                    //temp.Close();
                    s.Close();
                }
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException:{0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException:{0}", e);
            }
            Console.WriteLine("PressEntertoExit");
            Console.ReadLine();

        }

        /// <summary>
        /// 寮€濮嬬洃鍚?br />         /// </summary>
        public static void StartListen(string strIpAddress, string strPortNo)
        {
            byte[] data = new byte[1024];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(strIpAddress), int.Parse(strPortNo));
            newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            //缁戝畾缃戠粶鍦板潃
            newsock.Bind(ipep);
            newsock.ReceiveBufferSize = 1024 * 10000;

            //寰楀埌瀹㈡埛鏈篒P
            iPEndPoint = new IPEndPoint(IPAddress.Parse(strIpAddress), int.Parse(strPortNo));
            Remote = (EndPoint)(iPEndPoint);
        }

        /// <summary>
        /// 鎺ユ敹鏁版嵁
        /// </summary>
        /// <returns></returns>
        public static byte[] GetMessage()
        {
            byte[] data = new byte[4096];
            //鎺ュ彈淇℃伅    
            newsock.Receive(data, 0, 4096, SocketFlags.None);
            //int recv = newsock.ReceiveFrom(data, ref Remote);
            //return Encoding.ASCII.GetBytes(data, 0, recv);
            return data;
        }
    }
}


UDPClient

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace UDPClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                while (true)
                {
                    int port = 2001;
                    string host = "192.168.10.9";
                    IPAddress ip = IPAddress.Parse(host);
                    IPEndPoint ipe = new IPEndPoint(ip, port);//鎶奿p鍜岀鍙h浆鍖栦负IPEndPoint瀹炰緥
                    Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//鍒涘缓涓€涓猄ocket
                    //Console.WriteLine("Conneting...");
                    c.Connect(ipe);//杩炴帴鍒版湇鍔″櫒

                    Console.Write("Input sending message(q exit):");
                    string sendStr = Console.ReadLine();
                    if ("q".Equals(sendStr))
                    {
                        break;
                    }

                    EndPoint enp = (EndPoint)ipe;
                    byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                    c.SendTo(bs, enp);//鍙戦€佹祴璇曚俊鎭?br />                     string recvStr = "";
                    byte[] recvBytes = new byte[1024];
                    int bytes;

                    bytes = c.ReceiveFrom(recvBytes, ref enp);//浠庢湇鍔″櫒绔帴鍙楄繑鍥炰俊鎭?br />                     recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                    Console.WriteLine("Client Get Message:{0}", recvStr);//鏄剧ず鏈嶅姟鍣ㄨ繑鍥炰俊鎭?br />                     c.Close();
                }
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException:{0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException:{0}", e);
            }
            Console.WriteLine("PressEntertoExit");
            Console.ReadLine();
        }
    }
}

--------------------编程问答--------------------
寮曠敤 1 妤?nbsp;owen_0075 鐨勫洖澶?
Service
C# code

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDPServer
{
    class Program
    {

        private stati鈥︹€?/blockquote>

瀛︿範锛? --------------------编程问答-------------------- mark 鐗沊
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,