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;
}
}
}