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(); } } }