Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Codename!! en 26 Febrero 2010, 14:36 pm



Título: Problema de variables¿?
Publicado por: Codename!! en 26 Febrero 2010, 14:36 pm
Buenas! estoy intentado hacer una conexion a traves de TcpListener y Client, el problema esta en que al compilarlo me salta un error en Program.cs en la linea
Código:
Application.Run(new Form1());
He probado a quitar las variables  publicas y estaticas de arriba pero si las pongo en otros sitios ya no serian accesibles para los demas metodos que necesito hacer, que puedo hacer? que es lo que estoy haciendo mal??
Código:
using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace ServerTest
{
    public partial class Form1 : Form
    {
        public static IPAddress IP = IPAddress.Loopback;
        public static TcpListener Listener = new TcpListener(IP, 22222);
        public static Socket s = Listener.AcceptSocket();

        public Form1()
        {
            InitializeComponent();
        }

       public void inicio()
        {
            try
            {
                //////sección ESCUCHA///////
                Listener.Start();
                //////CONEXION///////
                MessageBox.Show("Conexion establecida con " + s.RemoteEndPoint);
                /////RECEPCION/////////
                byte[] buffer = new byte[100];
                int bufferAux = s.Receive(buffer);
                for (int i = 0; i < bufferAux; i++)
                {
                    Convert.ToChar(buffer[i]);
                }
                /////ENVIO//////////
                ASCIIEncoding codificacionEnvio = new ASCIIEncoding();
                s.Send(codificacionEnvio.GetBytes(("test envio")));
            }
            catch
            {
                MessageBox.Show("Error de algun tipo ");
               s.Close();
               Listener.Stop();
               inicio();
               
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            inicio();
        }
    }
}


Título: Re: Problema de variables¿?
Publicado por: [D4N93R] en 26 Febrero 2010, 20:13 pm
Cual es el error que te da.?


Título: Re: Problema de variables¿?
Publicado por: MANULOMM en 26 Febrero 2010, 22:53 pm
no estoy muy seguro de que esto pueda hacerse en ese punto.

Código:
public static Socket s = Listener.AcceptSocket();

haz esto:

Código:
public static Socket s = null;

//y en el constructor
        public Form1()
        {
            InitializeComponent();
            s = Listener.AcceptSocket();
        }

Atentamente,

Juan Manuel Lombana
Medellín - Colombia


Título: Re: Problema de variables¿?
Publicado por: 43H4FH44H45H4CH49H56H45H en 27 Febrero 2010, 02:45 am
El code puede funcionar de esta manera:

Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.Net.Sockets;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.    public partial class Form1 : Form
  15.    {
  16.        public static IPAddress IP;
  17.        public static TcpListener Listener;
  18.        public static Socket s;
  19.  
  20.        public Form1()
  21.        {
  22.            InitializeComponent();
  23.        }
  24.        public void inicio()
  25.        {
  26.            try
  27.            {
  28.                Listener = null;
  29.                IP = IPAddress.Loopback;
  30.                Listener = new TcpListener(IP, 22222);
  31.                //////sección ESCUCHA///////
  32.                Listener.Start();
  33.                s = Listener.AcceptSocket();
  34.                //////CONEXION///////
  35.                MessageBox.Show("Conexion establecida con " + s.RemoteEndPoint);
  36.                /////RECEPCION/////////
  37.                byte[] buffer = new byte[100];
  38.                int bufferAux = s.Receive(buffer);
  39.                for (int i = 0; i < bufferAux; i++)
  40.                {
  41.                    Convert.ToChar(buffer[i]);
  42.                }
  43.                /////ENVIO//////////
  44.                ASCIIEncoding codificacionEnvio = new ASCIIEncoding();
  45.                s.Send(codificacionEnvio.GetBytes(("test envio")));
  46.                s.Close();
  47.                Listener.Stop();
  48.            }
  49.            catch
  50.            {
  51.                MessageBox.Show("Error de algun tipo ");
  52.                s.Close();
  53.                Listener.Stop();
  54.                inicio();
  55.  
  56.            }
  57.  
  58.        }
  59.  
  60.        private void Form1_Load(object sender, EventArgs e)
  61.        {
  62.            inicio();
  63.        }
  64.    }
  65. }
  66.  

pero esta mal planteado a mi criterio, fijate los ejemplos de MSDN para ubicarte mejor.


Título: Re: Problema de variables¿?
Publicado por: Codename!! en 27 Febrero 2010, 18:52 pm
Asi es :)

muchas gracias, estoy comenzando con c# y eso era un error de base!  :-\

Gracias por vuestras respuestas!