6 Replies - 535 Views - Last Post: 19 October 2011 - 07:58 PM Rate Topic: -----

#1 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

From disappears when running the code....

Posted 19 October 2011 - 06:36 PM

i got server and client when i run the client alone the form appear with he correct error that the server is unreachable. when i run the server then run the client i see the console of the server but the form of the client is invisible to me but both are communicating and working. after closing the server the client appears.
Is This A Good Question/Topic? 0
  • +

Replies To: From disappears when running the code....

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: From disappears when running the code....

Posted 19 October 2011 - 06:42 PM

Without seeing the code, we are just shooting in the dark.

My suspicion is that you are doing your communication on the UI thread. Doing this could possibly cause the UI thread to freeze while the communication is happening. If the communication is constant, then the form will not show until the communication stops(when the server app closes).
Was This Post Helpful? 0
  • +
  • -

#3 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: From disappears when running the code....

Posted 19 October 2011 - 06:45 PM

That's what is happening and to solve that i need??

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Collections;
using System.Threading;
namespace threadedserver
{
    
    class MultiThreadedTcpServer
    {
        public static ArrayList tcpClients = new ArrayList();
        public static ArrayList al = new ArrayList();
        public static void Main(string[] args)
        {
            TcpListener tcpListener = new TcpListener(IPAddress.Any, 8000);
            tcpListener.Start();
            Console.WriteLine("The server is listening on port : 8000 \n" + "the local endpoint is : " + tcpListener.LocalEndpoint + "\n");
            Console.WriteLine("Waiting for connections ....");
            TcpClient tempc;
            while (true)
            {
                try
                {
                    
                    for (int z = 0; z < tcpClients.Count; z++ )
                    {
                        tempc = (TcpClient)(tcpClients[z]);
                        if(!tempc.Connected)
                        {
                            tcpClients.RemoveAt(z);

                            DissConnectionHandler dissConnectionHandler = new DissConnectionHandler(tempc, z);

                            Thread thread = new Thread(new
                                                     ThreadStart(dissConnectionHandler.HandleDissConnection));

                            thread.Start();
                        }
                    }
                
                    TcpClient client = tcpListener.AcceptTcpClient();
                    Socket s = client.Client;
                    EndPoint temp = (IPEndPoint)s.RemoteEndPoint;
                    Console.WriteLine("I am connected to " + temp);
                    al.Add(temp);
                    tcpClients.Add(client);
                    
                    ConnectionHandler connectionHandler = new ConnectionHandler(client);
                   
                    Thread secondThread = new Thread(new
                                             ThreadStart(connectionHandler.HandleConnection));
                    
                    secondThread.Start();
                }
                catch (Exception)
                {
                    Console.WriteLine("Connection failed on port " );
                }
            }
        }
    }

    class ConnectionHandler
    {

        private TcpClient client;
        private NetworkStream ns;

        private static int connections = 0;

        public ConnectionHandler(TcpClient client)
        {
            this.client = client;
        }

        public void HandleConnection()
        {
            try
            {
                
                connections++;
                Console.WriteLine("New client accepted: {0} active connections", connections);
                TcpClient tempClient;
                
                for (int i = 0; i < MultiThreadedTcpServer.tcpClients.Count; i++)
                {
                    Console.WriteLine("Clients   :" + MultiThreadedTcpServer.tcpClients.Count);
                    tempClient = (TcpClient)MultiThreadedTcpServer.tcpClients[i];
                    ns = tempClient.GetStream();
                    byte[] ba;
                    for (int j = 0; j < MultiThreadedTcpServer.al.Count; j++)
                        {
                            ASCIIEncoding asen = new ASCIIEncoding();
                            ba = asen.GetBytes("Client "+ j + " Ip Adreess : " + MultiThreadedTcpServer.al[j] + "  is connected ..! \n");
                            Console.WriteLine("Transmitting....." + MultiThreadedTcpServer.al[j]);
                            ns.Write(ba, 0, ba.Length);
                        }

                    
                   }
            }
            catch (Exception e)
            {
                Console.WriteLine("exception" + e.StackTrace);
            }
        }
    }
    class DissConnectionHandler
    {
        private TcpClient client;

