do you can to check this code?

NETANEL.S

Member
Joined
Feb 14, 2019
Messages
21
Programming Experience
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;
         }
    }
}

}
 
Check it for what? There's quite a bit of code there and asking us to read it with no idea what we're looking for is a bit much. You haven't even told us what it is supposed to do so how are we supposed to know whether it even does what you intend? Please provide a FULL and CLEAR explanation of the problem.
 
Check it for what? There's quite a bit of code there and asking us to read it with no idea what we're looking for is a bit much. You haven't even told us what it is supposed to do so how are we supposed to know whether it even does what you intend? Please provide a FULL and CLEAR explanation of the problem.
I have these problems in this code:
  1. problems in the listener
  2. runtime bugs on the code
--
thanks
netanel
 
Please be more specific. Provide a FULL and CLEAR explanation of what you're trying to achieve, where in the code you are having issues and exactly how the actual behaviour differs from what you expect.
 

Latest posts

Back
Top Bottom