CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2011
    Posts
    6

    No connection between client and server.

    hello, im working on c# client server program, no error, but i don't know why both client and server cannot connect, no response at all. i think something is wrong in my codes and i seriously don't understand whats the problem. Please help me. Thanks in advance.

    server code:

    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; //networking
    using System.Net.Sockets; //networking
    using System.Threading; //networking
    using System.Net.NetworkInformation; 
    
    namespace NBCS_serverlogin
    {
        public partial class NBCSadmin : Form
        {
            private TcpListener tcpListener;
            private Thread threadTcp;
            static NetworkStream clientStream;
            const int portnumber = 3333;
    
            public NBCSadmin()
            {
                InitializeComponent();
                portnotxt.Text = "3333";
                // initiateListen();
            }
    
            private void initiateListen()
            {
                //Tcp
                this.tcpListener = new TcpListener(IPAddress.Any, portnumber);
                tcpListener.Start();
                int intPort = Convert.ToInt32(portnotxt.Text.Trim());
                this.threadTcp = new Thread(new ThreadStart(ListenToClients));
                
            }
    
            //listen for connected client 
            private void ListenToClients()
            {
                //tcpListener.Start(); 
    
                while (true)
                {
                    try
                    {
                        // block till client connect to server
                        TcpClient clientCom = this.tcpListener.AcceptTcpClient(); 
    
                        //handle connected clients
                        Thread threadClient = new Thread(new ParameterizedThreadStart(HandleClients));
                        threadClient.Start(clientCom);
                        constatuslabel.Text = "Received Client Connection.";
    
                    }
                    catch (SocketException)
                    {
                        MessageBox.Show ("error");
                    }
                }
            }
            private void HandleClients(object clientCom)
            {
                TcpClient tcpClient = (TcpClient)clientCom;
                clientStream = tcpClient.GetStream();
    
                byte[] message = new byte[4096];
                int bytesRead;
    
                while (true)
                {
                    bytesRead = 0;
    
                    try
                    {
                        bytesRead = clientStream.Read(message, 0, 4096);
                    }
    
                    catch (Exception)
                    {
                        // error happened
                        break;
                    }
    
                    if (bytesRead == 0)
                    {
                        // client diconnected to server
                        constatuslabel.Text = "Client has disconnected from the server.";
                        break;
                    }
    
                    //msg has been successfully received
                    ASCIIEncoding asciiEncoder = new ASCIIEncoding();
                    // output msg
                    constatuslabel.Text = tcpClient.Client.LocalEndPoint + "" + tcpClient.Client.RemoteEndPoint + asciiEncoder.GetString(message, 0, bytesRead);
    
                    //System.Diagnostics.Debug.WriteLine(asciiEncoder.GetString(message, 0, bytesRead));
                }
                // (button)
                tcpClient.Close();
            }
    
            private void connecttoclientbtn_Click_1(object sender, EventArgs e)
            {
                initiateListen();
                constatuslabel.Text = "Waiting for a connection...";
                
            }
    
            private void stopconnbtn_Click(object sender, EventArgs e)
            {
                tcpListener.Stop();
                constatuslabel.Text = "Connection disconnected...";
            }
    
        }
    }
    client code:

    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 NBCS_Client
    {
        public partial class NBCSuser : Form
        {
            static TcpClient clientuser;
            IPEndPoint serverEndPoint;
            NetworkStream clientStream;
            const int portnumber = 3333;
            string myIPAdd = string.Empty; 
            string strgAddress = string.Empty;
    
            public NBCSuser()
            {
    
                InitializeComponent();
                ipaddtxt.Text = "127.0.0.1";
                portnotxt.Text = "3333";
            }
    
            public void initiateListenUser()
            {
                clientuser = new TcpClient();
    
                //get the remote ip add
                IPAddress ipaddress = IPAddress.Parse(ipaddtxt.Text.Trim());
                int intPort = System.Convert.ToInt32(portnotxt.Text.Trim());
                //create end point
                serverEndPoint = new IPEndPoint(ipaddress, portnumber);
    
            }
    
            public void connectTcpServer()
            {
                clientuser.Connect(serverEndPoint);
                clientStream = clientuser.GetStream();
    
                //create thread to handle communication with connected client
                Thread threadClient = new Thread(new ParameterizedThreadStart(HandleClients));
                threadClient.Start(clientuser);
    
            }
    
            public void HandleClients(object clientuser)
            {
                TcpClient tcpclient = (TcpClient)clientuser;
                NetworkStream clientStream = tcpclient.GetStream();
    
                byte[] message = new byte[4096];
                int bytesRead;
    
                while (true)
                {
                    bytesRead = 0;
    
                    try
                    {
                        //block until client send a msg
                        bytesRead = clientStream.Read(message, 0, 4096);
                    }
                    catch (SocketException)
                    {
                        //socket error
                        break;
                    }
    
                    if (bytesRead == 0)
                    {
                        //client disconnected to server
                        break;
                    }
                    //msg received successfully
                    ASCIIEncoding asciiEncoder = new ASCIIEncoding();
                    // output msg
                    userconstatuslabel.Text = tcpclient.Client.LocalEndPoint + "" + tcpclient.Client.RemoteEndPoint + asciiEncoder.GetString(message, 0, bytesRead);
                    //textFrmServer 
    
                }
                tcpclient.Close();
    
            }
    
            private void connectserverbtn_Click(object sender, EventArgs e)
            {
                initiateListenUser();
                connectTcpServer();
                userconstatuslabel.Text = "Connecting to server...";
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void exitbtn_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
        }
    }

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: No connection between client and server.

    Several things strike me as odd in regard to the server code...

    - The port number never seems to be assigned to the listener. (EDIT: my bad, it is indeed assigned)
    - The thread is never started.
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Jul 2011
    Posts
    6

    Re: No connection between client and server.

    the thread is never started? i dont understand...

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: No connection between client and server.

    In the server code, I can't see any calls to start the 'threadTcp' thread. If that's the case, how do you expect it to do anything?
    It's not a bug, it's a feature!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured