您可以检查此代码吗?

NETANEL.S

成员
已加入
2019年2月14日
留言内容
21
编程经验
3-5
C#:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Web.Mvc;
using System.Web.Razor;


namespace WindowsService1
{
    public class getter
    {
        void m()
        {
            
        }       
    }
// State object for reading client data asynchronously 
public class StateObject
{
    // Client  socket. 
    public Socket workSocket = null;
    // Size of receive buffer. 
    public const int BufferSize = 1024;
    // Receive buffer. 
    public byte[] buffer = new byte[BufferSize];
    // Received data string. 
    public StringBuilder sb = new StringBuilder();
}

public class AsynchronousSocketListener
{
    public static int myport = 5000;
    // Thread signal. 
    public static ManualResetEvent allDone = new ManualResetEvent(false);

    public AsynchronousSocketListener()
    {
    }
    public void listenter() {
      
    }
    public static void StartListening(byte[] ip,int port)
    {
        // Establish the local endpoint for the socket. 
        // The DNS name of the computer 
        // running the listener is "host.contoso.com".
        IPAddress ipAddress = new IPAddress(ip);
        try
        {
            AsynchronousSocketListener.StartListening(ip);
        }
        catch (Exception)
        {
            WindowsService1.mvc1.Controllers.HomeController H=new mvc1.Controllers.HomeController();
            AsynchronousSocketListener.StartListening(ip,H.port);
            
        }
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

        // Create a TCP/IP socket. 
        Socket listener = new Socket(ipAddress.AddressFamily,
            SocketType.Stream, ProtocolType.Tcp);

        // Bind the socket to the local endpoint and listen for incoming connections. 
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(100);

            while (true)
            {
                // Set the event to nonsignaled state. 
                allDone.Reset();

                // Start an asynchronous socket to listen for connections. 
                MessageBox.Show("Waiting for a connection...");
                listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    listener);

                // Wait until a connection is made before continuing. 
                allDone.WaitOne();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        System.Windows.Forms.MessageBox.Show("\nPress ENTER to continue...");

    }

    public static void StartListening(byte[] ip)
    {
        // Establish the local endpoint for the socket. 
        // The DNS name of the computer 
        // running the listener is "host.contoso.com".
        IPAddress ipAddress = new IPAddress(ip);

        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 5000);
        
        // Create a TCP/IP socket. 
        Socket listener = new Socket(ipAddress.AddressFamily,
            SocketType.Stream, ProtocolType.Tcp);

        // Bind the socket to the local endpoint and listen for incoming connections. 
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(100);

            while (true)
            {
                // Set the event to nonsignaled state. 
                allDone.Reset();

                // Start an asynchronous socket to listen for connections. 
                MessageBox.Show("Waiting for a connection...");
                listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    listener);

                // Wait until a connection is made before continuing. 
                allDone.WaitOne();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        System.Windows.Forms.MessageBox.Show("\nPress ENTER to continue...");

    }


    public static void AcceptCallback(IAsyncResult ar)
    {
        // Signal the main thread to continue. 
        allDone.Set();

        // Get the socket that handles the client request. 
        Socket listener = (Socket)ar.AsyncState;
        Socket handler = listener.EndAccept(ar);

        // Create the state object. 
        StateObject state = new StateObject();
        state.workSocket = handler;
        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReadCallback), state);
    }

    public static void ReadCallback(IAsyncResult ar)
    {
        String content = String.Empty;

        // Retrieve the state object and the handler socket 
        // from the asynchronous state object. 
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;
        string s = "";
        for (int i = 0; i < state.sb.Length; i++)
        {
            s = s + state.sb[i];
        }
        // Read data from the client socket.
        MessageBox.Show(DateTime.Now + "\t" + s + "\t");
        int bytesRead = handler.EndReceive(ar);


        while (bytesRead > 0)
        {
            //{
            // There  might be more data, so store the data received so far. 
            state.sb.Append(Encoding.ASCII.GetString(
                state.buffer, 0, bytesRead));

            // Check for end-of-file tag. If it is not there, read   
            // more data. 
            content = state.sb.ToString();
            if (content.IndexOf("<EOF>") > -1)
            {
                // All the data has been read from the   
                // client. Display it on the console..
                var x = string.Format("Read {0} bytes from socket. \n Data : {1}", content.Length, content);
                MessageBox.Show(x + "\n\n" + DateTime.UtcNow.ToLongDateString());

                // Echo the data back to the client. 




            }
        }
    }

        
          
        


     public void Main(int BARCODE)
    {
    
        addr_ A = new addr_();
    
     Model1 m=new Model1();
     var j=m.j;

     string IP = (from A1 in j where A1.id == BARCODE select A1.IP).ToString();
     var a=(byte)Convert.ToInt16(IP.Split('.')[0]);
     var b = (byte)Convert.ToInt16(IP.Split('.')[1]);
     var c = (byte)Convert.ToInt16(IP.Split('.')[2]);
     var d = (byte)Convert.ToInt16(IP.Split('.')[3]);
     var bytes = new byte[] { a, b, c, d };
     StartListening(bytes);


 }
    
 
  public static  void Main(String[] args)
     {
         try
         {
             StartListening(new byte[] { 127, 0, 0, 1 });       

         }
         catch (Exception)
         {
            
             throw;
         }
    }
}

}
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,556
地点
悉尼,澳大利亚
编程经验
10+
检查一下吗?那里有很多代码,要求我们不了解我们在寻找什么就读了很多。您甚至都没有告诉我们应该怎么做,那么我们应该如何知道它是否符合您的意图呢?请提供问题的完整解释。
 

NETANEL.S

成员
已加入
2019年2月14日
留言内容
21
编程经验
3-5
检查一下吗?那里有很多代码,要求我们不了解我们在寻找什么就读了很多。您甚至都没有告诉我们应该怎么做,那么我们应该如何知道它是否符合您的意图呢?请提供问题的完整解释。
我在这段代码中有这些问题:
  1. 听众中的问题
  2. 代码上的运行时错误
--
谢谢
内塔内尔
 
最佳 底部