using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks;
namespace Socket_Tcp { class Program { static void Main(string[] args) { //服務器端 //1.創建socket Socket tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//綁定ip地址和端口號(每個軟件都會有一個端口號) IPAddress ipAddress = new IPAddress(new byte[] {192,168,101,8 }); //IP地址可以用ipconfig查看一下 EndPoint endPoint = new IPEndPoint(ipAddress,2333); //IPEndPoint對端口號和ip地址進行了封裝,2333是隨便寫的一個端口號 tcpServer.Bind(endPoint);
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks;
namespace Socket_Tcp_Client { class Program { static void Main(string[] args) { //客戶端 //創建Socket Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//發起建立連接的請求 IPAddress ipAddress = IPAddress.Parse("192.168.101.8"); EndPoint endPoint = new IPEndPoint(ipAddress, 2333); tcpClient.Connect(endPoint); // 客戶端不需要監聽,但是要和服務器連接,且Ip地址和端口號要和服務器保持一致 byte[] date = new byte[1024]; int length = tcpClient.Receive(date); // 用date存儲接收過來的數據,長度是length
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks;
namespace udp_server { class Program { private static Socket udpServer; static void Main(string[] args) { //創建Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//接收數據 new Thread(ReceiveMessage) { IsBackground = true }.Start();//設置成后台線程,因為程序運行結束,也就不需要這個線程了。
udpServer.Close(); Console.ReadKey(); }
private static void ReceiveMessage() { while (true) { EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] data = new byte[1024]; int length = udpServer.ReceiveFrom(data, ref remoteEndPoint);
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks;
namespace udp_socket_Client { class Program { static void Main(string[] args) { Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram , ProtocolType.Udp); //發送數據 while (true) { EndPoint serverPoint = new IPEndPoint(IPAddress.Parse("192.168.101.8"), 2333)); string message = Console.ReadLine(); byte[] data = Encoding.UTF8.GetBytes(message); udpClient.SendTo(data, serverPoint); }
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks;
namespace Tcplistener_Server { class Program { static void Main(string[] args) { //1.對socket進行封裝,tcpListener內本身包含套接字 TcpListener listener = new TcpListener(IPAddress.Parse("192.168.101.8"),2223);
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks;
namespace _UdpListener { class Program { static void Main(string[] args) { UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.101.8"),2223));
//接收數據 IPEndPoint point = new IPEndPoint(IPAddress.Any, 0); byte[] data = udpClient.Receive(ref point);//通過point確定數據來自哪個ip和端口號,並返回接收的數據 string message = Encoding.UTF8.GetString(data); Console.WriteLine("收到數據: " + message); udpClient.Close(); Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks;
namespace _udpListenerClient { class Program { static void Main(string[] args) { UdpClient client = new UdpClient(); string message = Console.ReadLine(); byte[] data = Encoding.UTF8.GetBytes(message);
client.Send(data, data.Length, new IPEndPoint(IPAddress.Parse("192.169.101.8"), 2333)); client.Close(); Console.ReadKey(); } } }