Server.cs:
using UnityEngine;
using System.Collections;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Text;
public class Server : MonoBehaviour {
void Start () {
OpenServer();
}
void OpenServer()
{
IPAddress ipAdr = IPAddress.Parse("10.56.03.32");
IPEndPoint ipEp = new IPEndPoint(ipAdr , 1234);
Socket serverScoket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
serverScoket.Bind (ipEp);
serverScoket.Listen(20);
while(true)
{ //Unity3D教程手册:www.unitymanual.com
Socket client = serverScoket.Accept();
byte[] request = new byte[512];
int bytesRead = client.Receive(request);
string input = Encoding.UTF8.GetString(request,0,bytesRead);
print("server request:"+input);
string output = "连接服务器成功~~~~";
byte[] concent = Encoding.UTF8.GetBytes(output);
client.Send(concent);
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}
}
using UnityEngine;
using System.Collections;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
public class Client : MonoBehaviour {
void Start () {
ConncetServer();
}
void ConncetServer()
{
IPAddress ipAdr = IPAddress.Parse("10.56.03.32");
IPEndPoint ipEp = new IPEndPoint(ipAdr , 1234);
Socket clientScoket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
clientScoket.Connect(ipEp);
string output = "客户端请求连接~~~";
byte[] concent = Encoding.UTF8.GetBytes(output);
clientScoket.Send(concent); //Unity3D教程手册:www.unitymanual.com
byte[] response = new byte[1024];
int bytesRead = clientScoket.Receive(response);
string input = Encoding.UTF8.GetString(response,0,bytesRead);
print("Client request:"+input);
clientScoket.Shutdown(SocketShutdown.Both);
clientScoket.Close();
}
}
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System;
using System.Text;
using System.Threading;
public class TestAsyncSocketClient2 : MonoBehaviour
{
private Socket client = null;
private string ip = "127.0.0.1";
private int port = 8989;
private int size = 1024;
private byte[] readData = new byte[1024];
private byte[] data = new byte[1024];
void Start()
{ //Unity3D教程手册:www.unitymanual.com
// socket2.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 多socket 复用同一端口
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint address = new IPEndPoint(IPAddress.Parse(ip), port);
//client.Blocking = false;
client.BeginConnect(address, new AsyncCallback(Connected), null); //建立异步连接服务 , Connected 进行监听
//connectDone.WaitOne();
}
void Connected(IAsyncResult iar) //建立连接
{
//Socket client = (Socket)iar.AsyncState;
client.EndConnect(iar);
//client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
echo("建立连接");
}
void Send(string str)
{
byte[] msg = Encoding.UTF8.GetBytes(str);
client.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(SendData), client); //开始发送
}
void SendData(IAsyncResult iar) //发送数据
{ //Unity3D教程手册:www.unitymanual.com
Socket remote = (Socket)iar.AsyncState;
int sent = remote.EndSend(iar); //关闭发送
remote.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(ReceiveData), remote); //开始接收
}
void Update()
{
startReceive(); //这步很重要,,,不然会收不到服务器发过来的消息
}
bool ReceiveFlag = true;
void startReceive()
{
if (ReceiveFlag) {
ReceiveFlag = false;
client.BeginReceive(readData, 0, readData.Length, SocketFlags.None, new AsyncCallback(endReceive), client);
}
}
void endReceive(IAsyncResult iar) //接收数据
{
ReceiveFlag = true;
Socket remote = (Socket)iar.AsyncState;
int recv = remote.EndReceive(iar);
if (recv > 0)
{
string stringData = Encoding.UTF8.GetString(readData, 0, recv);
text2 += "\n" + "接收服务器数据:+++++++++++++++" + stringData;
}
}
void ReceiveData(IAsyncResult iar) //接收数据
{
Socket remote = (Socket)iar.AsyncState;
int recv = remote.EndReceive(iar); //关闭接收 注意:如果关闭了接收,就不能接收了 测试是这样
string stringData = Encoding.UTF8.GetString(data, 0, recv);
text2 += "\n" + "回收发送数据:+++++++++++++++" + stringData;
}
void CloseSocket() //关闭socket
{
if (client.Connected)
{
echo("关闭socket");
client.Close();
}
}
void OnApplicationQuit()
{
CloseSocket();
}
void echo(object msg)
{
Debug.Log(msg);
}
string text = "";
string text2 = "";
Vector2 p = new Vector2(600, 300);
void OnGUI()
{
GUILayout.BeginVertical(GUILayout.Width(500) );
text = GUILayout.TextField(text);
if (GUILayout.Button("发送数据"))
{
Send(text);
}
GUILayout.BeginScrollView(p);
text2 = GUILayout.TextArea(text2, GUILayout.Height(300));
GUILayout.EndScrollView();
GUILayout.EndVertical();
}
}