using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using ProtoBuf;
using System.Collections.Generic;
public class TCPServerTest : MonoBehaviour {
Socket serverSocket;
Socket clientSocket;
Thread clientThread;
Thread t;
// Use this for initialization
void Start () {
t = new Thread(new ThreadStart(OpenServer));
t.Start();
}
// Update is called once per frame
void Update () {
}
void OnDestroy(){
t.Abort();
<span style="white-space:pre"> </span>serverSocket.Close();
}
void OpenServer(){
IPAddress ipAdr = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEp = new IPEndPoint(ipAdr , 2234);
serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
serverSocket.Bind(ipEp);
serverSocket.Listen(10);
while(true){
try {
clientSocket = serverSocket.Accept();
Debug.Log("Accept");
clientThread = new Thread(new ThreadStart(ReceiveData));
clientThread.Start ();
} catch (System.Exception ex) {
print(ex);
}
}
}
void ReceiveData(){
bool keepalive = true;
Socket s = clientSocket;
byte[] buffer = new byte[1024];
//根据收听到的客户端套接字向客户端发送信息
IPEndPoint clientep = (IPEndPoint)s.RemoteEndPoint;
//lstServer.Items.Add("Client:" + clientep.Address + "("+clientep.Port+")");
string welcome = "IP为"+clientep.Address +"的客户端连接成功";
byte[] data = new byte[1024];
data = Encoding.UTF8.GetBytes(welcome);
s.Send(data, data.Length, SocketFlags.None);
while (keepalive)
{
//在套接字上接收客户端发送的信息
int bufLen = 0;
try
{
bufLen = s.Available;
s.Receive(buffer, 0, bufLen, SocketFlags.None);
if (bufLen == 0)
continue;
}
catch (Exception ex)
{
Debug.Log("Receive Error:" + ex.Message);
return;
}
clientep = (IPEndPoint)s.RemoteEndPoint;
string clientcommand = System.Text.Encoding.UTF8.GetString(buffer).Substring(0, bufLen);
Debug.Log("服务端收到:" +clientcommand);
//lstServer.Items.Add(clientcommand + "("+clientep.Address + ":"+clientep.Port+")");
}
}
}
using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using ProtoBuf;
using System.Collections.Generic;
public class TCPClientTest : MonoBehaviour {
Socket clientSocket;
byte[] sdata = new byte[1024];
Thread t;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void ConnectServer(){
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2234);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
clientSocket.Connect(ipep);
}
catch (SocketException ex)
{
Debug.Log("connect error: " + ex.Message);
return;
}
while (true)
{
//接收服务器信息
int bufLen = 0;
try
{
bufLen = clientSocket.Available;
clientSocket.Receive(sdata, 0, bufLen, SocketFlags.None);
if (bufLen == 0)
{
continue;
}
}
catch (Exception ex)
{
Debug.Log("Receive Error:" + ex.Message);
return;
}
string clientcommand = System.Text.Encoding.UTF8.GetString(sdata).Substring(0, bufLen);
Debug.Log("客户端收到:" + clientcommand);
//lstClient.Items.Add(clientcommand);
}
}
void sendMsg(){
byte[] data = new byte[1024];
data = Encoding.UTF8.GetBytes(sendStr);
clientSocket.Send(data, data.Length, SocketFlags.None);
}
string sendStr = "Send SomeThing";
void OnGUI(){
if(GUILayout.Button("Connect"))
{
t = new Thread(new ThreadStart(ConnectServer));
t.Start();
}
sendStr = GUILayout.TextArea(sendStr);
if(GUILayout.Button("send"))
{
sendMsg();
}
}
void OnDestroy(){
t.Abort();
<span style="white-space:pre"> </span>if(clientSocket!=null){
<span style="white-space:pre"> </span>clientSocket.Close();
<span style="white-space:pre"> </span>}
}
}