Autor Beitrag
VampireSilence
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 109
Erhaltene Danke: 5


C# (VS 2008 Express), PHP/MySQL, Windows XP
BeitragVerfasst: So 07.08.11 16:58 
Ich habe hier gerade einen Fehler bekommen und ich habe keinen blassen Schimmer, was ich damit anfangen soll. Er zeigt auch auf keine Codepassage, was das Verstehen nochmal schwieriger macht. Er lautet:
ausblenden Quelltext
1:
Beim Vorgang zum Rückgängigmachen wurde ein Kontext gefunden, der sich vom Kontext des entsprechenden Set-Vorgangs unterschied. Möglicherweise war der Kontext für den Thread "Set" und wurde nicht zurückgesetzt (rückgängig gemacht).					

Dazu sollte ich noch sagen, dass ich garnichts Rückgängigmachen möchte (und auch nicht tue) und ich auch garkeinen Thread mit dem Namen "Set" erstellt habe (geschweige denn sonst irgendeinen Thread, abgesehen von Hauptprogramm), deshalb ist das Ganze für mich umso rätselhafter.

Könnt ihr euch vllt einen Reim darauf machen ?

mfg
- VampireSilence


Moderiert von user profile iconKha: Topic aus C# - Die Sprache verschoben am So 07.08.2011 um 17:50
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 07.08.11 17:29 
Benutzt du vielleicht Netzwerkkomponenten oder andere, die intern einen Thread benutzen könnten?

Wenn ja, wie sieht dazu der Code aus?
VampireSilence Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 109
Erhaltene Danke: 5


C# (VS 2008 Express), PHP/MySQL, Windows XP
BeitragVerfasst: So 07.08.11 19:10 
Ja, in der Tat benutze ich die, mir war nur nicht ganz klar, dass die einfach neue Threads öffnen.

Also mein Server-Code sieht so aus:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
        static private TcpListener server = null;

        static public bool Connected
        {
            get
            {
                if (server == null)
                {
                    return false;
                }

                return server.Server.IsBound;
            }
        }

        static private bool active = false;
        static private int port = 9014;

        private static byte[] passBytes = new byte[0];
        public static string password
        {
            get
            {
                return Encoding.UTF8.GetString(passBytes);
            }

            set
            {
                passBytes = Encoding.UTF8.GetBytes(value);
            }
        }

        static List<Socket> clients = new List<Socket>();

        private const int BUFSIZE = 512;
        private const int BACKLOG = 5;

        public static void Listen(int servPort)
        {
            if (server != null)
            {
                server.Stop();
            }

            active = true;
            port = servPort;

            server = new TcpListener(IPAddress.Any, servPort);
            try
            {
                server.Start();
            }
            catch { server.Stop(); return; }
            server.BeginAcceptSocket(new AsyncCallback(Accept_Callback), server);
        }

        public static void Listen(int servPort, string Password)
        {
            active = true;
            port = servPort;
            password = Password;

            TcpListener tcp = new TcpListener(IPAddress.Any, servPort);
            tcp.Start();
            tcp.BeginAcceptSocket(new AsyncCallback(Accept_Callback), tcp);
        }

        private static void Accept_Callback(IAsyncResult ar)
        {
            if (!active)
            {
                return;
            }

            TcpListener tcp = (TcpListener)ar.AsyncState;

            Socket client;
            try
            {
                client = tcp.EndAcceptSocket(ar);
            }
            catch 
            {
                return;
            }

            byte[] rcvBuffer = new byte[BUFSIZE];
            int bytesRcvd;

            Debug.Print("Handling client at " + client.RemoteEndPoint + " - ");

            int totalbytes = 0;
            if ((bytesRcvd = client.Receive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None)) > 0)
            {
                totalbytes += bytesRcvd;
            }
            Debug.Print("received {0} bytes.", totalbytes);

            Array.Resize<byte>(ref rcvBuffer, totalbytes);

            if (Encoding.UTF8.GetString(rcvBuffer) == Encoding.UTF8.GetString(passBytes))
            {
                clients.Add(client);

                Debug.Print("password was correct.");
            }
            else
            {
                client.Send(new byte[] { 2 });
                client.Shutdown(SocketShutdown.Both);
                client.Close();

                Debug.Print("password was incorrect.");
            }

            Listen(port);
        }

        static public void Close()
        {
            active = false;
            server.Stop();

            for (int i = 0; i < clients.Count; i++)
            {
                Socket temp = clients[i];
                temp.Shutdown(SocketShutdown.Both);
                temp.Close();
                clients[i] = temp;
            }
        }


