阅读背景:

(第三季)605-unity聊天室-客户端发送消息到服务器端

来源:互联网 



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



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: