本文整理汇总了C#中UdpClient.Connect方法的典型用法代码示例。如果您正苦于以下问题:C# UdpClient.Connect方法的具体用法?C# UdpClient.Connect怎么用?C# UdpClient.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UdpClient
的用法示例。
在下文中一共展示了UdpClient.Connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
void Start()
{
client = new UdpClient(listenPort);
client.Connect(scHostname,scPort);
Send("/notify", 1);
StartCoroutine( Listen() );
}
示例2: CreateConnection
private void CreateConnection(NetworkOutboundDestination dest)
{
IPAddress ServerIP;
int ServerPort; //Temporary
switch (dest)
{
case NetworkOutboundDestination.LoginServer:
//NetConnection = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
ServerIP = IPAddress.Parse(GlobalValues.LOGIN_SERVER_IP);
ServerPort = GlobalValues.LOGIN_PORT;
break;
case NetworkOutboundDestination.MainServer:
//NetConnection = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
ServerIP = IPAddress.Parse(GlobalValues.MAIN_SERVER_IP);
ServerPort = GlobalValues.MAIN_PORT;
break;
default:
throw new System.Exception("Attempting to create connection with non-existant server!");
}
currentConnectionEP = new IPEndPoint(ServerIP, ServerPort);
sock = new UdpClient();
sock.Connect(currentConnectionEP);
}
示例3: Main
public static void Main()
{
string addr = "127.0.0.1";
int port = 7980;
short cmd = 1;
short seq = 0;
string msg = "Hello";
byte[] payload = Encoding.ASCII.GetBytes(msg);
byte[] packet = Packet.Create(cmd, seq, payload);
UdpClient udp = new UdpClient();
var ip = IPAddress.Parse(addr);
IPEndPoint ep = new IPEndPoint(ip, port);
udp.Connect(ep);
// send
udp.Send(packet, packet.Length);
// receive
bool done = false;
while (!done) {
if (udp.Available <= 0) {
IPEndPoint ep2 = new IPEndPoint(0, 0);
byte[] packet2 = udp.Receive(ref ep2);
Console.WriteLine("packet size: {0}", packet2.Length);
Dictionary<string, object> parsed = Packet.Parse(packet2);
foreach (KeyValuePair<string, object> item in parsed) {
Console.WriteLine("Received:{0} = {1}", item.Key, item.Value);
}
done = true;
}
}
}
示例4: StartListener
private static void StartListener()
{
bool done = false;
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
UdpClient listener = new UdpClient(groupEP);
Log.Notice("Listener", "Waiting for broadcast");
try
{
while (!done)
{
byte[] bytes = listener.Receive(ref groupEP);
Log.Info("Listener", "Client " + groupEP.ToString() + " is trying to connect");
listener.Connect(groupEP);
Log.Succes("Listener", "Listener connected to client " + groupEP.ToString());
done = true;
//TODO - rest of district server connecting
}
}
catch (Exception e)
{
FrameWork.Logger.Log.Error("Listener", e.ToString());
}
finally
{
listener.Close();
}
}
示例5: Connect
public bool Connect()
{
try
{
serverRemote = new IPEndPoint(IPAddress.Parse(ipAddress), portNumber);
udpClient = new UdpClient();
udpClient.Connect(serverRemote);
if(udpClient.Client.Connected)
{
Debug.Log("connected!");
sendUDPPacket("ANYONE OUT THERE?");
//byte[] data = Encoding.UTF8.GetBytes(toSend);
//client.Send(data, data.Length, serverRemote);
//client.SendTimeout(serverRemote, data);
}
}
catch(Exception ex)
{
print ( ex.Message + " : OnConnect");
}
isConnected = true;
if ( udpClient == null ) return false;
return udpClient.Client.Connected;
}
示例6: Backend
public Backend(string ipAddress, int port)
{
// endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
// this.EnableTimedTriggers();
endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
client = new UdpClient();
client.Connect(endpoint);
client.BeginReceive(new AsyncCallback(OnMessageRecieved), null);
}
示例7: Main
public static void Main ()
{
var ip = IPAddress.Parse ("239.255.255.250");
var bytes = new byte [] {60, 70, 75};
UdpClient udp = new UdpClient ();
udp.Connect (ip, 3802);
udp.Send (bytes, 3);
Console.WriteLine ("Sent");
udp.Close ();
}
示例8: Start
// Use this for initialization
void Start()
{
udp = new UdpClient(port);
udp.Client.ReceiveTimeout = 1000;
udp.Connect(host, port);
//thread = new Thread(new ThreadStart(Message));
//thread.Start();
}
示例9: Init
public void Init(string nothing)
{
remoteEndPoint = new IPEndPoint(IPAddress.Parse(remoteAddress), remotePort);
timestamps = new Dictionary<string, Timestamp>();
receivedMsgBuffer = new StringBuilder();
completeMsgBuffer = new List<string>();
try
{
TCPBuffer = new byte[1024];
sendingUDPClient = new UdpClient();
sendingUDPClient.ExclusiveAddressUse = false;
sendingUDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sendingUDPClient.Connect(remoteAddress, remotePort);
receivingUDPClient = new UdpClient();
receivingUDPClient.ExclusiveAddressUse = false;
receivingUDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
receivingUDPClient.Client.Bind((IPEndPoint)sendingUDPClient.Client.LocalEndPoint);
receivingUDPClient.Connect(remoteAddress, remotePort);
receivingUDPClient.BeginReceive(new AsyncCallback(OnMessageUDP), null);
// Send initial message to register the address and port at the server
string hello = "Hello Server";
byte[] bytes = Encoding.UTF8.GetBytes(hello);
try
{
sendingUDPClient.Send(bytes, bytes.Length);
}
catch (Exception err)
{
#if !UNITY_EDITOR
Application.ExternalCall(onErrorCallback, "An error occurred while sending the initial message to the server: " + err.Message);
#else
UnityEngine.Debug.LogError("An error occurred while sending the initial message to the server: " + err.Message);
#endif
}
TCPClient = new TcpClient();
TCPClient.NoDelay = true;
TCPClient.Connect(remoteEndPoint);
TCPClient.GetStream().BeginRead(TCPBuffer, 0, 1024, new AsyncCallback(OnMessageTCP), null);
}
catch (Exception err)
{
#if !UNITY_EDITOR
Application.ExternalCall(onErrorCallback, err.ToString());
#else
UnityEngine.Debug.LogError("Init(): " + err.ToString());
#endif
}
}
示例10: Awake
void Awake()
{
udpClient = new UdpClient(commandsPort);
udpClient.Connect(IPAddress.Loopback, feedbackPort);
// http://stackoverflow.com/a/7478498
uint IOC_IN = 0x80000000;
uint IOC_VENDOR = 0x18000000;
uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
udpClient.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
}
示例11: Awake
void Awake()
{
DontDestroyOnLoad(transform.gameObject);
//this.filePath = "port.txt";
//loadPort();
_broadcaster = new UdpClient();
_broadcaster.EnableBroadcast = true;
_broadcaster.Connect(new IPEndPoint(IPAddress.Broadcast, this.connectionPort));
this.StartServer();
}
示例12: FileSender
public FileSender(string ipAddress, int port, string fileName)
{
_fileName = fileName;
_udpClient = new UdpClient(new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp));
if (!_udpClient.Connect(ipAddress, port))
{
_udpClient.EndConnection();
return;
}
SendingFileToServer();
_udpClient.EndConnection();
}
示例13: Start
void Start()
{
Debug.Log("test");
int port = 4023;
UdpClient udpClient = new UdpClient(port);
try
{
udpClient.Connect("142.232.18.112", port);
// Sends a message to the host to which you have connected.
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
udpClient.Send(sendBytes, sendBytes.Length);
// Sends a message to a different host using optional hostname and port parameters.
UdpClient udpClientB = new UdpClient();
udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", port);
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine("This is the message you received " +
returnData.ToString());
Console.WriteLine("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString());
udpClient.Close();
udpClientB.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
/*
int myReliableChannelId; // above Start()
myReliableChannelId = config.AddChannel(QosType.Reliable);
int maxConnections = 10;
HostTopology topology = new HostTopology(config, maxConnections);
int socketId; // above Start()
int socketPort = 8888; // Also a class member variable
socketId = NetworkTransport.AddHost(topology, socketPort);
Debug.Log("Socket Open. SocketId is: " + socketId);
int myReliableChannelId; // above Start()
myReliableChannelId = config.AddChannel(QosType.Reliable);
int maxConnections = 10;
HostTopology topology = new HostTopology(config, maxConnections);
int socketId;
int socketPort = 4023;
byte error;
socketId = NetworkTransport.AddHost(topology, socketPort);
var connectionId = NetworkTransport.Connect(socketId, "127.0.0.1", socketPort, 0, out error);
byte[] buffer = new byte[1024];
Stream stream = new MemoryStream(buffer);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, "HelloServer");
int bufferSize = 1024;
NetworkTransport.Send(socketId, connectionId, myReliableChannelId, buffer, bufferSize, out error);
*/
}
示例14: SceneEnterSuccess
void SceneEnterSuccess(string jsonData)
{
Debug.Log ("SceneEnterSuccess");
Debug.Log ("SceneEnterSuccess: "+jsonData);
executeInUpdate.Enqueue (SceneEnterSuccessSync);
var N = JSON.Parse (jsonData);
myID = (byte)N ["characterID"].AsInt;
//Debug.Log (N["enemies"]);
enemies.Clear ();
foreach(string key in N["enemies"].AsObject.Keys) {
JSONNode enemy = N["enemies"][key];
Debug.Log ("Found enemy "+key+":"+enemy.ToString());
byte characterID = (byte) int.Parse(key);
executeInUpdate.Enqueue(delegate() {
//if(characterID != myID) enemies.Add(characterID, Instantiate(enemyPrefab, Vector3.zero, Quaternion.identity) as GameObject);
if(characterID != myID) {
//enemies.Add(characterID, Instantiate(enemyPrefab, Vector3.zero, Quaternion.identity) as GameObject);
UMADynamicAvatar avatar = LoadUMA(enemy["umaPackedRecipe"], null, "Enemy", false);
enemies.Add (characterID, MakeEnemy(avatar));
}
});
}
if (udpClient != null) {
udpClient.Close();
}
udpClient = new UdpClient (1100);
udpClient.Connect(alternativMUDClientScript.hostname, N["port"].AsInt);
}
示例15: Start
// Use this for initialization
void Start()
{
remoteEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
client = new UdpClient();
client.Connect(remoteEndPoint);
}