Ehe..I just started tinkering around in C# trying to figure it out..but I can't make heads or tails of how to declare a socket. I know I have to use System.Net.Sockets.Socket, but I'm not sure how.
Socket mySocket;
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Be sure to put this at the top of your code
using System.Net.Sockets;
EDIT:
You could also just:
System.Net.Sockets.Socket mySocket = new
System.Net.Sockets.Socket(
System.Net.AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp
);
Or:
Dim mySocket As New System.Net.Sockets.Socket( System.Net.AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Or even:
#using <mscorlib.dll>
using namespace System::Net::Sockets;
Socket *mySocket;
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
And still:
var mySocket : System.Net.Sockets.Socket = new System.Net.Sockets.Socket(
AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
etcetera.
Or if you're looking for a simpler interface you can use a TcpClient / TcpListener / UdpClient / UdpListener. Just browse the namespaces. It's fun.
using System;
using System.Net.Sockets;
try
{
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(address);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, port);
mySocket.Connect(remoteEndPoint);
String sData = "test";
byte[] bData = System.Text.Encoding.ASCII.GetBytes(sData);
mySocket.Send(bData);
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
Enjoy!