Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: R.a.B.B.i.T on June 13, 2004, 11:55 PM

Title: C# socket..hrm
Post by: R.a.B.B.i.T on June 13, 2004, 11:55 PM
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.
Title: Re:C# socket..hrm
Post by: Zeller on June 14, 2004, 01:17 AM

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:
Title: Re:C# socket..hrm
Post by: MyndFyre on June 14, 2004, 02:57 AM
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.
Title: Re:C# socket..hrm
Post by: K on June 14, 2004, 02:36 PM
Or if you're looking for a simpler interface you can use a TcpClient / TcpListener / UdpClient / UdpListener.  Just browse the namespaces.  It's fun.
Title: Re:C# socket..hrm
Post by: Fr0z3N on June 15, 2004, 09:06 PM

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!