123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- 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;
- }
- }
- }
- }
|