Und mein Client-Code so:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
        private static TcpClient client = new TcpClient();

        static public bool Connected
        {
            get
            {
                return client.Connected;
            }
        }

        private static byte[] rcvBuffer = new byte[2048];

        public static void Connect(string hostname, int port, string password)
        {
            if (client.Connected)
            {
                client.Close();
            }

            client = new TcpClient();
            client.SendBufferSize = BUFSIZE;
            client.SendTimeout = (2000);

            try
            {
                client.Connect(hostname, port);
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
                MessageBox.Show("Es konnte keine Verbindung hergestellt werden.");
                return;
            }

            byte[] passByte = Encoding.UTF8.GetBytes(password);

            NetworkStream netStream = client.GetStream();
            netStream.Write(passByte, 0, passByte.Length);
            netStream.Flush();
            netStream.Write(new byte[0], 00);
            netStream.Flush();

            ReceiveData();
        }

        public static void ReceiveData()
        {
            rcvBuffer = new byte[2048];
            client.Client.BeginReceive(rcvBuffer, 02048, SocketFlags.None, new AsyncCallback(Receive_Callback), client);
        }

        public static void Receive_Callback(IAsyncResult ar)
        {
            TcpClient acl = (TcpClient)ar.AsyncState;

            try
            {
                Array.Resize<byte>(ref rcvBuffer, acl.Client.EndReceive(ar));
            }
            catch
            {
                return;
            }

            string message = Encoding.UTF8.GetString(rcvBuffer);
            Debug.Print(message);

            ReceiveData();
        }

        public static void Close()
        {
            client.Close();
        }


Das Verbinden klappt soweit auch noch, nur wenn Daten ankommen, erhalte ich dann diesen Fehler.

mfg
- VampireSilence
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 07.08.11 20:57 
user profile iconVampireSilence hat folgendes geschrieben Zum zitierten Posting springen:
Ja, in der Tat benutze ich die, mir war nur nicht ganz klar, dass die einfach neue Threads öffnen.
Nicht nur die, auch du machst doch explizit einen neuen Thread auf...
user profile iconVampireSilence hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
            server.BeginAcceptSocket(new AsyncCallback(Accept_Callback), server);
        }

        private static void Accept_Callback(IAsyncResult ar)
        {
Du bist in Accept_Callback also auch in einem separaten Thread, denn genau das macht AsyncCallback ja, siehe Doku.
msdn.microsoft.com/e...m.asynccallback.aspx hat folgendes geschrieben:
Use an AsyncCallback delegate to process the results of an asynchronous operation in a separate thread.
VampireSilence Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 109
Erhaltene Danke: 5


C# (VS 2008 Express), PHP/MySQL, Windows XP
BeitragVerfasst: So 07.08.11 21:11 
Naja, ich schlage nicht jeden Befehl nach, den ich eingebe, sonst bin ich ja in 100 Jahren noch nicht fertig. ^^

In soweit machts jetzt zumindest schonmal Sinn. Was ich aber nach wie vor nicht mache, ist Etwas rückgängig zu machen. Ich meine, du siehst den Code ja nun. Was könnte diesen Fehler hervorrufen ?

mfg
- VampireSilence
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 07.08.11 21:39 
Du greifst unsynchronisiert auf die globale Liste clients zu, um den neuen Client hinzuzufügen. Ich schätze mal das ist das Problem.

Alternativ bliebe noch Debug.Print, bei dem ich schlicht nicht weiß, wie es mit Threads umgeht.