C# Chat

M

MrCodeMaster

Hi Leute,

Ich hab mir mal einen Chat - Server zusammengebastelt :)

Ich hab aber ein Problem, und zwar besteht es daraus, dass der Server nicht alles richtig beendet(listener, TcpClient usw.)

Ich poste mal meinen Code hier und hoffe auf eure Gutmütigkeit mir wieder mal zu helfen.

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace Server_FBA
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        public TcpListener s_client;
        public TcpClient s_connection;
        public TcpClient s_connect = new TcpClient();
        delegate void SetTextCallback(string text);

        public StreamWriter s_text_sent;
        public StreamReader s_text_received;

        public string text;
        public string finished_text;
        public char[] decryptet_text;
        public char[] encryptet_text;

        public int i;
        public int text_length;

        private void button1_Click(object sender, EventArgs e)
        {
            ThreadStart del;
            del = new ThreadStart(ServerConnection);
            Thread thread = new Thread(del);
            thread.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            txt_status.AppendText("Connection was closed by User!\n");

            try
            {
                s_connect = new TcpClient();
                s_connect.Connect(IPAddress.Parse(txt_ip.Text), 8001);

                StreamWriter c_text_sent = new StreamWriter(s_connect.GetStream());
                c_text_sent.WriteLine("Server closed Connection!\n");
                c_text_sent.Flush();
                
                s_connect.Close();
                s_connection.Close();
            }
            catch (Exception ex)
            {
                SetText("Server was not able to send Data! Error Code: " + ex);
            }

            
        }

        private void SetText(string text)
        { 
            if (this.txt_status.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.txt_status.AppendText(text + "\n") ;
            }
        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                s_connect = new TcpClient();
                s_connect.Connect(IPAddress.Parse(txt_ip.Text), 8001);

                text = "Server: " + txt_message.Text;
                i = 0;
                text_length = text.Length;

                encryptet_text = new char[text_length];

                while (i < text_length)
                {

                    encryptet_text[i] = Convert.ToChar(text[i] + 2);
                    i++;
                }

                finished_text = new String(encryptet_text);

                s_text_sent = new StreamWriter(s_connect.GetStream());
                s_text_sent.WriteLine(finished_text);
                s_text_sent.Flush();

                txt_status.AppendText("Server: " + txt_message.Text + "\n");
            }
            catch (Exception exp)
            {
                SetText("" + exp);
            }
        }

        public void ServerConnection()
        {
            try
            {

                s_client = new TcpListener(IPAddress.Any, 8000);
                s_client.Start();

                SetText("Server successfully started!\n");

                s_connect = new TcpClient();
                s_connection = s_client.AcceptTcpClient();

                s_text_received = new StreamReader(s_connection.GetStream());
                text = s_text_received.ReadLine();

                SetText(text);
                
                    if (text == "Test Message from Client!")
                    {
                        SetText("Connection etablished!\n");
                    }
                    else
                    {
                        SetText("Client is not online!\n");
                        
                    }

                s_connect.Connect(IPAddress.Parse(txt_ip.Text), 8001);

                s_text_sent = new StreamWriter(s_connect.GetStream());
                s_text_sent.WriteLine("Test Message from Server!\n");
                s_text_sent.Flush();

                while (true)
                {
                    s_connect = new TcpClient();
                    s_client.Start();

                    s_connection = s_client.AcceptTcpClient();
                    s_text_received = new StreamReader(s_connection.GetStream());
                    text = s_text_received.ReadLine();

                    decryptet_text = new char[text.Length];
                    i = 0;

                    while (i < text.Length)
                    {
                        decryptet_text[i] = Convert.ToChar(text[i] - 2);
                        i++;
                    }

                    text = new String(decryptet_text);
                    SetText(text);
                }
            }
            catch (Exception e)
            {
                SetText("Server was not able to load Socket! Error Code: " + e);
            }


        }
    }
}

Lg,
MrCodeMaster
 
M

MrCodeMaster

Hi,

Ich bekomme die Fehlermeldung, dass jede Socketadresse nur einmal verwendet werden kann...

wie beheb ich dass?

Code:
public void ServerConnection()
        {
           
            try
            {
                s_client = new TcpListener(IPAddress.Any, 8000);
                s_client.Start();
                

                SetText("Server successfully started!\n");

                
                s_connect = new TcpClient();
                s_connection = s_client.AcceptTcpClient();

                s_text_received = new StreamReader(s_connection.GetStream());
                text = s_text_received.ReadLine();
                
                i = 0;

                text_length = text.Length;

                while(i_1 < text_length)
                {
                    IP[i] = Convert.ToChar(text[i_1]);
                    i++;
                    i_1++;
                }

                IP_connect = new char[i];
                
                while( z < i)
                {
                    IP_connect[z] = IP[z];
                    z++;
                }

                text_message = new char[text.Length - (i + 1)];

                i = 0;
                while (i < text_message.Length)
                {
                    text_message[i] = text[i];
                    i++;
                }

                text = new string(text_message);

                SetText(text);
                
                    if (text == "Test Message from Client!")
                    {
                        SetText("Connection etablished!\n");
                    }
                    else
                    {
                        SetText("Client is not online!\n");
                        
                    }

                    

                    IP_connect_to_client = new string(IP_connect);

                s_connect.Connect(IPAddress.Parse(IP_connect_to_client), 8001);

                s_text_sent = new StreamWriter(s_connect.GetStream());
                s_text_sent.WriteLine("Test Message from Server!\n");
                s_text_sent.Flush();

                while (true)
                {
                    s_connect = new TcpClient();
                    s_client.Start();

                    s_connection = s_client.AcceptTcpClient();
                    s_text_received = new StreamReader(s_connection.GetStream());
                    text = s_text_received.ReadLine();

                    decryptet_text = new char[text.Length];
                    i = 0;

                    while (i < text.Length)
                    {
                        decryptet_text[i] = Convert.ToChar(text[i] - 2);
                        i++;
                    }

                    text = new String(decryptet_text);
                    SetText(text);
                    s_client.Stop();
                }
                
            }
            catch (Exception e)
            {
                SetText("Server was not able to load Socket! Error Code: " + e);
            }


        }

Zeile 7 löst das Problem beim 2 mal starten aus

Lg
 
Zuletzt bearbeitet von einem Moderator: