Socket connection
In this chapter you will learn:
- How to find out if a Socket is blocked or connected
- Bind Socket with an IPEndPoint
- Display the connected client IP address
- Socket connection
- Echo Client with UTF8 Encoding
Is Socket blocking or connected
using System;/*j a va2s . co m*/
using System.Net;
using System.Net.Sockets;
class MainClass
{
public static void Main()
{
IPAddress ia = IPAddress.Parse("127.0.0.1");
IPEndPoint ie = new IPEndPoint(ia, 8000);
Socket test = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("Blocking: {0}", test.Blocking);
test.Blocking = false;
Console.WriteLine("new Blocking: {0}",test.Blocking);
Console.WriteLine("Connected: {0}", test.Connected);
test.Close();
}
}
The code above generates the following result.
Bind Socket with an IPEndPoint
using System;/*from ja va2 s . c om*/
using System.Net;
using System.Net.Sockets;
class MainClass
{
public static void Main()
{
IPAddress ia = IPAddress.Parse("127.0.0.1");
IPEndPoint ie = new IPEndPoint(ia, 8000);
Socket test = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("Blocking: {0}", test.Blocking);
test.Bind(ie);
IPEndPoint iep = (IPEndPoint)test.LocalEndPoint;
Console.WriteLine("Local EndPoint: {0}",iep.ToString());
test.Close();
}
}
The code above generates the following result.
Display the connected client IP address
using System;/*from jav a 2s . c o m*/
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
string data;
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9999);
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ip);
socket.Listen(10);
Socket client = socket.Accept();
IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",newclient.Address, newclient.Port);
}
}
Socket connection
using System;/* ja v a 2 s .co m*/
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
IPAddress host = IPAddress.Parse("192.168.1.1");
IPEndPoint hostep = new IPEndPoint(host, 8000);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sock.Connect(hostep);
} catch (SocketException e) {
Console.WriteLine("Problem connecting to host");
Console.WriteLine(e.ToString());
sock.Close();
return;
}
sock.Close();
}
}
Echo Client with UTF8 Encoding
using System;/* ja v a 2 s . c o m*/
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
const int echoPort = 7;
[STAThread]
static void Main( string[] args )
{
Socket s = new Socket( AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp );
s.Connect( new IPEndPoint( IPAddress.Loopback, echoPort ) );
UTF8Encoding enc = new UTF8Encoding();
s.Send( enc.GetBytes( "test message" ) );
Byte[] buff = new Byte[ 1024 ];
s.Receive( buff );
System.Console.WriteLine( enc.GetString( buff ) );
}
}
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » Network