一个完整的简单聊天室的服务端和客户端的搭建
服务端
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Threading;
using System.Text;
namespace ChatroomTestServer
{
class MainClass
{
public static List<Client> clientList = new List<Client> ();
public static void Main (string[] args)
{
Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//IPAddress ip = new IPAddress(new Byte[]{192,168,1,201});
IPAddress ip = new IPAddress(new Byte[]{192,168,0,101});
EndPoint endPoint = new IPEndPoint(ip,7788);
serverSocket.Bind (endPoint);
serverSocket.Listen (100);
Console.WriteLine ("服务端 启动成功");
while (true) {
Socket clientSocket = serverSocket.Accept ();
Console.WriteLine ("一个客户端连接到服务端");
Client client = new Client (clientSocket);
clientList.Add (client);
}
}
public static void BroadcastMessage(string message) {
var notConnectedClients = new List<Client>();
foreach (var client in clientList) {
if (client.isConnected ()) {
client.SendMessage (message);
} else {
notConnectedClients.Add (client);
}
}
foreach (var notConnectClient in notConnectedClients) {
clientList.Remove (notConnectClient);
}
}
}
public class Client {
private Socket clientSocket;
private Thread t;
private byte[] data = new byte[1024];
public Client(Socket socket) {
clientSocket = socket;
//启动一个线程来处理客户端的数据接收
t = new Thread(ReceiveMessage);
t.Start ();
}
private void ReceiveMessage() {
while (true) {
//在接收数据之前,先验证一下socket连接是否断开
if (clientSocket.Poll (10, SelectMode.SelectRead)) {
clientSocket.Close ();
break;
} else {
int length = clientSocket.Receive (data);
string message = Encoding.UTF8.GetString (data, 0, length);
Console.WriteLine ("收到了客户端的消息:" + message);
//广播收到的消息
MainClass.BroadcastMessage(message);
}
}
}
public void SendMessage(string message) {
byte[] data = Encoding.UTF8.GetBytes (message);
clientSocket.Send (data);
}
public bool isConnected() {
return clientSocket.Connected;
}
}
}
客户端
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Net;
using UnityEngine;
public class ChatManager : MonoBehaviour {
public string ip = "192.168.0.101";
private Socket clientSocket;
public UIInput textInput;
public UILabel chatLabel;
private Thread t;
private byte[] data = new byte[1024];
private string receivedMessage = "";
// Use this for initialization
void Start () {
ConnectToServer ();
}
// Update is called once per frame
void Update () {
if (receivedMessage != "") {
chatLabel.text += receivedMessage;
receivedMessage = "";
}
}
void ConnectToServer() {
clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//跟服务器建立连接
clientSocket.Connect(new IPEndPoint(IPAddress.Parse(ip),7788));
//创建一个线程接受广播消息
t = new Thread(ReceiveMessage);
t.Start ();
}
void ReceiveMessage() {
while (true) {
if (!clientSocket.Connected) {
break;
}
int length = clientSocket.Receive (data);
string message = Encoding.UTF8.GetString (data, 0, length);
receivedMessage += "\n" + message;
}
}
void SendChatMessage(string message) {
byte[] data = Encoding.UTF8.GetBytes (message);
clientSocket.Send (data);
}
public void SendBtnOnClick() {
string message = textInput.value;
SendChatMessage (message);
}
void OnDestroy() {
clientSocket.Shutdown (SocketShutdown.Both);
clientSocket.Close ();
}
}