1. 程式人生 > >C#網編Console(二)

C#網編Console(二)

ipaddr 連接服務器 gen stream ole line port pen 發送

C#網編Console(二)

一、TCP

//服務端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets; //添加Socket類空間
using System.Net;
namespace Server_test
{
    class Program
    {
        private static byte[] result = new Byte[1024];
        private static int myport = 8012;
        static Socket serverSocket;
        static void Main(string[] args)
        {
            //服務器IP地址
            IPAddress ip = IPAddress.Parse("127.0.0.1");

            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            serverSocket.Bind(new IPEndPoint(ip, myport));

            serverSocket.Listen(10);

            Console.WriteLine("啟動監聽{0}", serverSocket.LocalEndPoint.ToString());

            //通過clientsocket發送數據
            string sendMessage = "server send Message Hello";

            Socket clientsocket = serverSocket.Accept();

            clientsocket.Send(Encoding.ASCII.GetBytes(sendMessage));

            Console.WriteLine("向客戶端發送消息:{0}", sendMessage);

            //通過clientsocket接收數據
            int receiveNumber = clientsocket.Receive(result);

            Console.WriteLine("接收客戶端{0}消息{1}", clientsocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));

            clientsocket.Shutdown(SocketShutdown.Both);

            clientsocket.Close();

            Console.ReadLine();
        }
    }
}
//客戶端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets; //添加Socket類空間
using System.Net;
namespace Client_test
{
    class Program
    {
        private static byte[] result = new Byte[1024];

        static void Main(string[] args)
        {
            //服務器IP地址
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                clientSocket.Connect(new IPEndPoint(ip, 8012));
                Console.WriteLine("連接服務器成功");
            }
            catch
            {
                Console.WriteLine("連接服務失敗,請按回車鍵退出");
                return;
            }
            //通過clientSocket接收數據
            int receiveLength = clientSocket.Receive(result);

            Console.WriteLine("接收服務器消息:{0}", Encoding.ASCII.GetString(result, 0, receiveLength));

            //通過clientSocket發送數據
            string sendMessage = "client send Message Hello";

            clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage));

            Console.WriteLine("向服務器發送消息:{0}", sendMessage);

            clientSocket.Shutdown(SocketShutdown.Both);

            clientSocket.Close();

            Console.ReadLine();
        }
    }
}

技術分享圖片

二、UDP

//發送方
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;   //add net namespace
using System.Net.Sockets;

namespace Send_Test
{
    class Program
    {
        private static int remoteReceivePort = 8012;
        static void Main(string[] args)
        {
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            //發送方:發送數據
            Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sendSocket.SendTo(Encoding.UTF8.GetBytes("測試數據"), new IPEndPoint(ip, remoteReceivePort));
            Console.WriteLine("發送測試數據");
            sendSocket.Shutdown(SocketShutdown.Send);
            sendSocket.Close();
            Console.ReadLine();
        }
    }
}
//接收方
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;   //add net namespace
using System.Net.Sockets;
namespace Recv_Test
{
    class Program
    {
        private static int receivePort = 8012;
        static void Main(string[] args)
        {
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            //接收準備
            Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            receiveSocket.Bind(new IPEndPoint(ip, receivePort));
            //接收數據
            byte[] result = new Byte[1024];
            EndPoint senderRemote = (EndPoint)(new IPEndPoint(IPAddress.Any, 0));
            //引用類型參數為EndPoint類型,用於存放發送方的IP地址和端點
            int length = receiveSocket.ReceiveFrom(result, ref senderRemote);
            Console.WriteLine("接收到{0}消息:{1}", senderRemote.ToString(), Encoding.UTF8.GetString(result, 0, length).Trim());
            receiveSocket.Shutdown(SocketShutdown.Receive);
            Console.ReadLine();
        }
    }
}

技術分享圖片

技術分享圖片

C#網編Console(二)