I'm trying to write a little application in C# which makes a tcp-ip connection to a linux server. When a connection is made and data is send over the line, the linux server makes a call over a serial modem. The number which is being dialed is a number which is connected to another modem in the Windows PC. I want to be able to detect the incoming call on the Windows PC.
Every component runs perfectly when tested independantly:
- Sending data to Linux server works.
- Making a call on Linux server works
- Detecting incoming call on Windows PC works
The only problem comes up when I try to combine 1 and 3. Everytime I've send the data to the Linux server I receive a 'Safe handle has been closed' error message (right after the m_socClient.Connect(remoteEndPoint)). When I comment out all the serial port stuff it seems to run OK (but of course no detection of traffic on the serial port is done)...
Below is the code I've written. Would be nice if someone can point me in the right direction...
Every component runs perfectly when tested independantly:
- Sending data to Linux server works.
- Making a call on Linux server works
- Detecting incoming call on Windows PC works
The only problem comes up when I try to combine 1 and 3. Everytime I've send the data to the Linux server I receive a 'Safe handle has been closed' error message (right after the m_socClient.Connect(remoteEndPoint)). When I comment out all the serial port stuff it seems to run OK (but of course no detection of traffic on the serial port is done)...
Below is the code I've written. Would be nice if someone can point me in the right direction...
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO.Ports;
using System.Runtime;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
//BackgroundWorker backgroundWorker1 = new BackgroundWorker();
public Form1()
{
InitializeComponent();
try
{
SerialPort port = new SerialPort("COM10", 2400, Parity.None, 8, StopBits.One);
//port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
}
catch { MessageBox.Show("Exception while opening port"); }
}
//public SerialPort port = new SerialPort("COM10", 2400, Parity.None, 8, StopBits.One);
//[STAThread]
public static void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
System.IO.Ports.SerialPort s = (System.IO.Ports.SerialPort)sender;
//s.Open();
// string message = s.ReadLine();
string message = "RECEIVED";
MessageBox.Show(message);
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Starting";
send_cmd();
}
public void send_cmd()
{
MessageBox.Show("Dialing");
Socket m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
string szIPSelected = "192.168.0.100";
string szPort = "2001";
int alPort = System.Convert.ToInt16(szPort, 10);
MessageBox.Show("Did conversion");
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
MessageBox.Show("Did IP addresses");
m_socClient.Connect(remoteEndPoint);
MessageBox.Show("Did connecting");
string szData = "Go";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(szData);
m_socClient.Send(byData);
MessageBox.Show("Did sending");
//Dispose();
}
}
}