using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.Text;
public class ChatManager : MonoBehaviour {
public string ipaddress = "192.168.51.102";
public int port = 7788;
public UIInput textInput;
private Socket clientSocket;
// Use this for initialization
void Start () {
ConnectToServer();
}
// Update is called once per frame
void Update () {
}
void ConnectToServer()
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 跟服务器端建立连接
clientSocket.Connect(new IPEndPoint(IPAddress.Parse(ipaddress), port));
}
void SendMessage(string message)
{
byte[] data = Encoding.UTF8.GetBytes(message);
clientSocket.Send(data);
}
public void OnSendButtonClick()
{
string value = textInput.value;
SendMessage(value);
textInput.value = "";
}
void OnDestroy()
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close(); // 关闭 连接
}
}
using UnityEngine;
using System.Collec