网络编程 笔记(九)Echo客户端 Utniy实现

来源:互联网 发布:java 图片添加文字 编辑:程序博客网 时间:2024/05/16 23:49
//********************************************************************// 文件名: UClient.cs// 描述: 客户端练习// 作者: 李伟// 创建时间: 2015-10-23////********************************************************************using UnityEngine;using System.Collections;using System.Collections.Generic;using System.Net;using System.Net.Sockets;using System.Text;public class UClient : MonoBehaviour {    byte[] RecvBuf = new byte[1024];    byte[] SendBuf;    int RecvLen = 0;    string recvmsg = string.Empty;    TcpClient remoteServer;    List<string> recvMsgList = new List<string>();    int sendLen = 0;    string TextFieldString = string.Empty;    void Awake()    {        IPEndPoint remoteEP = new IPEndPoint (IPAddress.Parse ("10.1.1.25"), 9195);        remoteServer = new TcpClient ();        remoteServer.Connect (remoteEP);    }    void Send ()     {        SendBuf = Encoding.UTF8.GetBytes(TextFieldString);        sendLen = remoteServer.Client.Send (SendBuf);        RecvLen = 0;        while (RecvLen < sendLen)         {            RecvLen = remoteServer.Client.Receive (RecvBuf);        }        recvMsgList.Add (Encoding.UTF8.GetString (RecvBuf, 0, RecvLen));    }    void OnGUI ()     {        TextFieldString = GUI.TextField (new Rect (10, 10, 200, 100), TextFieldString);        if (GUI.Button (new Rect (210, 10, 100, 100), "Btn"))        {            Send () ;        }        if (GUI.Button (new Rect (310, 10, 100, 100), "Close"))        {            remoteServer.Close();        }        int hight = 110;        foreach (var str in recvMsgList)         {            GUI.Label(new Rect(10, hight, 100, 20), str);            hight+=20;        }    }}
0 0
原创粉丝点击