ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
603
603
EMPFEHLEN
-
Hallo,
ich habe folgendes Problem, ich stelle eine Verbindung zwischen einem Server und einem bzw. es können auch mehrere Clients sein. Es handelt sich um einen einfachen Chat über Sockets nun die Verbindungen und sowas habe ich hinbekommen und es werden auch Nachrichten über den Server an alle Clients weitergekeitet nur nach ca. 20 Sekunden empfängt der Server zwar noch die Nachrichten aber leitet diese nicht weiter und ich habe keine idee warum hab auch schon nachgeforscht hänge jetzt seit 3 Tagen an dem Problem ich hoffe ihr könnt mir helfen.
Gruß Otianer.
P.S.
der Quellcode meiner Forms sowie der Klassen:
Server.cs:
Code :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
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace ClientServer { public partial class frmServer : Form { public frmServer ( ) { InitializeComponent ( ); mainThread = new Thread ( new ThreadStart ( this.mainListener ) ); Form.CheckForIllegalCrossThreadCalls = false; } public const int listenPort = 10000; public const int sleepTime = 200; public IPAddress ipAdress = IPAddress.Any; private Thread mainThread; public const int maxServerConnections = 100; public List<Socket> clients2 = new List<Socket> ( ); private void mainListener ( ) { TcpListener listener = new TcpListener ( ipAdress, listenPort ); lbxMessages.Items.Add ( "Horche auf Port : " + listenPort.ToString ( ) ); try { listener.Start ( ); while ( true ) { while ( !listener.Pending ( ) ) { Thread.Sleep ( sleepTime ); } Socket socket = listener.AcceptSocket ( ); lbxClents.Items.Add ( socket.RemoteEndPoint ); lbxMessages.Items.Add ( "Neue Client-Verbindung (" + "IP: " + socket.RemoteEndPoint + ", " + "Port " + ( ( IPEndPoint ) socket.LocalEndPoint ).Port.ToString ( ) + ")" ); ServerInstanz newConn = new ServerInstanz ( socket, this); clients2.Add ( socket ); } } catch ( Exception ex ) { throw new Exception ( "Fehler be Verbindung", ex ); } } private void cmdStart_Click ( object sender, EventArgs e ) { if ( mainThread.ThreadState != ThreadState.Running) { mainThread.Start ( ); } } private void cmdStoppen_Click ( object sender, EventArgs e ) { if ( mainThread.ThreadState == ThreadState.Running ) { mainThread.Suspend(); } } } }
ServerInstanz.cs:
Code :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
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; namespace ClientServer { class ServerInstanz { const int SleepTime = 200; public Thread serverThread; public Socket socket; private int BufferSize = 10240; frmServer serv; public ServerInstanz(Socket socket, frmServer serv) { this.socket = socket; this.serv = serv; serverThread = new Thread ( new ThreadStart ( Process ) ); serverThread.Start ( ); } public void Process ( ) { try { MemoryStream mem = new MemoryStream ( );// Empfangspuffer byte [ ] buffer = new byte [ BufferSize ]; while ( true ) { mem.Seek ( 0, SeekOrigin.Begin ); mem.SetLength ( 0 ); while ( socket.Available > 0 ) { //Byte[] buffer = new byte[bytesAvailable]; int bytesRead = socket.Receive ( buffer, buffer.Length, SocketFlags.None ); if ( bytesRead <= 0 ) continue; mem.Write ( buffer, 0, bytesRead ); // Alles zurücksetzen } if ( mem.Length > 0 ) { if ( mem.Length == 4 ) if ( System.Text.Encoding.ASCII.GetString ( mem.ToArray ( ), 0, 4 ) == "quit" ) { for ( int i = 0; i < serv.lbxClents.Items.Count; i++ ) { if ( serv.lbxClents.Items [ i ].ToString ( ) == socket.RemoteEndPoint.ToString ( ) ) { serv.lbxClents.Items.RemoveAt ( i ); } } for ( int i = 0; i < serv.clients2.Count; i++ ) { if ( serv.clients2 [ i ] == socket ) { serv.clients2.RemoveAt ( i ); } } serv.lbxMessages.Items.Add ( "Client-Verbindung getrennt(" + "IP: " + socket.RemoteEndPoint + ", " + "Port " + ( ( IPEndPoint ) socket.LocalEndPoint ).Port.ToString ( ) + ")" ); socket.Close ( ); socket = null; serverThread.Abort ( ); } serv.lbxMessages.Items.Add ( System.Text.Encoding.ASCII.GetString ( mem.ToArray ( ) ) ); Socket [ ] array = serv.clients2.ToArray ( ); for ( int i = 0; i < serv.clients2.Count; i++ ) { array [ i ].Send ( mem.ToArray ( ), SocketFlags.None ); serv.clients2 [ i ].Send ( mem.ToArray ( ) ); } mem.Seek ( 0, SeekOrigin.Begin ); mem.SetLength ( 0 ); } } } catch { System.Console.WriteLine ( "Verbindung zum Client beendet" ); } } } }
Client.cs:
Code :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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Security; using System.Security.Permissions; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; namespace Client { public partial class frmClient : Form { public frmClient ( ) { InitializeComponent ( ); Form.CheckForIllegalCrossThreadCalls = false; } Thread recive; private int SleepTime = 100; private int TimeOut = 5000; byte [ ] bytes; private bool receiving = false; public Socket socket = null; private int BufferSize = 10240; public void receive ( ) { try { int cnt = 0; receiving = true; MemoryStream mem = new MemoryStream ( );// Empfangspuffer byte [ ] buffer = new byte [ BufferSize ]; while ( cnt < ( TimeOut / SleepTime ) ) { while ( socket.Available > 0 ) { int bytesRead = socket.Receive ( buffer, buffer.Length, SocketFlags.None ); if ( bytesRead <= 0 ) continue; mem.Write ( buffer, 0, bytesRead ); } Thread.Sleep ( SleepTime ); if ( mem.Length > 0 && socket.Available == 0 ) { //Console.WriteLine("Client: {0} bytes received",mem.Length); receiving = false; bytes = mem.ToArray ( ); lbxEingang.Items.Add ( System.Text.Encoding.ASCII.GetString ( mem.ToArray ( ) ) ); } else { cnt++; } mem.Seek ( 0, SeekOrigin.Begin ); mem.SetLength ( 0 ); } receiving = false; bytes = null; } catch { receiving = false; bytes = null; } } public void client(String Address,int port) { try { IPHostEntry hostInfo = Dns.GetHostByName(Address); System.Net.IPEndPoint ep = new System.Net.IPEndPoint(hostInfo.AddressList[0],port); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); socket.Connect(ep); } catch (SecurityException ex) { throw new Exception("Fehler beim Herstellen der Verbindung zum Server, evtl. verursacht durch eine Firewall oder ähnliche Schutzmechanismen",ex); } catch (Exception ex) { throw new Exception("Fehler beim Herstellen der Verbindung zum Server",ex); } } private void frmClient_Load ( object sender, EventArgs e ) { } private void cmdVerbinden_Click ( object sender, EventArgs e ) { if ( txbIP.Text != "" && txbPort.Text != "" ) { client ( txbIP.Text, Convert.ToInt32 ( txbPort.Text ) ); recive = new Thread ( new ThreadStart ( receive ) ); recive.Start ( ); txbPort.Visible = false; txbIP.Visible = false; cmdVerbinden.Visible = false; cmdSenden.Visible = true; txbText.Visible = true; cmdTrennen.Visible = true; lbxEingang.Visible = true; } } private void cmdSenden_Click ( object sender, EventArgs e ) { if ( socket != null && txbText.Text != "" ) { socket.Send ( System.Text.Encoding.ASCII.GetBytes ( txbText.Text ) ); txbText.Text = ""; } } private void cmdTrennen_Click ( object sender, EventArgs e ) { if ( socket != null ) { socket.Send ( System.Text.Encoding.ASCII.GetBytes ( "quit" ) ); socket.Close ( ); recive.Join ( 10000 ); recive.Interrupt ( ); recive = null; socket = null; txbPort.Visible = true; txbIP.Visible = true; cmdVerbinden.Visible = true; cmdSenden.Visible = false; txbText.Visible = false; cmdTrennen.Visible = false; lbxEingang.Visible = false; } } } }Geändert von Otianer (03.03.09 um 12:25 Uhr)
Ähnliche Themen
-
'Socket Error #10038 Socket operation on non-socket'
Von jupp2oo8 im Forum C/C++Antworten: 2Letzter Beitrag: 30.01.08, 10:24 -
'Socket Error #10038 Socket operation on non-socket'
Von jupp2oo8 im Forum C/C++Antworten: 2Letzter Beitrag: 29.01.08, 14:59 -
Problem mit Socket
Von JavaJoe im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 1Letzter Beitrag: 30.06.07, 22:07 -
Socket problem
Von mrno im Forum JavaAntworten: 1Letzter Beitrag: 15.07.06, 13:59 -
Socket Problem
Von 2fast4you87 im Forum C/C++Antworten: 3Letzter Beitrag: 30.09.05, 16:49





Zitieren
Login





