濡備綍杩愮敤 C#璁剧疆缃戠粶閫氫俊鐨勬敹鍙戝姛鑳?/span> [闂鐐规暟锛?0鍒哴
杩愮敤C#璁剧疆缃戠粶閫氫俊鐨勬敹鍙戝姛鑳斤紝鍙鑳界畝鍗曠殑涓庣浉搴旂殑IP鍦板潃鑱旂綉锛屽彂鍑虹畝鍗曚俊鎭紝瀵规柟鑳藉鎺ユ敹鍒板氨鍙互 --------------------编程问答-------------------- Service
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDPServer
{
class Program
{
private static Socket newsock;
private static IPEndPoint iPEndPoint;
private static EndPoint Remote;
static void Main(string[] args)
{
int i = 0;
try
{
while (true)
{
int port = 2001;
string host = "192.168.10.9";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//鍒涘缓涓€涓猄ocket绫?br /> s.Bind(ipe);//缁戝畾2000绔彛
//s.Listen(0);//寮€濮嬬洃鍚?br /> //Console.WriteLine("Wait for connect");
//Socket temp = s.Accept();//涓烘柊寤鸿繛鎺ュ垱寤烘柊鐨凷ocket銆?br /> //Console.WriteLine("Get a connect");
string recvStr = "";
byte[] recvBytes = new byte[4096];
int bytes;
EndPoint enp = (EndPoint)ipe;
bytes = s.ReceiveFrom(recvBytes, ref enp);//浠庡鎴风鎺ュ彈淇℃伅
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("Server Get Message:{0}", recvStr);//鎶婂鎴风浼犳潵鐨勪俊鎭樉绀哄嚭鏉?br />
string sendStr = " Ok! Client Send Message Sucessful!";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
s.SendTo(bs, enp);//杩斿洖瀹㈡埛绔垚鍔熶俊鎭?br /> Console.Write("test :{0} times", i++);
//temp.Close();
s.Close();
}
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException:{0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException:{0}", e);
}
Console.WriteLine("PressEntertoExit");
Console.ReadLine();
}
/// <summary>
/// 寮€濮嬬洃鍚?br /> /// </summary>
public static void StartListen(string strIpAddress, string strPortNo)
{
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(strIpAddress), int.Parse(strPortNo));
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//缁戝畾缃戠粶鍦板潃
newsock.Bind(ipep);
newsock.ReceiveBufferSize = 1024 * 10000;
//寰楀埌瀹㈡埛鏈篒P
iPEndPoint = new IPEndPoint(IPAddress.Parse(strIpAddress), int.Parse(strPortNo));
Remote = (EndPoint)(iPEndPoint);
}
/// <summary>
/// 鎺ユ敹鏁版嵁
/// </summary>
/// <returns></returns>
public static byte[] GetMessage()
{
byte[] data = new byte[4096];
//鎺ュ彈淇℃伅
newsock.Receive(data, 0, 4096, SocketFlags.None);
//int recv = newsock.ReceiveFrom(data, ref Remote);
//return Encoding.ASCII.GetBytes(data, 0, recv);
return data;
}
}
}
UDPClient
--------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace UDPClient
{
class Program
{
static void Main(string[] args)
{
try
{
while (true)
{
int port = 2001;
string host = "192.168.10.9";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);//鎶奿p鍜岀鍙h浆鍖栦负IPEndPoint瀹炰緥
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//鍒涘缓涓€涓猄ocket
//Console.WriteLine("Conneting...");
c.Connect(ipe);//杩炴帴鍒版湇鍔″櫒
Console.Write("Input sending message(q exit):");
string sendStr = Console.ReadLine();
if ("q".Equals(sendStr))
{
break;
}
EndPoint enp = (EndPoint)ipe;
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.SendTo(bs, enp);//鍙戦€佹祴璇曚俊鎭?br /> string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.ReceiveFrom(recvBytes, ref enp);//浠庢湇鍔″櫒绔帴鍙楄繑鍥炰俊鎭?br /> recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("Client Get Message:{0}", recvStr);//鏄剧ず鏈嶅姟鍣ㄨ繑鍥炰俊鎭?br /> c.Close();
}
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException:{0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException:{0}", e);
}
Console.WriteLine("PressEntertoExit");
Console.ReadLine();
}
}
}
瀛︿範锛? --------------------编程问答-------------------- mark 鐗沊