Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi my name is vishal for past 1 week i have been breaking my head on How to make my winsock control in client application to connect to server application which also has a winsock control in c# windows forms?
So i have a client application named:Winsock client which has a form named:Form1 which also has a winsock control named:Winsock1,Protocol:0-sckTCPProtocol and visible:true

Given below is my c# code of Form1 of Winsock client(client application):
C#
namespace Winsock_client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
private void btnConnect_Click(object sender, EventArgs e)
        {
            Winsock1.RemoteHost = "192.168.0.118" ;//Change this to your host ip
            Winsock1.RemotePort = 7777;
            Winsock1.Connect();
            shpGo.Visible=true;
            txtPrice.Focus();
        }
private void btnDisconnect_Click(object sender, EventArgs e)
        {
            Winsock1.Close();
            shpGo.Visible=false;
            shpWait.Visible =false;
            shpError.Visible = true;
        }
private void Winsock1_SendComplete(object sender, EventArgs e)
        {
            label3.Text = "Completed Data Transmission";
        }
 private void Winsock1_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string sData="";
            Object cand = (object)sData;
            Winsock1.GetData(ref cand);
            sData = (String)cand;
            txtPrice.Text = sData;
            label3.Text = "Received Data";
            shpGo.Visible = true;
            shpWait.Visible = false;
            shpError.Visible = false;
        }
 private void btnSend_Click(object sender, EventArgs e)
        {
            
           if(Winsock1.CtlState==2)
           {
            Winsock1.SendData(txtPrice.Text);
            shpGo.Visible =true;
            label3.Text = "Sending Data";
           }
            else
           {
            shpGo.Visible=false;
            shpWait.Visible=false;
            shpError.Visible =true;
            label3.Text = "Not currently connected to host";
           }
        }

So i also have a server application named:Winsock server which has a form named:Form1 and also has a winsock control named:Socket,Protocol:0-sckTCPProtocol and visible:true

