I’m learning how to use C# sockets and am stumped at the moment–maybe someone can show me the error of my ways. I really haven’t grasped much about sockets yet.
I’ve written two programs, a server and a client. Right now they are simple programs meant to run on the same machine in order to learn about sockets. They don’t use .NET MF sockets but use the sockets of the fullblown .NET framework. Once working, I will incorporate the client into a .NET MF app using MF/GHI sockets to run on the Panda II. The server will remain as a .NET app running .NET sockets.
The server sets up a TcpListener and waits (with its AcceptTcpClient method) for a client to request connection. The client sends some test data to be read. When connected, the server reads the data (via its NetworkStream) and writes it to a text file. This simulates data being saved on a PC that was collected by the remote Panda.
When i run the programs (i start the server first), there is a connection but no bytes are read. When i step through the server program with the debugger, everything works just as expected and bytes are read from the client and written to the text file.
Here’s the server and client code, liberally smeared with WriteLines to the console and to log files. (Please ignore other problems with logic, code is not very functional or cleaned up.) Copies of the logs follow, both when running free (showing no bytes read) and when stepping through the server program w/ the debugger (showing 33 bytes read.)
Server:
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
class PBRDataServer
{
const int cServerIP = 65;
const string cPBRDataFile = "PBRData.dat";
const int cMaxClients = 5;
const int cPort = 2000;
const int cBufSize = 1024;
public static void Main()
{
IPAddress serverIP = new IPAddress(new byte[] { 192, 168, 1, cServerIP });
byte[] buf = new byte[cBufSize];
StreamWriter txtDataOut = new StreamWriter(@ cPBRDataFile);
TcpListener listener = new TcpListener(serverIP, cPort);
TcpClient client;
StreamWriter log = new StreamWriter(@ "ServerLog.txt");
listener.Start();
Console.WriteLine("Data server started, listening to port {0}", cPort);
Console.WriteLine("Listener local end point: {0}", listener.LocalEndpoint);
log.WriteLine("Data server started, listening to port {0}", cPort);
log.WriteLine("Listener local end point: {0}", listener.LocalEndpoint);
//while (true) {
client = listener.AcceptTcpClient(); // wait here until client connects
Console.WriteLine("Connected socket local end point: {0}", client.Client.LocalEndPoint);
Console.WriteLine("Connected socket remote end point: {0}", client.Client.RemoteEndPoint);
log.WriteLine("Connected socket local end point: {0}", client.Client.LocalEndPoint);
log.WriteLine("Connected socket remote end point: {0}", client.Client.RemoteEndPoint);
try
{
int count = client.Available;
Console.WriteLine("Bytes available: {0}", count);
log.WriteLine("Bytes available: {0}", count);
client.GetStream().Read(buf, 0, count);
Console.WriteLine("Data server read {0} bytes", count);
log.WriteLine("Data server read {0} bytes", count);
count = Encoding.UTF8.GetCharCount(buf);
txtDataOut.WriteLine(Encoding.UTF8.GetChars(buf), 0, count);
Console.WriteLine("Data server wrote {0} chars to file {1}", count, cPBRDataFile);
log.WriteLine("Data server wrote {0} chars to file {1}", count, cPBRDataFile);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
log.WriteLine(e.Message);
}
Console.WriteLine("Data server disconnected from client {0}", client.Client.RemoteEndPoint);
log.WriteLine("Data server disconnected from client {0}", client.Client.RemoteEndPoint);
client.Close();
//}
Console.ReadLine();
txtDataOut.Close();
log.Close();
}
}
Client:
using System;
using System.IO;
using System.Threading;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace TestClient_WCA
{
public class Program
{
private struct DataPoint {
public float value;
public DateTime time;
public DataPoint(float value, DateTime time)
{
this.value = value;
this.time = time;
}
}
// constants
const int cServerIP = 65;
const int cClientIP = 222;
const int cPort = 2000;
static string label = "Test data"; // text id for this data
static DataPoint current = new DataPoint(5.5f, DateTime.Now); // test data
static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static StreamWriter log = new StreamWriter(@ "ClientLog.txt");
public static void Main()
{
IPEndPoint serverEndPoint = new IPEndPoint(new IPAddress(new byte[] { 192, 168, 1, cServerIP }), cPort);
Console.WriteLine("Connecting to server...");
log.WriteLine("Connecting to server...");
socket.Connect(serverEndPoint);
Console.WriteLine("Connected? {0}", socket.Connected);
Console.WriteLine("Socket local end point: {0}", socket.LocalEndPoint);
Console.WriteLine("Socket remote end point: {0}", socket.RemoteEndPoint);
log.WriteLine("Connected? {0}", socket.Connected);
log.WriteLine("Socket local end point: {0}", socket.LocalEndPoint);
log.WriteLine("Socket remote end point: {0}", socket.RemoteEndPoint);
new Thread(new ThreadStart(Write)).Start(); // start new thread to write data to network
Console.ReadLine();
log.Close();
}
// save data to file via network
private static void Write()
{
byte[] bytesToSend = Encoding.UTF8.GetBytes(label + "," +
current.value.ToString() + "," +
current.time.ToString());
Console.WriteLine("Inside new thread");
Console.WriteLine("sending {0} bytes to {1}", bytesToSend.Length, socket.RemoteEndPoint);
log.WriteLine("Inside new thread");
log.WriteLine("sending {0} bytes to {1}", bytesToSend.Length, socket.RemoteEndPoint);
try
{
socket.Send(bytesToSend);
}catch (Exception e) {
Console.WriteLine(e.Message);
log.WriteLine(e.Message);
}
}
}
}
Log files
Server Log without stepping through program:
Data server started, listening to port 2000
Listener local end point: 192.168.1.65:2000
Connected socket local end point: 192.168.1.65:2000
Connected socket remote end point: 192.168.1.65:49329
Bytes available: 0
Data server read 0 bytes
Data server wrote 1024 chars to file PBRData.dat
Data server disconnected from client 192.168.1.65:49329
Client log without stepping through program:
Connecting to server…
Connected? True
Socket local end point: 192.168.1.65:49329
Socket remote end point: 192.168.1.65:2000
Inside new thread
sending 33 bytes to 192.168.1.65:2000
Server Log with stepping through program with debugger:
Data server started, listening to port 2000
Listener local end point: 192.168.1.65:2000
Connected socket local end point: 192.168.1.65:2000
Connected socket remote end point: 192.168.1.65:49330
Bytes available: 33
Data server read 33 bytes
Data server wrote 1024 chars to file PBRData.dat
Data server disconnected from client 192.168.1.65:49330
Client log with stepping through server program with debugger:
Connecting to server…
Connected? True
Socket local end point: 192.168.1.65:49330
Socket remote end point: 192.168.1.65:2000
Inside new thread
sending 33 bytes to 192.168.1.65:2000