服务端using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Socket聊天室unity
{
class Program
{
//创建一个client索引,用来存储client对象
static List<Client>clientlist = new List<Client> ();
/// <summary>
/// 广播消息的方法
/// </summary>
/// <param name="message"></param>
public static void BroadcastMessage(string message)
{
//已经断开连接的集合
var NotConectesList = new List<Client>();
foreach(var client in clientlist)
{
if(client .Connected )
client.SendMessage(message );
else
{
NotConectesList.Add(client);
}
}
foreach(var temp in NotConectesList)
{
//删除断开连接的集合
clientlist.Remove(temp);
}
}
static void Main(string[] args)
{
//创建一个Socket
Socket TcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 100.73.76.253绑定ip地址
TcpServer.Bind(new IPEndPoint(IPAddress.Parse("100.73.76.253"), 6677));
//添加监听
TcpServer.Listen(100); //参数是最大监听数
Console.WriteLine("服务器已启动");
//while建立死循环用来不断的添加客户端
while (true)
{
//添加客户端
Socket clientSocket = TcpServer.Accept();
Console.WriteLine("有一个客户端连接进来");
//把与每个客户端通信的逻辑放到cilent类中进行处理
Client client = new Socket聊天室unity.Client(clientSocket ) ;
//把客户端加入索引
clientlist.Add(client);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Socket聊天室unity
{
/// <summary>
/// 新建类用来跟客户端做通信
/// </summary>
class Client
{
//创建一个socket对象
private Socket clientSocket;
//创建一个线程,便于管理
private Thread t;
//创建一个byte数组当作数据容器
private byte[] data = new byte[1024];
public Client(Socket s)
{
clientSocket = s;
//启动一个线程,处理数据的接收
t = new Thread(ReceiveMessage);
t.Start();
}
/// <summary>
/// 接收客户端数据的方法
/// </summary>
private void ReceiveMessage()
{
//一直接收客户端数据
while (true)
{
//在接收数据之前判断Socket连接是否断开
if(clientSocket .Poll(10, SelectMode.SelectRead)){
clientSocket.Close();
break;
}
int length = clientSocket.Receive(data);
//把接收到的数组转化为string
string message = Encoding.UTF8.GetString(data, 0, length);
//收到数据的时候把数据分发到客户端,广播消息
Program.BroadcastMessage(message);
Console.WriteLine("收到消息:" + message);
}
}
public void SendMessage(string message)
{
byte[] data = Encoding.UTF8.GetBytes(message);
clientSocket.Send(data);
}
public bool Connected
{
get { return clientSocket.Connected; }
}
}
}
unity客户端
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
public class SocketClient : MonoBehaviour {
//ip地址
public string ipaddress = "100.73.76.253";
//端口号
public int port = 6677;
//新建一个Socket
private Socket clientSocket;
//获取inputText组件
public InputField inputText;
//创建一个新的线程来接收消息
private Thread t;
//声明一个byte数组当作数据容器
private byte[] data = new byte[1024];
//获取聊天框
public Text messageText;
//消息容器
private string message;
void Start () {
ConnectServer();
}
void Update () {
if(message!=null &&message != "")
{
messageText.text += "/n" + message;
message = "";
}
}
/// <summary>
///连接服务器的方法
/// </summary>
void ConnectServer()
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//连接服务器
clientSocket.Connect(new IPEndPoint(IPAddress.Parse(ipaddress), port));
//创建一个新的线程 用来接收消息
t = new Thread(ReceiveMessage);
t.Start();
}
/// <summary>
/// 用来循环接收消息
/// </summary>
void ReceiveMessage()
{
while(true)
{
if(clientSocket.Connected==false)
{
break;
}
int length = clientSocket.Receive(data);
message = Encoding.UTF8.GetString(data, 0, length);
//
}
}
/// <summary>
/// 向服务器发送消息的方法
/// </summary>
/// <param name="message"></param>
void SendMessage(string message)
{
//把字符串转成byte数组
byte[] data = Encoding.UTF8.GetBytes(message);
clientSocket.Send(data);
}
/// <summary>
/// 提交按钮的点击方法
/// </summary>
public void SendButtonClick()
{
string inputMessage = inputText.text;
SendMessage(inputMessage);
inputText.text = "";
}
/// <summary>
/// 客户端关闭执行的方法
/// </summary>
void OnDestroy()
{
clientSocket.Shutdown(SocketShutdown.Both);
}
}
版权声明:本文为qq_35711014原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。