[CODE]
public MainForm()
{
InitializeComponent();
tcpServer.LocalPort = 88;
tcpServer.DataArrival += new AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEventHandler(tcpServer_DataArrival);
tcpServer.ConnectionRequest += new AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEventHandler(tcpServer_ConnectionRequest);
tcpServer.ConnectEvent +=new EventHandler(tcpServer_ConnectEvent);
tcpServer.Error +=new AxMSWinsockLib.DMSWinsockControlEvents_ErrorEventHandler(tcpServer_Error);
tcpServer.Close();
this.FormClosing +=new FormClosingEventHandler(MainForm_FormClosing);
}
private void tcpServer_Error(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent e)
{
MessageBox.Show(e.description);
}
//added to close socket before program exit. this also is not working
//as subsequent calls to program returns "Address in use" error
private void MainForm_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
tcpServer.Close();
}
private void tcpServer_ConnectEvent(object sender, EventArgs e)
{
richTextBox1.AppendText("TCP Connection Made...\r\n");
try
{
tcpServer.SendData("Hey Ryan Here I am!"); //server does not receive this for some reason.
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// tcpServer.Close();
}
private void tcpServer_ConnectionRequest(object sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
{
richTextBox1.AppendText("TCP Connection Requested...\r\n");
tcpServer.Close();
tcpServer.Accept(e.requestID);
isconnected = true;
tcpServer.SendData("1"); //client side will receive this message for some reason
}
private void tcpServer_DataArrival(object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{
richTextBox1.AppendText("Receiving TCP Data...\r\n");
string inputstr="";
object data=(object)inputstr;
tcpServer.GetData(ref data);
richTextBox1.AppendText((string)data + "\r\n");
if (radioServer.Checked)
{
inputstr = (string)data;
ConnectModems();
sendingData = inputstr;
}
}
private void radioClient_CheckedChanged(object sender, EventArgs e)
{
if (radioClient.Checked)
{
groupContacts.Visible = false;
groupTests.Visible = false;
tcpServer.Close();
lblRole.Text = "CLIENT APP";
}
}
private void radioServer_CheckedChanged(object sender, EventArgs e)
{
if (radioServer.Checked)
{
tcpServer.Close();
groupContacts.Visible = true;
groupTests.Visible = true;
tcpServer.Listen();
lblRole.Text = "SERVER APP";
}
}
[/CODE]
|