gpt4 book ai didi

c# - 具有多个客户端的异步套接字服务器

转载 作者:行者123 更新时间:2023-11-30 23:08:41 24 4
gpt4 key购买 nike

我有一个问题无法解决,因为我的 C# 知识不是很好。在网上找了一些代码,根据自己的需要修改。

我的问题是,当我向客户端发送消息时,只有一个客户端会收到消息,然后下一条消息会被另一个客户端接收,依此类推。我想在不丢失任何数据的情况下向所有连接的客户端发送相同的消息。

服务器

using System;
using System.Net.Sockets;
using System.Threading;


public class AsynchIOServer
{
static TcpListener tcpListener = new TcpListener(10);

static void Listeners()
{

Socket socketForClient = tcpListener.AcceptSocket();
if (socketForClient.Connected)
{
Console.WriteLine("Client:"+socketForClient.RemoteEndPoint+" now connected to server.");
NetworkStream networkStream = new NetworkStream(socketForClient);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);

////here we send message to client
while (true){
Console.WriteLine("type your message to be recieved by client:");
string theString = Console.ReadLine();
streamWriter.WriteLine(theString);
////Console.WriteLine(theString);
streamWriter.Flush();
}
streamReader.Close();
networkStream.Close();
streamWriter.Close();

}
socketForClient.Close();
Console.WriteLine("Press any key to exit from server program");
Console.ReadKey();
}

public static void Main()
{
//TcpListener tcpListener = new TcpListener(10);
tcpListener.Start();
Console.WriteLine("************This is Server program************");
Console.WriteLine("Hoe many clients are going to connect to this server?:");
int numberOfClientsYouNeedToConnect =int.Parse( Console.ReadLine());
for (int i = 0; i < numberOfClientsYouNeedToConnect; i++)
{
Thread newThread = new Thread(new ThreadStart(Listeners));
newThread.Start();
}
}
}

客户:

using System;
using System.Net.Sockets;
using System.Threading;
public class Client
{
static public void Main(string[] Args)
{
TcpClient socketForServer;
try
{
socketForServer = new TcpClient("localHost", 10);
}
catch
{
Console.WriteLine(
"Failed to connect to server at {0}:999", "localhost");
return;
}

NetworkStream networkStream = socketForServer.GetStream();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(networkStream);
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter(networkStream);
Console.WriteLine("*******This is client program who is connected to localhost on port No:10*****");

try
{
string outputString;
// read the data from the host and display it
{
while (true)
{
outputString = streamReader.ReadLine();
Console.WriteLine("Message Recieved by server:" + outputString);

streamWriter.Flush();
}
}
}
catch
{
Console.WriteLine("Exception reading from Server");
}
// tidy up
networkStream.Close();
Console.WriteLine("Press any key to exit from client program");
Console.ReadKey();
}

private static string GetData()
{
//Ack from sql server
return "ack";
}
}

谢谢

最佳答案

简单的工作多线程服务器:

    static void Process(Socket client) {

Console.WriteLine("Incoming connection from " + client.RemoteEndPoint);

const int maxMessageSize = 1024;
byte[] response;
int received;

while (true) {

// Send message to the client:
Console.Write("Server: ");
client.Send(Encoding.ASCII.GetBytes(Console.ReadLine()));
Console.WriteLine();

// Receive message from the server:
response = new byte[maxMessageSize];
received = client.Receive(response);
if (received == 0) {
Console.WriteLine("Client closed connection!");
return;
}

List<byte> respBytesList = new List<byte>(response);
respBytesList.RemoveRange(received, maxMessageSize - received); // truncate zero end
Console.WriteLine("Client (" + client.RemoteEndPoint + "+: " + Encoding.ASCII.GetString(respBytesList.ToArray()));
}
}

static void Main(string[] args) {

int backlog = -1, port = 2222;

Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.ReceiveTimeout = -1;

// Start listening.
try {
server.Bind(new IPEndPoint(IPAddress.Any, port));
server.Listen(backlog);
}
catch (Exception) {
Console.WriteLine("Listening failed!");
Console.ReadKey();
return;
}

Console.WriteLine("Start listening...");

while(true) {
Socket client = server.Accept();
new System.Threading.Thread(() => {
try { Process(client); } catch (Exception ex) { Console.WriteLine("Client connection processing error: " + ex.Message); }
}).Start();
}

//Console.WriteLine("Press any key for exit...");
//Console.ReadKey();
}

和客户:

    static void WorkWithServer(Socket server) {

const int maxMessageSize = 1024;
byte[] response;
int received;

while(true) {

try {

// Receive message from the server:
response = new byte[maxMessageSize];
received = server.Receive(response);
if (received == 0) {
Console.WriteLine("Server closed connection.");
return;
}

List<byte> respBytesList = new List<byte>(response);
respBytesList.RemoveRange(received, maxMessageSize - received); // truncate zero end
Console.WriteLine("Server: " + Encoding.ASCII.GetString(respBytesList.ToArray()));

// Send message to the server:
Console.Write("You: ");
server.Send(Encoding.ASCII.GetBytes(Console.ReadLine()));
Console.WriteLine();
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
return;
}
}

}

static void Main(string[] args) {

IPEndPoint serverEp = new IPEndPoint(IPAddress.Parse("192.168.1.2"), 2222);

Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.ReceiveTimeout = -1;

// Connect to the server.
try { server.Connect(serverEp); }
catch (Exception) {
Console.WriteLine("Establish connection with server (" + serverEp + ") failed!");
Console.ReadKey();
return;
}

Console.WriteLine("Connection with server (" + serverEp + ") established!");
WorkWithServer(server);

Console.WriteLine("Press any key for exit...");
Console.ReadKey();
}

根据需要进行编辑。

关于c# - 具有多个客户端的异步套接字服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46346706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com