TCP服务器中的多个客户端(C#) [英] Mulitple clients in a TCP server (C#)

查看:75
本文介绍了TCP服务器中的多个客户端(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个TCP客户端和一个TCP服务器,但我希望它是Multihtreaded。那么有人想帮助我吗?



我的尝试:



以下是客户:

Hi,
i have a TCP Client and a TCP Server, but I want it Multihtreaded. So does anyone want to help me?

What I have tried:

Here's the Client:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Collections;

namespace Spielerinfo_Client
{
    public class Program
    {
        static public void Main(string[] Args)
        {
            try
            {
                while (true)
                {
                    TcpClient tcpclnt = new TcpClient();
                    Console.WriteLine("Connecting.....");

                    tcpclnt.Connect("127.0.0.1", 8001);
                    // use the ipaddress as in the server program

                    Console.WriteLine("Connected");
                    Console.Write("Enter the string to be transmitted : ");

                    String str = Console.ReadLine();
                    Stream stm = tcpclnt.GetStream();

                    ASCIIEncoding asen = new ASCIIEncoding();
                    byte[] ba = asen.GetBytes(str);
                    Console.WriteLine("Transmitting.....");

                    stm.Write(ba, 0, ba.Length);

                    byte[] bb = new byte[100];
                    int k = stm.Read(bb, 0, 100);

                    for (int i = 0; i < k; i++)
                        Console.Write(Convert.ToChar(bb[i]));

                    tcpclnt.Close();
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }
    }
}





和服务器:



And the Server:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Collections;

namespace Spielerinfo_Server
{
    public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                while (true)
                {
                    TcpListener myList = new TcpListener(IPAddress.Any, 8001);
                    myList.Start();
                    Console.WriteLine("The server is running at port 8001...");
                    Console.WriteLine("The local End point is  :" +
                    myList.LocalEndpoint);
                    Console.WriteLine("Waiting for a connection.....");
                    Socket s = myList.AcceptSocket();
                    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
                    byte[] b = new byte[100];
                    int k = s.Receive(b);
                    Console.WriteLine("Recieved...");
                    string Command = string.Empty;
                    for (int i = 0; i < k; i++)
                    {
                        Command = Command + Convert.ToChar(b[i]);
                    }
                    Console.WriteLine(Command);
                    ASCIIEncoding asen = new ASCIIEncoding();
                    s.Send(asen.GetBytes("The string was recieved by the server."));
                    Console.WriteLine("\nSent Acknowledgement");
                    s.Close();
                    myList.Stop();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }
    }

}

推荐答案

有吨网上的例子。您只需搜索 [ ^ ]。
There's tons of examples on the web. You need only to search[^].


这篇关于TCP服务器中的多个客户端(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