Csharp/C Sharp/Network/Socket Connection
Содержание
Creating Socket Connections
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client.Chapter_14___Networking_and_WWW
{
public class CreatingSocketConnections
{
[STAThread]
static void Main(string[] args)
{
TcpClient MyClient = new TcpClient();
MyClient.Connect("http://www.nfex.ru", 10000);
NetworkStream MyNetStream = MyClient.GetStream();
if(MyNetStream.CanWrite && MyNetStream.CanRead)
{
// Does a simple write.
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there");
MyNetStream.Write(sendBytes, 0, sendBytes.Length);
// Reads the NetworkStream into a byte buffer.
byte[] bytes = new byte[MyClient.ReceiveBufferSize];
MyNetStream.Read(bytes, 0, (int) MyClient.ReceiveBufferSize);
// Returns the data received from the host to the console.
string returndata = Encoding.ASCII.GetString(bytes);
Console.WriteLine("This is what the host returned to you: " + returndata);
}
else if (!MyNetStream.CanRead)
{
Console.WriteLine("You can not write data to this stream");
MyClient.Close();
}
else if (!MyNetStream.CanWrite)
{
Console.WriteLine("You can not read data from this stream");
MyClient.Close();
} } }
}
Multi Receive
/*
C# Network Programming
by Richard Blum
Publisher: Sybex
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class MultiRecv
{
public static void Main()
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Console.WriteLine("Ready to receive...");
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
EndPoint ep = (EndPoint)iep;
sock.Bind(iep);
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption(IPAddress.Parse("224.100.0.1")));
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
sock.Close();
}
}
Multi Send
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class MultiSend
{
public static void Main()
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050);
byte[] data = Encoding.ASCII.GetBytes("This is a test message");
server.SendTo(data, iep);
server.Close();
}
}
New Multi Send
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class NewMultiSend {
public static void Main()
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9051);
IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050);
server.Bind(iep);
byte[] data = Encoding.ASCII.GetBytes("This is a test message");
server.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.100.0.1")));
server.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 50);
server.SendTo(data, iep2);
server.Close();
}
}
new Socket
using System;
using System.Net;
using System.Net.Sockets;
class SockProp {
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(test.AddressFamily);
Console.WriteLine(test.SocketType);
Console.WriteLine(test.ProtocolType);
Console.WriteLine(test.Blocking);
test.Blocking = false;
Console.WriteLine(test.Blocking);
Console.WriteLine(test.Connected);
test.Bind(ie);
IPEndPoint iep = (IPEndPoint)test.LocalEndPoint;
Console.WriteLine(iep.ToString());
test.Close();
}
}
Socket Connect, Send
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class SocketExcept {
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;
}
try {
sock.Send(Encoding.ASCII.GetBytes("testing"));
} catch (SocketException e) {
Console.WriteLine("Problem sending data");
Console.WriteLine(e.ToString());
sock.Close();
return;
}
sock.Close();
}
}
Socket Exception
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SocketExcept
{
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;
}
try {
sock.Send(Encoding.ASCII.GetBytes("testing"));
} catch (SocketException e) {
Console.WriteLine("Problem sending data");
Console.WriteLine( e.ToString());
sock.Close();
return;
}
sock.Close();
}
}
Socket property
using System;
using System.Net;
using System.Net.Sockets;
public class SockProp {
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("AddressFamily: {0}",test.AddressFamily);
Console.WriteLine("SocketType: {0}",test.SocketType);
Console.WriteLine("ProtocolType: {0}",test.ProtocolType);
Console.WriteLine("Blocking: {0}", test.Blocking);
test.Blocking = false;
Console.WriteLine("new Blocking: {0}",test.Blocking);
Console.WriteLine("Connected: {0}", test.Connected);
test.Bind(ie);
IPEndPoint iep = (IPEndPoint)test.LocalEndPoint;
Console.WriteLine("Local EndPoint: {0}",iep.ToString());
test.Close();
}
}
Thread and socket
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class Advertise {
public static void Main()
{
Advertise server = new Advertise();
}
public Advertise()
{
Thread advert = new Thread(new ThreadStart(sendPackets));
advert.IsBackground = true;
advert.Start();
Console.Write("Press Enter to stop");
string data = Console.ReadLine();
}
void sendPackets()
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 9050);
string hostname = Dns.GetHostName();
byte[] data = Encoding.ASCII.GetBytes(hostname);
while (true)
{
sock.SendTo(data, iep);
Thread.Sleep(60000);
}
}
}