没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
Unity3D 教程:学习 Socket(二)
Posted on 2013 年 07 月 25 日 by U3d / Unity3D 脚本/插件 /被围观 11 次
Unity3D 教程:学习 Socket(一)
001
using UnityEngine;
002
using System.Collections;
003
using System.Net.Sockets;
004
using System.Net;
005
using System;
006
using System.Text;
007
using System.Threading;
008
009
public class TestAsyncSocketClient2 : MonoBehaviour
010
{
011
private Socket client = null;
012
private string ip = "127.0.0.1";
013
private int port = 8989;
014
015
private int size = 1024;
016
private byte[] readData = new byte[1024];
017
private byte[] data = new byte[1024];
018
void Start()
019
{ //Unity3D
教程手册:
www.unitymanual.com
020
021
// socket2.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true);
多
socket
复用同一端口
022
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
023
IPEndPoint address = new IPEndPoint(IPAddress.Parse(ip),
port);
024
//client.Blocking = false;
025
client.BeginConnect(address, new AsyncCallback(Connected),
null); //
建立异步连接服务
, Connected
进行监听
026
//connectDone.WaitOne();
027
}
028
void Connected(IAsyncResult iar) //
建立连接
029
{
030
//Socket client = (Socket)iar.AsyncState;
031
client.EndConnect(iar);
032
//client.BeginReceive(data, 0, size, SocketFlags.None, new
AsyncCallback(ReceiveData), client);
033
echo("建立连接");
034
035
}
036
void Send(string str)
037
{
038
byte[] msg = Encoding.UTF8.GetBytes(str);
039
client.BeginSend(msg, 0, msg.Length, SocketFlags.None, new
AsyncCallback(SendData), client); //
开始发送
040
}
041
void SendData(IAsyncResult iar) //
发送数据
奔跑的楠子
- 粉丝: 22
- 资源: 299
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0