        private NetworkStream ns;
        private int z;
        public DissConnectionHandler(TcpClient client, int z)
        {
            this.client = client;
            this.z = z;
        }
        public void HandleDissConnection()
        {
            TcpClient tempClient;

            for (int i = 0; i < MultiThreadedTcpServer.tcpClients.Count; i++)
            {
                Console.WriteLine("Clients   :" + MultiThreadedTcpServer.tcpClients.Count);
                tempClient = (TcpClient)MultiThreadedTcpServer.tcpClients[i];
                ns = tempClient.GetStream();
                
                    ASCIIEncoding asen = new ASCIIEncoding();
                    byte[] ba = asen.GetBytes("Client  "+ z +" with ip address  : " + MultiThreadedTcpServer.al[z] + "  disconnected");
                    ns.Write(ba, 0, ba.Length);
                
            }
        }
    }
}




client form:
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.Sockets;
using System.Net;
namespace ClientGui
{
    public partial class Form1 : Form
    {
        TcpClient tcpClient = new TcpClient();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                tcpClient = new TcpClient();
                msg("Connecting ...... \n");
                tcpClient.Connect("127.0.0.1", 8000);
                msg("Connected ...... \n");
                NetworkStream networkStream = tcpClient.GetStream();
                ASCIIEncoding encoder = new ASCIIEncoding();
                while (true)
                {

                    byte[] buffer2 = new byte[1024];
                    
                    msg("waiting for update");
                    int k = networkStream.Read(buffer2, 0, buffer2.Length);
                    msg(encoder.GetString(buffer2, 0, k));

                }
                            }
            catch (Exception exe)
            {
                msg("Unable to connect   !!!!\n" + exe.StackTrace);
                
            }
        }

        private void disconnect_Click(object sender, EventArgs e)
        {
            tcpClient.Close();
        }

        public void msg(string mesg)
        {
            textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        } 

    }
}



This post has been edited by kngofdrkns: 19 October 2011 - 06:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: From disappears when running the code....

Posted 19 October 2011 - 06:49 PM

You need to use multi-threading to do the communication on a background thread.

Here is a tutorial to help you out.

C# Multi-Threading In A GUI Environment
Was This Post Helpful? 1
  • +
  • -

#5 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: From disappears when running the code....

Posted 19 October 2011 - 07:42 PM

When applying the tutorial i faced a problem where i cant get access the textBox1 item from the WorkItem class by using:
Form1.textBox1

to do the :
public void msg(string mesg)
        {
            
            textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
        }


method in the WorkItem class
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: From disappears when running the code....

Posted 19 October 2011 - 07:50 PM

If the TextBox isn't part of the class, you won't have access to it.

What are you trying to accomplish with that code? Why is it in a separate class? It directly uses a control on the form, therefore, that code should be in the code-behind of the form.
Was This Post Helpful? 0
  • +
  • -

#7 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: From disappears when running the code....

Posted 19 October 2011 - 07:58 PM

ok when when i removed the connections and receiving parts from the form and added it to the workitem it had msg(" "); used there. i am confused. what i did is that i took :
try
            {
                tcpClient = new TcpClient();
                msg("Connecting ...... \n");
                tcpClient.Connect("127.0.0.1", 8000);
                msg("Connected ...... \n");
                NetworkStream networkStream = tcpClient.GetStream();
                ASCIIEncoding encoder = new ASCIIEncoding();
                while (true)
                {

                    byte[] buffer2 = new byte[1024];

                    msg("waiting for update");
                    int k = networkStream.Read(buffer2, 0, buffer2.Length);
                    msg(encoder.GetString(buffer2, 0, k));

                }
            }
            catch (Exception exe)
            {
                msg("Unable to connect   !!!!\n" + exe.StackTrace);

            }


and added it to the performwork method in workitem so msg is also copied to workitem therefore need the msg method there. (i guess i am totaly lost) :bananaman:

This post has been edited by kngofdrkns: 19 October 2011 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1