Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Server coding
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace MultiCon
{
    class Listener
    {
        Socket s;
        public bool Listening
        {
            get;
            private set;
        }
        public int Port
        {
            get;
            private set;
        }
        public Listener(int port)
        {
            Port= port;
            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        }
        public void start()
        {
            if (Listening)
                return;
            s.Bind(new IPEndPoint(0, Port));
            s.Listen(0);
            s.BeginAccept(callback, null);
            Listening = true;
        }
        public void stop()
        {
            if (!Listening)
                return;
            s.Close();
            s.Dispose();
            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
        void callback(IAsyncResult ar)
        {
            try
            {
                Socket s = this.s.EndAccept(ar);
                if (SocketAccepted != null)
                {
                    SocketAccepted(s);
                }
                this.s.BeginAccept(callback, null);
            }
            
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        public delegate void SocketAcceptedHandler(Socket e);
        public event SocketAcceptedHandler SocketAccepted;
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace MultiCon
{
    class Program
    {
        
        
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Main());
        }
       
    }
}


Client Side Programming
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace MultiCon_C
{
    public partial class Main : Form
    {
        Socket sck;
        public Main()
        {
            InitializeComponent();
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          
        }

        private void btnconnect_Click(object sender, EventArgs e)
        {
            sck.Connect("192.168.201.5", 22);
            MessageBox.Show("Connected");
        }

        private void btnsend_Click(object sender, EventArgs e)
        {
            int s=sck.Send(Encoding.Default.GetBytes(txtMsg.Text));
            if(s>0)
            {
                MessageBox.Show("Data Sent");
            }
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            sck.Close();
            sck.Dispose();
            Close();
        }

        private void txtMsg_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


What I have tried:

I m getting this error while running the code clicking on the connect button from the client side.

System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Posted
Comments
F-ES Sitecore 8-Feb-16 4:38am    
What port is your server code listening on?
Rob Philpott 8-Feb-16 5:06am    
Firstly, as you have a client and a server and don't know which one is misbehaving, try running the server and connecting to it with telnet.

That said, I would think its the server. What's that zero about in the IPEndPoint? Try using IPAddress.Any and see if that makes any odds.

1 solution

Start here from something that works : WCF Killer[^]

Also check your firewall settings to allow the port numbers you are using.

And try not to use known ports that may already be in use.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900