Autor Beitrag
erdmulch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Do 20.01.11 12:46 
Hallo zusammen,

ich habe ein kleines Chat Programm geschrieben, die Kommunikation mit dem Server kommt auch zustande!
Sobald ich am Server eine Nachricht eingebe und die Enter-Taste betätige, springt mir das Programm in den Catchblock

kann mir jemand sagen an was das liegt?
danke im voraus



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:
using System;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(80);
            int requestCount = 0;
            TcpClient clientSocket = default(TcpClient);
            serverSocket.Start();
            Console.WriteLine(" >> Server Started");
            clientSocket = serverSocket.AcceptTcpClient();
            Console.WriteLine(" >> Accept connection from client");
            requestCount = 0;

            while ((true))
            {
                try
                {
                    requestCount = requestCount + 1;
                    NetworkStream networkStream = clientSocket.GetStream();
                    byte[] bytesFrom = new byte[10025];
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    Console.WriteLine(" >> Data from client - " + dataFromClient);
                    string serverResponse = "Server response " + Convert.ToString(requestCount);
                    Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                    networkStream.Write(sendBytes, 0, sendBytes.Length);
                    networkStream.Flush();
                    Console.WriteLine(" >> " + serverResponse);
                }
                catch (Exception ex)
                {
                  Console.WriteLine(ex.ToString());
                }
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> exit");
            Console.ReadLine();
        }
    }
}


Moderiert von user profile iconKha: C#-Tags hinzugefügt
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Do 20.01.11 15:51 
user profile iconerdmulch hat folgendes geschrieben Zum zitierten Posting springen:
Sobald ich am Server eine Nachricht eingebe und die Enter-Taste betätige, springt mir das Programm in den
Dürften wir auch noch erfahren, warum es das tut, also welche Exception genau auftritt :) ?

_________________
>λ=
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 20.01.11 16:31 
Hi,

was soll das denn für ein Programm werden: Client und Server in einem???
Eine bessere Benennung wäre daher angebracht (d.h. listener anstatt serverSocket)...

P.S.
Und bei der Zeile
ausblenden C#-Quelltext
1:
TcpClient clientSocket = default(TcpClient);					

mußte ich ersteinmal überlegen, ob das überhaupt compiliert und was als Ergebnis rauskommmt (nämlich 'null' -)