tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
1010
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Registriert seit
    Jun 2008
    Ort
    Nah bei Köln
    Beiträge
    252
    Hallo zusammen,

    habe mir folgendes Tutorial angeschaut: http://www.switchonthecode.com/tutor...ded-tcp-server

    Funktioniert soweit sehr gut. Wenn ich folgenden Code (beim Client)

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
     
                TcpClient client = new TcpClient();
     
                IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12597);
     
                client.Connect(serverEndPoint);
     
                NetworkStream clientStream = client.GetStream();
     
                ASCIIEncoding encoder = new ASCIIEncoding();
                byte[] buffer = encoder.GetBytes("Hello Server!");
     
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();

    um folgendes erweitere:

    Code :
    1
    2
    
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();

    Kommt bei der Ausgabe folgendes (benutze eine MessageBox) heraus "Hello Server!Hello Server!". Ich möchte aber, dass beim Server zwei MessageBoxen erscheinen. Wie stell ich das an?
    Hier noch der Code vom Server:

    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
    
    using System;
    using System.Text;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;
     
    namespace TCPServerTutorial
    {
        class Server
        {
            private TcpListener tcpListener;
            private Thread listenThread;
     
            public Server()
            {
                this.tcpListener = new TcpListener(IPAddress.Any, 12597);
                this.listenThread = new Thread(new ThreadStart(ListenForClients));
                this.listenThread.Start();
            }
            private void ListenForClients()
            {
                this.tcpListener.Start();
     
                while (true)
                {
                    //blocks until a client has connected to the server
                    TcpClient client = this.tcpListener.AcceptTcpClient();
     
                    //create a thread to handle communication
                    //with connected client
                    Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                    clientThread.Start(client);
                }
            }
            private void HandleClientComm(object client)
            {
                TcpClient tcpClient = (TcpClient)client;
                NetworkStream clientStream = tcpClient.GetStream();
     
                byte[] message = new byte[4096];
                int bytesRead;
     
                while (true)
                {
                    message = new byte[4096];
                    bytesRead = 0;
     
                    try
                    {
                        //blocks until a client sends a message
                        bytesRead = clientStream.Read(message, 0, 4096);
                    }
                    catch
                    {
                        //a socket error has occured
                        break;
                    }
     
                    if (bytesRead == 0)
                    {
                        //the client has disconnected from the server
                        break;
                    }
     
                    //message has successfully been received
                    ASCIIEncoding encoder = new ASCIIEncoding();
                    System.Windows.Forms.MessageBox.Show(encoder.GetString(message, 0, bytesRead));
                }
     
                tcpClient.Close();
            }
        }
    }

    Danke im Voraus!

    Einen Guten Rutsch wünsch' ich euch!!
    Geändert von WorldRacer (31.12.09 um 20:18 Uhr)
     

  2. #2
    Erik Erik ist offline Mitglied Gold
    Registriert seit
    Jul 2008
    Beiträge
    171
    Hi,

    Ich würde am Anfang der Nachricht noch ein Byte setzten welches die Länge der Nachricht symbolisiert. Dann kannst du sie dir nachher wieder auseinander puzzeln.

    Viele Grüße und auch nen guten Rutsch
    Erik
     

  3. #3
    Registriert seit
    Jun 2008
    Ort
    Nah bei Köln
    Beiträge
    252
    Hallo,

    danke für die Idee, so simpel, da kommt man von allein nicht drauf xDD

    Habs jetzt in eine Client-Klasse gepackt und die Server-Klasse ein wenig erweitert:

    Server-Klasse:
    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
    
    using System;
    using System.Text;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;
     
    namespace TCPServerTutorial
    {
        class Server
        {
            private TcpListener tcpListener;
            private Thread listenThread;
     
            public Server()
            {
                this.tcpListener = new TcpListener(IPAddress.Any, 12597);
                this.listenThread = new Thread(new ThreadStart(ListenForClients));
                this.listenThread.Start();
            }
            private void ListenForClients()
            {
                this.tcpListener.Start();
     
                while (true)
                {
                    //blocks until a client has connected to the server
                    TcpClient client = this.tcpListener.AcceptTcpClient();
     
                    //create a thread to handle communication
                    //with connected client
                    Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                    clientThread.Start(client);
                }
            }
            private void HandleClientComm(object client)
            {
                TcpClient tcpClient = (TcpClient)client;
                NetworkStream clientStream = tcpClient.GetStream();
     
                byte[] message = new byte[4096];
                int bytesRead;
     
                while (true)
                {
                    message = new byte[4096];
                    bytesRead = 0;
     
                    try
                    {
                        //blocks until a client sends a message
                        bytesRead = clientStream.Read(message, 0, 4096);
                    }
                    catch
                    {
                        //a socket error has occured
                        break;
                    }
     
                    if (bytesRead == 0)
                    {
                        //the client has disconnected from the server
                        break;
                    }
     
                    //message has successfully been received
                    ASCIIEncoding encoder = new ASCIIEncoding();
                    processMessages(encoder.GetString(message, 0, bytesRead));
                }
     
                tcpClient.Close();
            }
            private void processMessages(string Message)
            {
                string[] commands = Message.Split(new char[] { '\n' });
                foreach (string command in commands)
                {
                    if (command != string.Empty)
                    {
                        handleMessage(command);
                    }
                }
            }
            public virtual void handleMessage(string command){
                System.Windows.Forms.MessageBox.Show(command);
            }
        }
    }

    Client-Klasse:
    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
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    namespace devConnector
    {
        class devClient
        {
            private TcpClient client = null;
            private IPEndPoint serverEndPoint = null;
            private int gate = 0;
            private string ipaddr = "";
            NetworkStream clientStream = null;
            public devClient(string IP, int port){
                
                gate = port;
                ipaddr = IP;
                
            }
            
            public void Connect(){
                if(client == null){
                    client = new TcpClient(ipaddr, gate);
                    clientStream = client.GetStream();
                }
            }
            public void Disconnect(){
                if(client != null){
                    if(client.Connected){
                        client.Close();
                        client = null;
                    }
                }
            }
                
            public void SendCommand(string Command){
                ASCIIEncoding encoder = new ASCIIEncoding();
                byte[] buffer = encoder.GetBytes(Command + "\n");
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();            
            }
        }
    }

    Code in der Anwendung der Client-Klasse:
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    
     static void Main(string[] args)
            {
                devClient clt = new devClient("10.0.100.137", 12597);
                clt.Connect();
                clt.SendCommand("Blah"); 
                clt.SendCommand("Blah");
                clt.Disconnect();
            }

    Hoffe ich kann damit jemandem weiterhelfen.

    Frohes neues
     

Ähnliche Themen

  1. Welche Einstellungen zum convertieren für RSTP Server (Darwin Stream server)?
    Von sweet18-4ever im Forum Videoschnitt, Videotechnik & -produktion
    Antworten: 0
    Letzter Beitrag: 31.10.10, 15:26
  2. Antworten: 0
    Letzter Beitrag: 12.09.08, 16:19
  3. MSSQL: Mappen von User-Daten von einem Server auf einen anderen Server
    Von JimKnopf80 im Forum Relationale Datenbanksysteme
    Antworten: 0
    Letzter Beitrag: 02.03.07, 14:57
  4. Antworten: 0
    Letzter Beitrag: 10.05.04, 12:21
  5. Von WIN2000 auf SQL-Server zugreifen geht, von WIN2003 Server nicht, warum?
    Von Bordi im Forum Relationale Datenbanksysteme
    Antworten: 0
    Letzter Beitrag: 07.05.04, 12:50