Академический Документы
Профессиональный Документы
Культура Документы
Csharp
Mohamed Khatiri 1988
Year : 2010
3 rd Computer Science
Mido-kok@hotmail.fr
Socket connection
using System;
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.Strea
m, ProtocolType.Tcp);
try
{
sock.Connect(hostep);
sock.Send(Encoding.ASCII.GetBytes("testing"));
} catch (SocketException e) {
Console.WriteLine("Problem connecting to host");
Console.WriteLine(e.ToString());
sock.Close();
return;
}
sock.Close();
}
}
2
Listen for Socket Request in Thread
using System;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
while( true ) {
using( Socket newConnection = listenSock.Accept() ) {
// Send the data.
byte[] msg = Encoding.UTF8.GetBytes( "Hello World!" );
newConnection.Send( msg, SocketFlags.None );
}
}
}
3
Simple Tcp server: receive data from a client
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any,9999);
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,
ProtocolType.Tcp);
socket.Bind(ip);
socket.Listen(10);
Console.WriteLine("Waiting for a client...");
Socket client = socket.Accept();
IPEndPoint clientep =(IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",clientep.Address ,
clientep.Port);
while(true)
{
data = new byte[1024];
int receivedDataLength = client.Receive(data);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, receivedDataLength));
client.Send(data, receivedDataLength, SocketFlags.None);
}
4
Simple Tcp server: send data to the client
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any,9999);
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stre
am, ProtocolType.Tcp);
socket.Bind(ip);
socket.Listen(10);
Console.WriteLine("Waiting for a client...");
Socket client = socket.Accept();
IPEndPoint clientep =(IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",clientep.Address,
clientep.Port);
5
Simple Tcp Client: send data to the server
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
try
{
server.Connect(ip);
} catch (SocketException e){
Console.WriteLine("Unable to connect to server.");
return;
}
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
6
Simple Tcp Client: receive data from server
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
try
{
server.Connect(ip);
} catch (SocketException e){
Console.WriteLine("Unable to connect to server.");
return;
}
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
7
Simple Udp Client: send and receive
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
byte[] data = new byte[1024];
string input, stringData;
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
server.Close();
}
}
8
Simple Udp server
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
int receivedDataLength;
byte[] data = new byte[1024];
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9999);
socket.Bind(ip);
while(true)
{
data = new byte[1024];
receivedDataLength = socket.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, receivedDataLength));
socket.SendTo(data, receivedDataLength, SocketFlags.None, Remote);
}
}
}
9
Create a NetworkStream from a Socket
using System;
using System.IO;
using System.Net.Sockets ;
class MainClass
{
public static void Main()
{
TcpListener tcpl = new TcpListener(9999);
tcpl.Start();
for (;;)
{
Socket newSocket = tcpl.AcceptSocket();
if (newSocket.Connected)
{
NetworkStream ns = new NetworkStream(newSocket);
ns.Flush();
ns.Close();
}
newSocket.Close();
break;
}
}
10
NetworkStream TcpClient
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
try
{
server.Connect(ip);
} catch (SocketException e){
Console.WriteLine(e.ToString());
return;
}
if (ns.CanRead){
byte[] data = new byte[1024];
int receivedDataLength = ns.Read(data, 0, data.Length);
string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
Console.WriteLine(stringData);
}else{
Console.WriteLine("Error: Can't read from this socket");
}
ns.Close();
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
11
Network Streaming Server
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
for ( ; ; )
{
Socket socketForClient = tcpListener.AcceptSocket();
SendFileToClient( socketForClient );
socketForClient.Close();
break;
}
}
12
Network Streaming Client
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
if ( outputString != null )
{
Console.WriteLine( outputString );
}
}while ( outputString != null );
networkStream.Close();
}
}
13
Use NetworkStream to read and write to a server
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
try
{
server.Connect(ip);
} catch (SocketException e){
Console.WriteLine(e.ToString());
return;
}
while(true)
{
byte[] data = new byte[1024];
string input = Console.ReadLine();
if (ns.CanWrite){
ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
ns.Flush();
}
ns.Close();
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
14
Create NetworkStream from TcpClient
using System;
using System.IO;
using System.Net.Sockets ;
class MainClass
{
public static void Main()
{
TcpClient newSocket = new TcpClient("localhost", 50001);
NetworkStream ns = newSocket.GetStream();
ns.Close();
newSocket.Close();
15
TcpListener Sample
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
int receivedDataLength;
byte[] data = new byte[1024];
while(true)
{
data = new byte[1024];
receivedDataLength = ns.Read(data, 0, data.Length);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, receivedDataLength));
ns.Write(data, 0, receivedDataLength);
}
ns.Close();
client.Close();
socket.Stop();
}
}
16