Valhalla Legends Forums Archive | .NET Platform | C# socket..hrm

AuthorMessageTime
R.a.B.B.i.T
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.
June 14, 2004, 4:55 AM
Zeller
[code]
Socket mySocket;
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
[/code]

Be sure to put this at the top of your code
[code]
using System.Net.Sockets;
[/code]

EDIT:
June 14, 2004, 6:17 AM
Myndfyr
You could also just:

[code]
System.Net.Sockets.Socket mySocket = new
System.Net.Sockets.Socket(
System.Net.AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp
);
[/code]

Or:

[code]
Dim mySocket As New System.Net.Sockets.Socket( System.Net.AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
[/code]

Or even:

[code]
#using <mscorlib.dll>
using namespace System::Net::Sockets;

Socket *mySocket;

mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
[/code]

And still:
[code]
var mySocket : System.Net.Sockets.Socket = new System.Net.Sockets.Socket(
AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
[/code]

etcetera.
June 14, 2004, 7:57 AM
K
Or if you're looking for a simpler interface you can use a TcpClient / TcpListener / UdpClient / UdpListener. Just browse the namespaces. It's fun.
June 14, 2004, 7:36 PM
Fr0z3N
[code]
using System;
using System.Net.Sockets;
[/code]

[code]

         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);
         }
[/code]

Enjoy!
June 16, 2004, 2:06 AM

Search