Given below is my c# code of Form1 of Winsock server(Server application):
C#
namespace Winsock_server
{
    public partial class Form1 : Form
    {
        int iSockets;
        string sServerMsg;
        string sRequestID;
        Socket sck;
        public Form1()
        {
            InitializeComponent();
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        }
        private string GetLocalIP()
        {
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            return "127.0.0.1";
        }
private void Form1_Load(object sender, EventArgs e)
        {
            txtHostID.Text = Socket.LocalHostName;
            txtAddress.Text = Socket.LocalIP;
            Socket.LocalPort = 7777;
             sServerMsg = "Listening to port: " + Socket.LocalPort;
            listBox1.Items.Add(sServerMsg);
            Socket.Listen();
        }
private void Socket_CloseEvent(object sender, EventArgs e)
        {
            sServerMsg = "Connection closed: " + Socket.RemoteHostIP;
            listBox1.Items.Add(sServerMsg);
            Socket.Close();
            iSockets = iSockets - 1;
            txtConnections.Text = iSockets.ToString();
        }
private void Socket_ConnectionRequest(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
        {
            sServerMsg = "Connection request id " + e.requestID + " from " + Socket.RemoteHostIP;
            listBox1.Items.Add(sServerMsg);
            sRequestID = e.requestID.ToString();
            iSockets = iSockets + 1;
            txtConnections.Text = iSockets.ToString();
        }
private void Socket_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string incommingData = "";
            Object rend = (object)incommingData;
            Socket.GetData(ref rend);
            incommingData = (String)rend;
            listBox1.Text = listBox1.Text + System.Environment.NewLine + incommingData;
        }

So i execute my client application(Winsock client) and server application(Winsock server) simultaneously for client-server communication.
So i am able to connect my client application(Winsock client) partially,Given below is c# code of that:
C#
private void btnConnect_Click(object sender, EventArgs e)
        {
            Winsock1.RemoteHost = "192.168.0.118" ;//Change this to your host ip
            Winsock1.RemotePort = 7777;
            Winsock1.Connect();
            shpGo.Visible=true;
            txtPrice.Focus();
        }

But here is the problem after connection is established between client application(Winsock client) and Server application(Winsock server) when i enter data in textbox named:txtPrice in Form1 of Winsock client(client application) and when i click btnSend in Winsock client to send data entered in textbox(txtPrice) to listbox named:listBox1 which is present in Form1 of Winsock server(Server application) given below is c# code of that:
C#
private void btnSend_Click(object sender, EventArgs e)
        {
           if(Winsock1.CtlState==2)
           {
            Winsock1.SendData(txtPrice.Text);
            shpGo.Visible =true;
            label3.Text = "Sending Data";
           }
            else
           {
            shpGo.Visible=false;
            shpWait.Visible=false;
            shpError.Visible =true;
            label3.Text = "Not currently connected to host";
           }
        }

After entering data in txtPrice(textbox control in Form1) of Winsock client(Client application) and when i click/press btnSend(button control in Form1 of Winsock client) i always get message "Not currently connected to host" in my label3(label control in Form1 of Winsock client(client application)).

This error makes me wonder how to make/enable my winsock control named:Winsock1 present in client application named:Winsock client to connect to winsock control named:Socket present in server application name:Winsock server?

Can anyone help me on how to establish connection between client application(Winsock client) and server application(Winsock server) using Winsock controls only in c# windows forms?

I have to use only Winsock controls for this purpose because of my BOSS's orders.
Can anyone help me Please? Can anyone help me/guide me on how to solve my problem? Any help/guidance in solving of this problem would be greatly appreciated!
Posted
Comments
ZurdoDev 14-Jul-14 7:19am    
That's too much code for me to look through and find your issue. I don't even understand exactly where you are stuck. However, the basic concept is to have each side listening on a port and then you can communicate.
Member 10248768 14-Jul-14 7:29am    
Dear RyanDev
Thank for replying to my question on such short notice.
I am stuck at given below c# code of Form1 of Winsock client(client application):
private void btnSend_Click(object sender, EventArgs e)
{
if(Winsock1.CtlState==2)
{
Winsock1.SendData(txtPrice.Text);
shpGo.Visible =true;
label3.Text = "Sending Data";
}
else
{
shpGo.Visible=false;
shpWait.Visible=false;
shpError.Visible =true;
label3.Text = "Not currently connected to host";
}
}
Since after connection with client and server application.After i enter data in txtPrice(textbox control in Form1 of Winsock client) and press/click btnSend(button control) i get message:"Not currently connected to host" in my label3(label control). So my question is how to make my winsock control named:Winsock1 of Winsock client(client application) to connect to Winsock server(Server application) which has a winsock control named:Socket? Reply please?! I am waiting for your reply Sir!
Member 10248768 14-Jul-14 7:52am    
Dear Mr.RyanDev
I also have another question
Given below is my c# code of Winsock client(Client application) using Winsock1(winsock control) for connection with/to Winsock server(Server application) using Socket(winsock control)
Winsock Client c# code:
private void btnConnect_Click(object sender, EventArgs e)
{
Winsock1.RemoteHost = "192.168.0.118" ;//Change this to your host ip
Winsock1.RemotePort = 7777;
Winsock1.Connect();
shpGo.Visible=true;
txtPrice.Focus();
}

Winsock server c# code:
private void Form1_Load(object sender, EventArgs e)
{
txtHostID.Text = Socket.LocalHostName;
txtAddress.Text = Socket.LocalIP;
Socket.LocalPort = 7777;
sServerMsg = "Listening to port: " + Socket.LocalPort;
listBox1.Items.Add(sServerMsg);
Socket.Listen();
}
private void Socket_ConnectionRequest(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
{
sServerMsg = "Connection request id " + e.requestID + " from " + Socket.RemoteHostIP;
listBox1.Items.Add(sServerMsg);
sRequestID = e.requestID.ToString();
iSockets = iSockets + 1;
txtConnections.Text = iSockets.ToString();
}
So my question is have done connection correctly for Winsock client(client application) using winsock control named:Winsock1 to connect to/with Winsock server(Server application) which has a winsock control named:Socket?
I am asking you this because i believe there must be problem/error of connection between Winsock client(client application) and Winsock server(Server application) using winsock controls? If so can you point what mistakes have i made and what must be done to rectify that mistake? Reply please Sir! I hope i get reply from you Sir!
johannesnestler 14-Jul-14 7:56am    
Hi Member,
Sorry that I can't spot your error. Your error handling is "not good" - the message you give yourself is just based "on a guess" because of unexpected control state - Debug: What is the Winsock1.CtlState in real? Try to hunt down the possible error sources. Ask yourself questions like: does this code work if "Firewall disabled, run on a local computer with loopback, ...". Also if you are a developer you should be able to argue for tech decisions. Your taged .NET 4, used C# and told you HAVE TO use Winsock control???? What kind of Boss is this? Been in coma last decade ;-)
Member 10248768 14-Jul-14 8:16am    
Dear johannesnestler Winsock1.CtlState represents the state of my winsock control.My winsock control named:Winsock has a CtlState property which in design time shows:0-sckClosed. The CtlState is of short data type.
Can you at least tell me what is equivalent of State property of winsock control of vb6 in c# windows forms of winsock control?,Answer to this question would really help me figure out the error and solving it. Reply please!

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