using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace SocketToolbox { class SocketLinker { public static SocketLinker ins = null; public static SocketHandler CurrentHandler = null; private Socket listener; private int listen_port = 9000; public void Start() { // Create a TCP/IP socket. listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.100"), listen_port); // Bind the socket to the local endpoint and listen for incoming connections. listener.Bind(localEndPoint); listener.Listen(20); // Start an asynchronous socket to listen for connections. Console.WriteLine("Waiting for a connection..."); listener.BeginAccept( new AsyncCallback(AcceptCallback), listener); } public void AcceptCallback(IAsyncResult ar) { try { // Get the socket that handles the client request. Socket listener = (Socket)ar.AsyncState; listener.BeginAccept( new AsyncCallback(AcceptCallback), listener); Socket conn = listener.EndAccept(ar); IPEndPoint remoteIPE = (IPEndPoint)(conn.RemoteEndPoint); // Create the state object. SocketHandler handler = new SocketHandler(0, new IPEndPoint(remoteIPE.Address, 7)); handler.connection = conn; handler.StartReceive(); CurrentHandler = handler; } catch (Exception e) { Console.WriteLine(e.ToString()); } } } class SocketHandler { //laser id public int id { get; set; } public bool writeAvailable { get; set; } public bool linkAvailable { get; set; } //remote ipe private EndPoint remotePE { get; set; } // Client socket. public Socket remote { get; set; } public Socket connection { get; set; } // Size of receive buffer. private const int BufferSize = 512; // remote receive buffer private byte[] sendBuffer = new byte[BufferSize]; // connection receive buffer public byte[] receiveBuffer = new byte[BufferSize]; public SocketHandler(int id, EndPoint pe) { this.id = id; remotePE = pe; } public void StartReceive() { try { Receive(); remote = new Socket(remotePE.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. remote.BeginConnect(remotePE, new AsyncCallback(ConnectCallback), null); } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); } } public void StartSend() { try { remote = new Socket(remotePE.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. remote.BeginConnect(remotePE, new AsyncCallback(ConnectCallback), null); } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); } } public void Close() { if (connection != null) { connection.Close(); } if (remote != null) { remote.Close(); } } private void ConnectCallback(IAsyncResult ar) { try { // Complete the connection. remote.EndConnect(ar); Console.WriteLine("Socket connected to {0}", remote.RemoteEndPoint.ToString()); writeAvailable = true; } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); writeAvailable = false; } } private void Receive() { try { connection.BeginReceive(receiveBuffer, 0, BufferSize, 0, new AsyncCallback(ReceiveCallback), null); } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); linkAvailable = false; } } private void Send() { receiveBuffer.CopyTo(sendBuffer, 0); try { remote.BeginSend(sendBuffer, 0, sendBuffer.Length, 0, new AsyncCallback(SendCallback), null); } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); writeAvailable = false; } } public void Send(byte[] bs) { bs.CopyTo(sendBuffer, 0); try { remote.BeginSend(sendBuffer, 0, sendBuffer.Length, 0, new AsyncCallback(SendCallback), null); } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); writeAvailable = false; } } private void ReceiveCallback(IAsyncResult ar) { try { int bytesRead = connection.EndReceive(ar); //获得信息则再次监听 if (bytesRead > 0) { //Send(); connection.BeginReceive(receiveBuffer, 0, BufferSize, 0, new AsyncCallback(ReceiveCallback), null); } else { this.Close(); } } catch (Exception e) { Console.WriteLine("雷达" + id + "掉线\n" + e.ToString()); this.Close(); linkAvailable = false; } } private void SendCallback(IAsyncResult ar) { try { remote.EndSend(ar); } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); writeAvailable = false; } } } }