C# socket connection transmits the first message only

August 13, 2012 at 15:43:34
Specs: Windows XP
Hello,
I am trying to put a program together which reads messages from an intercom and transmits them via sockets over the Inter-/Intranet. The receiving part of the program (serial communication) works and the incoming messages are shown on the console but the socket transmits the first message only, nothing afterwards. Can somebody please help me to change the program, so it works continually. Thank you!
My code is:

using System;
using System.Text;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;

namespace CommSlave
{
class Program
{
static SerialPort cPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
static int Port = 55455;
static string IPv4 = "192.168.2.109";
static string Parkhaus = "3002";
public static string cMeldungOld;

public static void Main(string[] args)
{
cPort.DataReceived += new SerialDataReceivedEventHandler(cPort_DataReceived);
cPort.Open();
while (true)
{ }
}

private static void cPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string cMeldung = cPort.ReadExisting();
if (cMeldung.Length == 20)
{
if (cMeldung.Equals(cMeldungOld))
{}
else
{
cMeldungOld = cMeldung;
cMeldung = cMeldung.Substring(1, 18);
Console.WriteLine(cMeldung);
Console.WriteLine();

IPAddress ipAdr = IPAddress.Parse(IPv4);
IPEndPoint ipEo = new IPEndPoint(ipAdr, Port);
Socket sock = null;
sock = new Socket(ipEo.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ipEo);
if (sock.Connected)
{
try
{
string request = cMeldung + Parkhaus + "\r\n";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
sock.Send(bytesSent, bytesSent.Length, SocketFlags.None);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}
}
}
}


See More: C# socket connection transmits the first message only

Report •


#1
August 14, 2012 at 18:27:58
Two general rules:

1) If you're going to post code in a discussion forum, use the board's method for keeping indentation. This board uses < pre> tags.

2) If an object implements IDisposable, call Dispose() when you're done using it. Thankfully, C# makes that easy with the keyword using.

How To Ask Questions The Smart Way


Report •

#2
August 15, 2012 at 08:06:47
Hallo Razor2.3,
thanks for your comment. I am not familiar with HTML (pre tag), however I formatted the code. I've thought that whoever will help me will paste the code into the C# editor which automatically formats it. Concerning IDisposable, I don't want to get clogged with further C# misunderstandings. I told you that I program in WinDev/Webdev which cover C#, ASP.NET, ADO.NET, HTML, Java, PHP etc, so I normally don't use any of these languages and tools. This current program needs to be written in C# by customer request. As you probably also experienced in your early days, it is not easy to dig C#. Would you please help me with the code below?

using System;
using System.Text;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;

namespace CommSlave
{
class Program
{
static SerialPort cPort = new SerialPort("COM1", 19200, 8, Parity.none, StopBits.One);
static int Port = 55455;
static string IPv4 = "192.168.2.109";
static string Parkhaus = "3002";
public static string cMeldungOld;

public static void Main(string[] args)
{
cPort.DataReceived += new SerialDataReceivedEventHandler(cPort_DataReceived);
cPort.Open();
while (true)
{ }
}

private static void cPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string cMeldung = cPort.ReadExisting();
if (cMeldung.Length == 20)
{
if (cMeldung.Equals(cMeldungOld))
{}
else
{
cMeldungOld = cMeldung;
cMeldung = cMeldung.Substring(1, 18);
Console.WriteLine(cMeldung);
Console.WriteLine();

IPAddress ipAdr = IPAddress.Parse(IPv4);
IPEndPoint ipEo = new IPEndPoint(ipAdr, Port);
Socket sock = null;
sock = new Socket(ipEo.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ipEo);
if (sock.Connected)
{
try
{
string request = cMeldung + Parkhaus + "\r\n";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
sock.Send(bytesSent, bytesSent.Length, SocketFlags.None);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}
}
}
}


Report •

#3
August 15, 2012 at 08:09:04
Well, it took out the blanks I tried to format it with. Please paste the code into the C# editor, it will properly format it. Thanks!

Report •

Related Solutions

#4
August 16, 2012 at 06:30:55
I am not familiar with HTML (pre tag)
Concerning IDisposable, I don't want to get clogged with further C# misunderstandings.
That's not helping yourself. That's shutting down until you get an answer from those who have no real incentive to give you one.

Socket sock = null;
sock = new Socket(ipEo.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

using (var sock = new Socket(ipEo.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) {

I'll leave it up to you to place the closing bracket.

How To Ask Questions The Smart Way


Report •


Ask Question