UDP_Client_Server

Download Report

Transcript UDP_Client_Server

UDP Client-Server
The Socket Class
The Socket class provides a set of methods and properties for network
communications. The Socket class allows you to perform both synchronous
and asynchronous data transfer using any of the communication protocols
listed in the ProtocolType enumeration.
The Socket class follows the .NET Framework naming pattern for
asynchronous methods. For example, the synchronous Receive method
corresponds to the asynchronous BeginReceive and EndReceive methods.
The UdpClient Class
One of the simplest types of machine-to-machine communication is the client-toserver with a static IP address.
In the typical application, the server accepts a connection from any client, but the
client must know the IP address of the server.
The UdpClient class provides simple methods for sending and receiving
connectionless UDP datagrams in blocking synchronous mode.
Because UDP is a connectionless transport protocol, you do not need to establish
a remote host connection prior to sending and receiving data. You do,
however, have the option of establishing a default remote host in one of the
following two ways:
1. Create an instance of the UdpClient class using the remote host name and
port number as parameters.
2. Create an instance of the UdpClient class and then call the Connect method.
UdpClient.Connect Method
The Connect method establishes a default remote host using the values
specified in the port and hostname parameters. Once established, you do not
have to specify a remote host in each call to the Send method.
Establishing a default remote host is optional. Specifying a default remote
host limits you to that host only. If you want to send datagrams to a different
remote host, you must make another call to the Connect method or create
another UdpClient without a default remote host.
IPEndPoint
The IPEndPoint class contains the host and local or remote port information
needed by an application to connect to a service on a host.
By combining the host's IP address and port number of a service, the
IPEndPoint class forms a connection point to a service.
IPAddress.Any provides an IP address that indicates that the server must
listen for client activity on all network interfaces. This field is read-only. The
Any field is equivalent to 0.0.0.0 in dotted-quad notation.
Communicating with a Byte Array
ProtocolType enumeration
Protocol types supported by the .NET Framework
Ggp
Icmp
Idp
Igmp
IP
IPv6
Ipx
ND
Pup
Raw
Spx
SpxII
Tcp
Udp
Unknown
Unspecified
Gateway To Gateway Protocol
Internet Control Message Protocol
Internet Datagram Protocol
Internet Group Management Protocol
Internet Protocol
Internet Protocol version 6 (IPv6)
Internet Packet Exchange Protocol
Net Disk Protocol (unofficial)
PARC Universal Packet Protocol
Raw IP packet protocol
Sequenced Packet Exchange protocol
Sequenced Packet Exchange version 2 protocol
Transmission Control Protocol
User Datagram Protocol
Unknown protocol
Unspecified protocol
UDP Client
using
using
using
using
System;
System.Text;
System.Net;
System.Net.Sockets;
namespace UDPClient
{
class Class1
{
static void Main(string[] args)
{
string sendStr = "";
UdpClient theClient = new UdpClient("IP here", 9050);
while (!sendStr.Trim().ToUpper().Equals("END"))
{
Console.Write("# ");
sendStr = Console.ReadLine();
byte[] myData = new byte[1024];
myData = Encoding.ASCII.GetBytes(sendStr);
theClient.Send(myData, myData.Length);
}
theClient.Close();
}
}
}
UDP Server (Listener)
using
using
using
using
System;
System.Text;
System.Net;
System.Net.Sockets;
namespace UDPServer
{
class Class1
{
static void Main(string[] args)
{
string rcvData = "";
IPEndPoint IPEP = new IPEndPoint(IPAddress.Any, 9050);
UdpClient theSock = new UdpClient(IPEP);
IPEndPoint fromClient;
while (!rcvData.Trim().ToUpper().Equals("END"))
{
byte[] myData = new byte[1024];
fromClient = new IPEndPoint(IPAddress.Any, 0);
myData = theSock.Receive(ref fromClient);
rcvData = Encoding.ASCII.GetString(myData);
Console.WriteLine(fromClient.ToString() + " " + rcvData);
}
theSock.Close();
}
}
}
Peer-to-Peer Communications
using
using
using
using
using
System;
System.Threading;
System.Net;
System.Net.Sockets;
System.Text;
namespace ClientServer_1
{
class Chat1
{
private static Thread clientThread;
private static Thread serverThread;
static void Main(string[] args)
{
CreateThreads();
}
private static void CreateThreads()
{
clientThread = new Thread(new ThreadStart(RunClientThread));
clientThread.Start();
}
serverThread = new Thread(new ThreadStart(RunServerThread));
serverThread.Start();
Client Thread
private static void RunClientThread()
{
string sendStr="";
UdpClient theClient= new UdpClient("127.0.0.1",9050);
while (!sendStr.Trim().ToUpper().Equals("END"))
{
sendStr=Console.ReadLine();
byte[] myData= new byte[10];
myData=Encoding.ASCII.GetBytes(sendStr);
theClient.Send(myData,myData.Length);
}
theClient.Close();
}
Server Thread
private static void RunServerThread()
{
string rcvData="";
IPEndPoint IPEP= new IPEndPoint(IPAddress.Any,9051);
UdpClient theSock= new UdpClient(IPEP);
IPEndPoint fromClient;
while (!rcvData.Trim().ToUpper().Equals("END"))
{
byte[] myData= new byte[10];
fromClient= new IPEndPoint(IPAddress.Any,0);
myData= theSock.Receive(ref fromClient);
rcvData=Encoding.ASCII.GetString(myData);
Console.WriteLine(fromClient.ToString() + " " + rcvData);
}
theSock.Close();
}
}
}
Summary
User Datagram Protocol (UDP) - permits a simple method of communication
between two computers
UDP does not check for transmission errors
Client must know the IP address of the intended Server
Server accepts connection from any Client
The .NET Socket provides a set of methods for network communication
Multi-Threaded Programming can be used for Peer-to-Peer Communication