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:
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:
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:
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:
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!