Foros del Web » Programación para mayores de 30 ;) » .NET »

[C#.net]Ayuda con socket auxiliar

Estas en el tema de [C#.net]Ayuda con socket auxiliar en el foro de .NET en Foros del Web. Hola, He estado creando un socket auxiliar y tengo 2 errores que no se como hacer para arreglarlos :S. Los dos Trheaders tienen que tener ...
  #1 (permalink)  
Antiguo 15/07/2010, 09:57
 
Fecha de Ingreso: julio-2009
Mensajes: 28
Antigüedad: 14 años, 9 meses
Puntos: 0
[C#.net]Ayuda con socket auxiliar

Hola,

He estado creando un socket auxiliar y tengo 2 errores que no se como hacer para arreglarlos :S.

Los dos Trheaders tienen que tener acceso a los mismos Strings

Todos los sockets tendran que tener su propio string de serverData + clientData
Source:

[SPOILER]
Código C:
Ver original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using System.Text;
  7.  
  8. namespace HabboTunel
  9. {
  10.     class Program
  11.     {
  12.         // BOOT
  13.         static void Main()
  14.         {
  15.             Console.Title = "Habbo Tunel by Timo, Edited by: Privilege & Jairo34";
  16.  
  17.             Console.WriteLine("This server has been coded by Timo");
  18.             Console.WriteLine("Edited by: Privilege, Jairo34");
  19.             Console.WriteLine("\n\n");
  20.             TcpListener serv = new TcpListener(IPAddress.Any, 8888);
  21.             serv.Start();
  22.             Console.WriteLine("[SOCKET] Server has been listened!");
  23.             while (true)
  24.             {
  25.                 if (serv.Pending())
  26.                 {
  27.                     TcpClient cl = serv.AcceptTcpClient();
  28.                    
  29.                     //Los dos Trheaders tienen que tener acceso a los mismos Strings, todos los sockets tendran que tener su propio string de serverData + clientData
  30.  
  31.                     /*
  32.                      * -- TODO: --
  33.                      *
  34.                      * serverData + clientData should be changeable
  35.                      * and readable on both Threads!
  36.                      * The both threaders must have access to the same strings
  37.                      * every sockets must have it's own string of ServerData + clientData
  38.                      *
  39.                      *  -- PARA-HACER: --
  40.                      *  
  41.                      * serverData + clientData deben ser cambiados
  42.                      * y legibles en los 2 Threads!
  43.                      * Los dos Trheaders tienen que tener acceso a los mismos Strings
  44.                      * todos los sockets tendran que tener su propio string de serverData + clientData
  45.                      *
  46.                      */
  47.  
  48.                     String serverData = "@" + Crypto.Encoding.encodeB64("@@" + cl.Client.RemoteEndPoint.ToString().Split(new char[] { ':' })[0]) + "@@" + cl.Client.RemoteEndPoint.ToString().Split(new char[] { ':' })[0];
  49.                     String clientData = "";
  50.  
  51.                     Console.WriteLine("[SOCKET][REQUEST]" + " Socket for " + cl.Client.RemoteEndPoint.ToString().Split(new char[] { ':' })[0] + " has been listened!");
  52.  
  53.  
  54.                     Thread th = new Thread(worker_server);
  55.                     th.Start();
  56.                     Thread th2 = new Thread(new ParameterizedThreadStart(worker_client));
  57.                     th2.Start(cl);
  58.                 }
  59.                 else
  60.                     Thread.Sleep(250);
  61.             }
  62.         }
  63.  
  64.         // SERVER
  65.         #region Server worker
  66.         static void worker_server()
  67.         {
  68.             try
  69.             {
  70.                 IPAddress ip = IPAddress.Parse("78.46.64.208");
  71.                 IPEndPoint ep = new IPEndPoint(ip, 30000);
  72.                 TcpClient conn = new TcpClient();
  73.                 conn.Connect(ep);
  74.  
  75.                 while (conn.Connected)
  76.                 {
  77.                     // RECEIVE
  78.                     if (conn.Available > 0)
  79.                     {
  80.                         byte[] buff = new byte[conn.Available];
  81.                         conn.GetStream().Read(buff, 0, conn.Available);
  82.                         conn.GetStream().Flush();
  83.                         String thisData = Encoding.ASCII.GetString(buff);
  84.                         if (thisData != "") { clientData = clientData + thisData; }
  85.                     }
  86.                     // RECEIVE
  87.  
  88.                     // SEND
  89.                     if (serverData != "" && clientData == "")
  90.                     {
  91.                         String sendData = serverData;
  92.                         serverData = "";
  93.                         byte[] buff = new byte[1024];
  94.                         buff = Encoding.ASCII.GetBytes(sendData);
  95.                         conn.GetStream().Write(buff, 0, buff.Length);
  96.                         Console.WriteLine("[SOCKET][SERVER] " + sendData.Replace(Convert.ToString(Convert.ToChar(0)), "{0}").Replace(Convert.ToString(Convert.ToChar(1)), "{1}").Replace(Convert.ToString(Convert.ToChar(2)), "{2}"));
  97.                     }
  98.                     // SEND
  99.  
  100.                     // SLEEP
  101.                     if (serverData == "" && clientData == "" && conn.Available == 0)
  102.                         Thread.Sleep(250);
  103.                     // SLEEP
  104.                 }
  105.                 conn.Close();
  106.             }
  107.             catch
  108.             { }
  109.         }
  110.         #endregion
  111.         //SERVER
  112.  
  113.         // CLIENT
  114.         #region Client worker
  115.         static void worker_client(object param)
  116.         {
  117.             try
  118.             {
  119.                 TcpClient cl = (TcpClient)param;
  120.                 while (cl.Connected)
  121.                 {
  122.                     // RECEIVE
  123.                     if (cl.Available > 0)
  124.                     {
  125.                         byte[] buff = new byte[cl.Available];
  126.                         cl.GetStream().Read(buff, 0, cl.Available);
  127.                         cl.GetStream().Flush();
  128.                         String thisData = Encoding.ASCII.GetString(buff);
  129.                         if (thisData != "") { serverData = serverData + thisData; }
  130.                     }
  131.                     // RECEIVE
  132.  
  133.                     // SEND
  134.                     if (clientData != "")
  135.                     {
  136.                         String sendData = clientData;
  137.                         clientData = "";
  138.                         byte[] buff = new byte[1024];
  139.                         buff = Encoding.ASCII.GetBytes(sendData);
  140.                         cl.GetStream().Write(buff, 0, buff.Length);
  141.                         Console.WriteLine("[SOCKET][CLIENT] " + sendData.Replace(Convert.ToString(Convert.ToChar(0)), "{0}").Replace(Convert.ToString(Convert.ToChar(1)), "{1}").Replace(Convert.ToString(Convert.ToChar(2)), "{2}"));
  142.                     }
  143.                     // SEND
  144.  
  145.                     // SLEEP
  146.                     if (serverData == "" && clientData == "" && cl.Available == 0)
  147.                         Thread.Sleep(250);
  148.                     // SLEEP
  149.                 }
  150.                 cl.Close();
  151.             }
  152.             catch { }
  153.         }
  154.         #endregion
  155.         // CLIENT
  156.     }
  157. }
  158.  
  159.  
  160.  
  161. // CRYPTO -- Big thanks to Jeax
  162. #region Encryption
  163. namespace Crypto
  164. {
  165.     public static class Encoding
  166.     {
  167.         #region Base64
  168.         public static string encodeB64(int value, int length)
  169.         {
  170.             string stack = "";
  171.             for (int x = 1; x <= length; x++)
  172.             {
  173.                 int offset = 6 * (length - x);
  174.                 byte val = (byte)(64 + (value >> offset & 0x3f));
  175.                 stack += (char)val;
  176.             }
  177.             return stack;
  178.         }
  179.         public static string encodeB64(string Val)
  180.         {
  181.             int value = Val.Length;
  182.             int length = 2;
  183.             string stack = "";
  184.             for (int x = 1; x <= length; x++)
  185.             {
  186.                 int offset = 6 * (length - x);
  187.                 byte val = (byte)(64 + (value >> offset & 0x3f));
  188.                 stack += (char)val;
  189.             }
  190.             return stack;
  191.         }
  192.         public static int decodeB64(string Val)
  193.         {
  194.             char[] val = Val.ToCharArray();
  195.             int intTot = 0;
  196.             int y = 0;
  197.             for (int x = (val.Length - 1); x >= 0; x--)
  198.             {
  199.                 int intTmp = (int)(byte)((val[x] - 64));
  200.                 if (y > 0)
  201.                 {
  202.                     intTmp = intTmp * (int)(Math.Pow(64, y));
  203.                 }
  204.                 intTot += intTmp;
  205.                 y++;
  206.             }
  207.             return intTot;
  208.         }
  209.         #endregion
  210.         #region VL64
  211.         public static string encodeVL64(int i)
  212.         {
  213.             byte[] wf = new byte[6];
  214.             int pos = 0;
  215.             int startPos = pos;
  216.             int bytes = 1;
  217.             int negativeMask = i >= 0 ? 0 : 4;
  218.             i = Math.Abs(i);
  219.             wf[pos++] = (byte)(64 + (i & 3));
  220.             for (i >>= 2; i != 0; i >>= 6)
  221.             {
  222.                 bytes++;
  223.                 wf[pos++] = (byte)(64 + (i & 0x3f));
  224.             }
  225.  
  226.             wf[startPos] = (byte)(wf[startPos] | bytes << 3 | negativeMask);
  227.  
  228.             System.Text.ASCIIEncoding encoder = new ASCIIEncoding();
  229.             string tmp = encoder.GetString(wf);
  230.             return tmp.Replace("\0", "");
  231.         }
  232.         public static int decodeVL64(string data)
  233.         {
  234.             return decodeVL64(data.ToCharArray());
  235.         }
  236.         public static int decodeVL64(char[] raw)
  237.         {
  238.             try
  239.             {
  240.                 int pos = 0;
  241.                 int v = 0;
  242.                 bool negative = (raw[pos] & 4) == 4;
  243.                 int totalBytes = raw[pos] >> 3 & 7;
  244.                 v = raw[pos] & 3;
  245.                 pos++;
  246.                 int shiftAmount = 2;
  247.                 for (int b = 1; b < totalBytes; b++)
  248.                 {
  249.                     v |= (raw[pos] & 0x3f) << shiftAmount;
  250.                     shiftAmount = 2 + 6 * b;
  251.                     pos++;
  252.                 }
  253.  
  254.                 if (negative)
  255.                     v *= -1;
  256.  
  257.                 return v;
  258.             }
  259.             catch
  260.             {
  261.                 return 0;
  262.             }
  263.         }
  264.         #endregion
  265.     }
  266. }
  267. #endregion
  268. // CRYPTO --  Big thanks to Jeax
[/SPOILER]
Gracias por tu ayuda
  #2 (permalink)  
Antiguo 17/07/2010, 04:50
 
Fecha de Ingreso: julio-2009
Mensajes: 28
Antigüedad: 14 años, 9 meses
Puntos: 0
Respuesta: [C#.net]Ayuda con socket auxiliar

¿Podrían ayudarme?
casi todos mis posts en este foro son para ayudar a la gente y por una vez que necesito ayuda no me responden como siempre...

Etiquetas: auxiliar, socket
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:14.