Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi my name is vishal .Recently i have created a client-server communication using winsock control in c# windows forms.
In my server form i have 4textboxes,2 button control and 1 winsock control named:SockMain
my server form name:frmServer
I have a client form named:frmClient which contains 3 textboxes,1 winsock control named:SockMain and 2 button controls.
I am able to connect frmServer and frmClient through following c# code:
Given below is c# code of frmClient:
C#
private void btnListen_Click(object sender, EventArgs e)
        {
            SockMain.LocalPort = Convert.ToInt32(txtPort.Text);
            SockMain.Listen();
        }
private void SockMain_ConnectionRequest(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
        {
            if (SockMain.CtlState != 0)
            {
                SockMain.Close();
            }
            SockMain.Accept(e.requestID);
            txtStatus.Text = txtStatus.Text + "Accepted Connection from: " + SockMain.RemoteHostIP + System.Environment.NewLine;
        }

Given below is c# code of frmServer:
C#
private void btnConnect_Click(object sender, EventArgs e)
        {
            SockMain.RemoteHost = txtHost.Text;
            SockMain.RemotePort = Convert.ToInt32(txtPort.Text);
            SockMain.Connect();
        }

So i am able to connect between frmClient and frmServer using Winsock control in c# windows forms which as a result i get message:"Accepted Connection from: 192.168.0.105(which is ip address of my system) in my textbox named:txtStatus in my form in frmClient.
However the problem is i am not able to communicate asynchronously between my client(frmClient) and server(frmServer) and i dont know why! .

Given below is c# code of frmClient where i get stuck:
C#
private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] abData;
           string str=txtSend.Text;
           abData = System.Text.Encoding.Default.GetBytes(str);
            SockMain.SendData(abData);
        }
private void SockMain_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string strData=txtSend.Text;
            Object buffer2 = (object)strData;
            SockMain.GetData(ref buffer2);
            txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
        }

Given below is c# code of frmServer where i get stuck:
C#
private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] abData;
            string str = txtSend.Text;
            abData = System.Text.Encoding.Default.GetBytes(str);
            SockMain.SendData(abData);
        }
private void SockMain_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string strData=txtSend.Text;
            Object buffer2 = (object)strData;
            SockMain.GetData(ref buffer2);
            txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
        }

What i want is once my frmServer and frmClient is connected through winsock control in c# windows forms i want my client form(frmClient) and server form(frmServer) to communicate asynchronously based on text entered in textbox(txtSend) through button(btnSend).

I believe i am getting stuck at transferring(string data) and receiving(string data) data using SendData function and GetData function of winsock control(SockMain).

Can anyone help me how to send string data using SendData function of winsock control and receive string data using GetData function of winsock control? Can anyone help me please? Any help/guidance in solving of this problem would be greatly appreciated!
Posted

1 solution

Hi my name is vishal(Member 10248768) of this forum.
Well by God's grace i was able to solve my problem through following code:
So in frmServer i made some modifications given below is c# code:
C#
private void SockMain_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string strData="";
            Object tend = (object)strData;
            SockMain.GetData(ref tend);
            strData = (String)tend;
            txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
        }
private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] abData;
            string str = txtSend.Text;
            abData = System.Text.ASCIIEncoding.Default.GetBytes(str);
            SockMain.SendData(txtSend.Text);
        }

In frmClient i made similar modifications given below is it's c# code:
C#
private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] abData;
           string str=txtSend.Text;
           abData = System.Text.ASCIIEncoding.Default.GetBytes(str);
            SockMain.SendData(txtSend.Text);
        }
 private void SockMain_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            string strData="";
            Object send = (object)strData;
            SockMain.GetData(ref send);
            strData = (String)send;
            txtStatus.Text = txtStatus.Text + strData + System.Environment.NewLine;
        }

Since i was able to connect between my client(frmClient) and server(frmServer) through winsock control in c# windows forms.
In order to help my client and server communicate asynchronously i have used the above code and it works!
Thanks for your help anyway and i thank the Almighty in helping me solve the problem by myself!
 
Share this answer
 
Comments
Arthi 12521509 14-May-16 3:24am    
Hi Mr. Vishal

nice coding i'm also working in c# winsock server client application. but iam getting an error message in server and client

when i give some data in to txtSend and click send button i got an error message in this line:
SockMain.SendData(txtSend.Text);
error message as:
Additional information: Exception from HRESULT: 0x800A9C46

and in client when i give some data in to txtSend and click send button i got an error message in this line:

SockMain.SendData(txtSend.Text);

Error message is:

Additional information: Exception from HRESULT: 0x800A9C46

what is the problem in my code help me to solve

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