1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| using System; using System.Collections; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.IO;
using System.Threading; using UnityEngine; using UnityEngine.UI;
public class client : MonoBehaviour { private string staInfo = "NULL"; private string inputIp = "10.175.229.183"; private string inputPort = "6000"; public string inputMes = "NULL"; private int recTimes = 0; private string recMes = "NULL"; private Socket socketSend; private bool clickSend = false;
void Start() {
}
void Update() {
}
private void ClickConnect() { try { int _port = Convert.ToInt32(inputPort); string _ip = inputIp;
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(_ip); IPEndPoint point = new IPEndPoint(ip, _port);
socketSend.Connect(point); Debug.Log("连接成功 , " + " ip = " + ip + " port = " + _port); staInfo = ip + ":" + _port + " 连接成功";
Thread r_thread = new Thread(Received); r_thread.IsBackground = true; r_thread.Start();
Thread s_thread = new Thread(SendMessage); s_thread.IsBackground = true; s_thread.Start(); } catch (Exception) { Debug.Log("IP或者端口号错误......"); staInfo = "IP或者端口号错误......"; } }
void Received() { while (true) { try { byte[] buffer = new byte[1024 * 6]; int len = socketSend.Receive(buffer); if (len == 0) { break; }
recMes = Encoding.UTF8.GetString(buffer, 0, len);
Debug.Log("客户端接收到的数据 : " + recMes);
recTimes ++; staInfo = "接收到一次数据,接收次数为 :" + recTimes; Debug.Log("接收次数为:" + recTimes); } catch { } } }
void SendMessage() { try { while (true) { if (clickSend) { clickSend = false; string msg = inputMes; byte[] buffer = new byte[1024 * 6]; buffer = Encoding.UTF8.GetBytes(msg); socketSend.Send(buffer); Debug.Log("发送的数据为:" + msg); } } } catch { } }
private void OnDisable() { Debug.Log("begin OnDisable()");
if (socketSend.Connected) { try { socketSend.Shutdown(SocketShutdown.Both); socketSend.Close(); } catch (Exception e) { print(e.Message); } }
Debug.Log("end OnDisable()"); }
void OnGUI() { GUI.color = Color.black;
GUI.Label(new Rect(65, 10, 60, 20), "状态信息");
GUI.Label(new Rect(135, 10, 80, 60), staInfo);
GUI.Label(new Rect(65, 70, 50, 20), "服务器ip地址");
inputIp = GUI.TextField(new Rect(125, 70, 100, 20), inputIp, 20);
GUI.Label(new Rect(65, 110, 50, 20), "服务器端口");
inputPort = GUI.TextField(new Rect(125, 110, 100, 20), inputPort, 20);
GUI.Label(new Rect(65, 150, 80, 20), "接收到消息:");
GUI.Label(new Rect(155, 150, 80, 20), recMes);
GUI.Label(new Rect(65, 190, 80, 20), "发送的消息:");
inputMes = GUI.TextField(new Rect(155, 190, 100, 20), inputMes, 20);
if (GUI.Button(new Rect(65, 230, 60, 20), "开始连接")) { ClickConnect(); }
if (GUI.Button(new Rect(65, 270, 60, 20), "发送信息")) { clickSend = true; } } }
|