patterncsharpMinor
Mimicing a TCP connection
Viewed 0 times
mimicingtcpconnection
Problem
I've recently started a new job and been drafted on to a project that's running late. My task was initially to add Bluetooth connectivity to a C# app, but at the request of a more senior colleague I've had to write a standalone executable to open up two Bluetooth connections and forward any data sent/received from the Bluetooth devices over two TCP connections. The Code also listens to the TCP connections and forwards messages on to the Bluetooth device.
When the Network connection is closed by the client the Bluetooth connection also dies, this was requested behaviour.
The code works as it's required to from the tests that I've done, but I don't have much experience with C# or Bluetooth. The implementation I have written spawns a few threads which I would ideally reduce, and I'm looking for ways to possibly improve reliability - although it's worked in testing I'd like to make this a bullet-proof as possible as it seems like it's going to form the crux of the product we're making.
I should add that the code uses the 32feet.Net library to find and connect to Bluetooth devices.
Main Method
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace BluetoothManager
{
class Program
{
static void Main(string[] args)
{
BTManager rover_btm = new BTManager();
BTManager base_btm = new BTManager();
base_btm.Port = 0xba5e;
rover_btm.Port = 17825;
base_btm.Name = "Base";
rover_btm.Name = "Rover";
base_btm.match = (args.Length >= 1 && args[0] != "") ? args[0] : "baseBT";
rover_btm.match = (args.Length >= 2 && args[1] != "") ? args[1] : "roverBT";
if (rover_btm.match == base_btm.match)
{
Console.WriteLine("WARNING: The rover and base names are the same, this can be problematic");
}
Console.WriteLine("Base Station match: " + base_btm.match);
Console.WriteLine("Rover match: " + rover_b
When the Network connection is closed by the client the Bluetooth connection also dies, this was requested behaviour.
The code works as it's required to from the tests that I've done, but I don't have much experience with C# or Bluetooth. The implementation I have written spawns a few threads which I would ideally reduce, and I'm looking for ways to possibly improve reliability - although it's worked in testing I'd like to make this a bullet-proof as possible as it seems like it's going to form the crux of the product we're making.
I should add that the code uses the 32feet.Net library to find and connect to Bluetooth devices.
Main Method
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace BluetoothManager
{
class Program
{
static void Main(string[] args)
{
BTManager rover_btm = new BTManager();
BTManager base_btm = new BTManager();
base_btm.Port = 0xba5e;
rover_btm.Port = 17825;
base_btm.Name = "Base";
rover_btm.Name = "Rover";
base_btm.match = (args.Length >= 1 && args[0] != "") ? args[0] : "baseBT";
rover_btm.match = (args.Length >= 2 && args[1] != "") ? args[1] : "roverBT";
if (rover_btm.match == base_btm.match)
{
Console.WriteLine("WARNING: The rover and base names are the same, this can be problematic");
}
Console.WriteLine("Base Station match: " + base_btm.match);
Console.WriteLine("Rover match: " + rover_b
Solution
In C#, you have auto-properties, which means instead of :
you can use only
It reduces noise in the code, I think you should use it in your
Also, you don't need to specify the parameterless constructor. If there is no constructor specified in C#, it will assume there is a public parameterless one.
The C# naming convention implies that method names should be in PascalCase, which means
Finally, I don't know if it is because you had trouble with formatting your code here, but pay caution to the white spacing of your code!
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}you can use only
public string Name{get;set;}It reduces noise in the code, I think you should use it in your
BTManager class since you don't do anything particular in your getters/setters.Also, you don't need to specify the parameterless constructor. If there is no constructor specified in C#, it will assume there is a public parameterless one.
The C# naming convention implies that method names should be in PascalCase, which means
nullEverything should be NullEverything (same for all the other methods). Try to be consistent in your private field naming, some of them have underscores at the beginning, some don't. There's no official documentation specifying you should use the underscore, so pick the way you want and stick with it.Finally, I don't know if it is because you had trouble with formatting your code here, but pay caution to the white spacing of your code!
Code Snippets
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}public string Name{get;set;}Context
StackExchange Code Review Q#61459, answer score: 4
Revisions (0)
No revisions yet.