Frage zu Async Sockets



  • Hallo!

    Ich versuche mich zurzeit in Async Sockets einzuarbeiten, zurzeit stehe ich beim folgenden problem, mein Paket das ich vershicken möchte wird irgendwie mehrmals vershickt, obwohl ich die Send() fkt nur einmal aufrufe, hier der code:

    BinaryWriter writer = new BinaryWriter(new MemoryStream(loginOutBuffer, loginOutBufferIndex, 8192 - loginOutBufferIndex));
                            writer.Write((ushort)0);
                            writer.Write((ushort)0x6100);
                            writer.Write((ushort)0);
                            writer.Write((byte)18);
                            writer.Write((ushort)9);
                            writer.Write("SR_Client".ToCharArray());
                            writer.Write((uint)203);
                            long offset = writer.BaseStream.Position;
                            writer.Seek(0, SeekOrigin.Begin);
                            writer.Write((ushort)(offset - 6));
                            loginOutBufferIndex += (int)offset;
                            Client.Send(loginOutBuffer);
                            Array.Clear(loginOutBuffer, 0, loginOutBuffer.Length);
                            loginOutBufferIndex = 0;
    

    Bin für jede hilfe dankbar.



  • Setze ein Breackpoint an Client.Send und schau wie oft Du dort landest.



  • Knuddlbaer schrieb:

    Setze ein Breackpoint an Client.Send und schau wie oft Du dort landest.

    Ok, hab gemacht. Der Breakpoint wurde nur einmal ausgelöst. Hier der ganze code, ja ich weiß das man es schöner machen kann, wollte nur hinkriegen dass es endlich funktioniert. Weiß echt nicht was ich falsch mache. Gruß. 🤡

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Net.Sockets;
    using System.Windows.Forms;
    using System.IO;
    using System.Net;
    
        public class NetworkInterface
        {
            private Socket Client;
            private byte[] myBuffer = new byte[8192];
            private int myBufferIndex = 0;
    
            private byte[] workBuffer = new byte[8192];
            private byte[] loginOutBuffer = new byte[8192];
            private int loginOutBufferIndex = 0;
    
            public void Initialize()
            {
                try
                {
    
    				if( Client != null && Client.Connected )
    				{
    					Client.Shutdown( SocketShutdown.Both );
    					Client.Close();
    				}
    
    				Client = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
    
                    IPEndPoint ProxySrv = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 16000);
    
    				Client.Blocking = false;
    				AsyncCallback onconnect = new AsyncCallback( OnConnect );
    
    				Client.BeginConnect( ProxySrv, onconnect, Client );
    			}
    			catch( Exception ex )
    			{
                    // MainDialog.myDialog.sendText(ex.ToString());
                    //MainDialog.myDialog.sendText("Failed to connect.");
    			}
            }
    
            public void OnConnect(IAsyncResult ar)
            {
    
                Socket sock = (Socket)ar.AsyncState;
    
                try
                {
                    if (sock.Connected)
                    {
                        SetupRecieveCallback(sock);
                    }
                    else
                    {
                    }
                        //MainDialog.myDialog.sendText("Unable to connect to proxy.");
                }
                catch (Exception ex)
                {
                    //MainDialog.myDialog.sendText(ex.ToString());
                    //MainDialog.myDialog.sendText("Something really bad happened.");
                }
            }
    
            public void RecvDataCallback(IAsyncResult ar)
            {
    
                Socket sock = (Socket)ar.AsyncState;
    
                try
                {
                    int byteRecv = sock.EndReceive(ar);
                    if (byteRecv == 0)
                    {
                        sock.Shutdown(SocketShutdown.Both);
                        sock.Close();
                    }
                    else
                    {
                        myBufferIndex += byteRecv;
                        while (myBufferIndex > 2)
                        {
                            MemoryStream stm = new MemoryStream(myBuffer, 0, myBufferIndex);
                            BinaryReader rdr = new BinaryReader(stm);
                            ushort packetSize = rdr.ReadUInt16();
                            ushort packetOpcode = rdr.ReadUInt16();
                            ushort packetSecurity = rdr.ReadUInt16();
                            if (packetSize + 6 <= myBufferIndex)
                            {
                                rdr.Read(workBuffer, 0, packetSize);
                                MemoryStream stm2 = new MemoryStream(workBuffer, 0, packetSize);
                                BinaryReader rdr2 = new BinaryReader(stm2);
                                OnPacketRecv(packetOpcode, packetSize, packetSecurity, rdr2);
                                Buffer.BlockCopy(myBuffer, packetSize + 6, myBuffer, 0, myBufferIndex - (packetSize + 6));
                                myBufferIndex -= (packetSize + 6);
                            }
                            else
                            {
                                break;
                            }
                        }
                        SetupRecieveCallback(sock);
    
                    }
                }
                catch (Exception ex)
                {
                    //MainDialog.myDialog.sendText(ex.ToString());
                    //MainDialog.myDialog.sendText("Something really bad happened(RecvDataCallback).");
                }
            }
            public void SendPacket(byte[] buff)
            {
                Client.BeginSend(buff, 0, buff.Length, SocketFlags.None,
                             new AsyncCallback(SendData), Client);
            }
    
            void SendData(IAsyncResult iar)
            {
                Socket remote = (Socket)iar.AsyncState;
                int sent = remote.EndSend(iar);
                remote.BeginReceive(myBuffer, 0, myBuffer.Length, SocketFlags.None,
                              new AsyncCallback(RecvDataCallback), remote);
    
            }
    
            public void OnPacketRecv(ushort opCode, ushort packetSize, ushort packetSec, BinaryReader readPacket)
            {
                string fool = opCode.ToString();
                //MainDialog.myDialog.sendText(fool);
                switch ( opCode )
                {
                    case 0x2001:
                    {
                        //MainDialog.myDialog.sendText("0x2001:");
                        UInt16 srvLen = readPacket.ReadUInt16();
                        char[] foos = readPacket.ReadChars(srvLen);
                        string srvName = new string(foos);            
    
                        if ( srvName == "GatewayServer")
                        {
                            // MainDialog.myDialog.sendText("Connected to:" +  srvName);
    
                            BinaryWriter writer = new BinaryWriter(new MemoryStream(loginOutBuffer, loginOutBufferIndex, 8192 - loginOutBufferIndex));
                            writer.Write((ushort)0);
                            writer.Write((ushort)0x6100);
                            writer.Write((ushort)0);
                            writer.Write((byte)18);
                            writer.Write((ushort)9);
                            writer.Write("SR_Client".ToCharArray());
                            writer.Write((uint)203);
                            long offset = writer.BaseStream.Position;
                            writer.Seek(0, SeekOrigin.Begin);
                            writer.Write((ushort)(offset - 6));
                            loginOutBufferIndex += (int)offset;
                            //Client.Send(loginOutBuffer);
                            SendPacket(loginOutBuffer);
    
                            Array.Clear(loginOutBuffer, 0, loginOutBuffer.Length);
                            loginOutBufferIndex = 0;
    
                        }
    
                    } break;
    
                }
            }
            public void SetupRecieveCallback(Socket sock)
            {
                try
                {
                    AsyncCallback recieveData = new AsyncCallback(RecvDataCallback);
                    sock.BeginReceive(myBuffer, 0, myBuffer.Length, SocketFlags.None, recieveData, sock);
                }
                catch (Exception ex)
                {
                    //MainDialog.myDialog.sendText(ex.ToString());
                    // MainDialog.myDialog.sendText("Setup receive callback failed.");
                }
            }
    
        }
    


  • omg, Ich idiot.

    Client.BeginSend(buff, 0, buff.length, SocketFlags.None,
                             new AsyncCallback(SendData), Client);
    

    Hat 8192 bytes an den server geshickt.. problem gelöst, danke dir dennoch!



  • Paul advised us to be ever hopeful. ,


Anmelden zum Antworten