本文介绍了从服务器向所有客户端发送数据[聊天]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我做了一个聊天系统应用程序
客户端可以向服务器发送数据,服务器可以从所有客户端接收数据.
但我有一个问题:
我希望服务器可以将数据发送到所有客户端,
例如:
如果客户端A向服务器发送数据,则服务器接收数据并发送给其他客户端(B C D ...)
这是完整的代码(服务器端):
hello
i made a Chat System App
Clients Can Send data to Server, and Server can Receive Data From all Client.
but i have a problem in it:
i want that Server can Send Data to all Client,
for Example:
if Client A send Data to Server, server receive data and send to other Client ( B C D ... )
this is a full code ( side Server ):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
namespace chat_Server
{
public partial class Form1 : Form
{
Socket ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ServerIP = new IPEndPoint(IPAddress.Any, 6634);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
ServerSocket.Bind(ServerIP);
ServerSocket.Listen(10);
Thread StartT = new Thread(Start);
StartT.Start();
MessageBox.Show("Server Started...");
}
void Start()
{
while (true)
{
ConnectionThread newconnection = new ConnectionThread();
try
{
newconnection.client = ServerSocket.Accept();
}
catch
{
break;
}
Thread newThread = new Thread(new ThreadStart(newconnection.HandleConnection));
newThread.Start();
}
}
class ConnectionThread
{
private static Dictionary<int, Socket> allSocket = new Dictionary<int, Socket>();
string Spliter = "~$%^&";
string ReverseSpliter = "&^%$~";
public Socket client;
int BufferSize = 100000;
private static int rand = 0;
int id = 0;
public void HandleConnection()
{
int recv;
byte[] data = new byte[BufferSize];
string textRecieve;
string StringData;
rand++;
id = rand;
allSocket.Add(rand, client);
recv = client.Receive(data);
textRecieve = Encoding.Unicode.GetString(data, 0, recv);
string[] txtR = GetDataPart(textRecieve);
if (txtR[0] == "hi")
{
MessageBox.Show("Show Hi");
}
else if (txtR[0] == "bye")
{
MessageBox.Show("Show Bye");
}
try
{
while (true)
{
data = new byte[BufferSize];
recv = client.Receive(data);
if (recv == 0)
{
allSocket.Remove(id);
client.Close();
break;
}
StringData = Encoding.Unicode.GetString(data, 0, recv);
txtR = GetDataPart(StringData);
if (txtR[0] == "Send")
{
MessageBox.Show("Client type --> Send");
}
else
SendText(StringData);
}
}
catch
{
allSocket.Remove(id);
}
}
void SendText(string Data)
{
string[] txtR = GetDataPart(Data);
for (int i = 0; i < id; i++)
{
allSocket[id].Send(Encoding.Unicode.GetBytes(Data));
}
}
string[] GetDataPart(string StringData)
{
string[] txtR = new string[10];
int Index = 0;
int LastIndex = 0;
int j = 0;
for (int i = 0; i < 10; i++)
txtR[i] = "";
while (Index != -1)
{
Index = StringData.IndexOf(Spliter, LastIndex);
if (Index != -1)
{
if ((Index + 5) == StringData.Length || ((Index + 10) < StringData.Length || StringData.Substring(Index + 5, 5) != ReverseSpliter))
{
txtR[j] += StringData.Substring(LastIndex, Index - LastIndex);
j++;
LastIndex = Index + 5;
}
else if ((Index + 10) < StringData.Length && StringData.Substring(Index + 5, 5) == ReverseSpliter)
{
txtR[j] += StringData.Substring(LastIndex, Index - LastIndex + 5);
LastIndex = Index + 10;
}
}
}
return txtR;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ServerSocket.Close();
Application.Exit();
Application.ExitThread();
}
private void btnStop_Click(object sender, EventArgs e)
{
ServerSocket.Close();
}
}
}
我如何向所有客户端发送数据? (例如聊天室)
how can i send data to all client ? (Like Chat Room)
推荐答案
我如何向所有客户端发送数据? (如聊天室)
how can i send data to all client ? (Like Chat Room)
这篇关于从服务器向所有客户端发送数据[聊天]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!