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):
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" ;
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):
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:
private void btnConnect_Click(object sender, EventArgs e)
{
Winsock1.RemoteHost = "192.168.0.118" ;
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:
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!