Click here to Skip to main content
15,896,201 members

Moving from Winsock to Net.Sockets.Socket

TheRedEye asked:

Open original thread
I have an ethernet device (PIC microcontroller) that is programmed to receive simple commands via ethernet. I have it working perfectly using the Winsock2005DLL.Winsock control. I'm trying without success to convert the code to use the System.Sockets.Socket class instead. Where am I missing ?

With the following 'Winsock' code, a connection is made and the device responds correctly to the hex commands:
C#
// make the connection
Winsock2005DLL.Winsock ws = new Winsock2005DLL.Winsock();
ws.RemotePort = 8888;
ws.RemoteServer = "10.0.0.10";
ws.Connect();

//prepare the data for sending
string s = "";
char cmd = (char)'\x00C0';
char[] bData = new char[] { (char)'\x00FE', (char)'\x00C0', (char)'\x00FF' };
foreach (char c in bData)
       s += c.ToString();

// send the data
ws.Send(s);


This 'Socket' code however, creates the connection successfully, but the device DOESN'T respond to the hex data that is sent.
C#
// make the connection
System.Net.EndPoint ep = new IPEndPoint(IPAddress.Parse("10.0.0.10"), 8888);
System.Net.Sockets.Socket sckt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sckt.Connect(ep);

// prepare the data for sending
string s = "";
char cmd = (char)'\x00C0';
char[] bData = new char[] { (char)'\x00FE', (char)'\x00C0', (char)'\x00FF' };
foreach (char c in bData)
       s += c.ToString();
byte[] bitData = System.Text.Encoding.ASCII.GetBytes(bData);

// send the data
sckt.Send(bitData);


Any help would be greatly appreciated.
Tags: C#, Sockets

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900