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

//********************************************************************
// 文件名: 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值