• Welcome to Valhalla Legends Archive.
 

C# socket..hrm

Started by R.a.B.B.i.T, June 13, 2004, 11:55 PM

Previous topic - Next topic

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.

Zeller

#1

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:

MyndFyre

#2
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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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.

Fr0z3N


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!