How to create new socket connection
First step for socket programming is creating a new socket connection
Following is the code sample for creating a new socket connection
Following name spaces are required
using System;
using System.Net;
using System.Net.Sockets;
class MySocket {
public static void Main() {
IPAddress MyIPAddress = IPAddress.Parse("125.1.1.1");
IPEndPoint MyIPEndPoint = new IPEndPoint(MyIPAddress, 6000);
Socket MySocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine(MySocket.SocketType);
Console.WriteLine(MySocket.AddressFamily);
Console.WriteLine(MySocket.Blocking);
Console.WriteLine(MySocket.Connected);
MySocket.Blocking = false;
Console.WriteLine(MySocket.Blocking);
Console.WriteLine(MySocket.Connected);
MySocket.Bind(MyIPEndPoint);
IPEndPoint MyNewIPEndPoint = (IPEndPoint)MySocket.LocalEndPoint;
Console.WriteLine(MyNewIPEndPoint.ToString());
MySocket.Close();
}
}
Code Explanation
1. Create an instance of ipaddress
2. Create an instance of the ipendpoint
3. Create an instance of Socket
4. Display some properties of the created socket
5. make the blocking into false
6. Display some properties of the socket (Now you can see the difference)
7. create new endpoint by binding the old one
8. Display the endpoint
9. Close the socket
By
Nathan
