永发信息网

C# 多个客户端与服务端连接 并且每个客户端可以发送多条消息

答案:4  悬赏:0  手机版
解决时间 2021-02-14 21:05
  • 提问者网友:回忆在搜索
  • 2021-02-14 16:38
在线等~~~
最佳答案
  • 五星知识达人网友:思契十里
  • 2021-02-14 17:37
服务端:
class mTcpServer
{
class server
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Thread newThread;
try
{
serverTcpListener = new TcpListener(localAddr, port);
serverTcpListener.Start();
while (true)
{
Console.Write("Waiting for a connection... ");
client = serverTcpListener.AcceptTcpClient();
Console.WriteLine("Connected!");
newThread = new Thread(new ThreadStart(newScream));
newThread.Start();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
Console.WriteLine("Stop listening for new clients");
serverTcpListener.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();

}

static Int32 port = 9050;
static IPAddress localAddr = IPAddress.Parse("127.0.0.1");
static TcpListener serverTcpListener = null;
static Byte[] bytes = new Byte[256];
static String data = null;
static TcpClient client;
static void newScream()
{
data = null;
NetworkStream stream = client.GetStream();
stream.Write(Encoding.ASCII.GetBytes("Connected!"), 0, 10);
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
}
}
}
客户端:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace mTcpClint
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class client
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
byte[] data = new byte[1024];
//Socket newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.Write("please input the server ip:");
string ipadd = Console.ReadLine();
Console.WriteLine();
Console.Write("please input the server port:");
int port = Convert.ToInt32(Console.ReadLine());
IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port);//服务器的IP和端口
TcpClient newclient;
newclient = new TcpClient();
newclient.Connect(ipadd, port);
int recv = newclient.Client.Receive(data);
string stringdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringdata);
while (true)
{
string input = Console.ReadLine();
if (input == "exit") break;
newclient.Client.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
recv = newclient.Client.Receive(data);
stringdata = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringdata);
}
Console.WriteLine("disconnect from server");
newclient.Client.Shutdown(SocketShutdown.Both);
newclient.Close();
}
}
}
全部回答
  • 1楼网友:零点过十分
  • 2021-02-14 19:25
//客户端连接按钮 private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(TcpClientFunction); thread.Start(); } //创建服务器的端点 IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6003); //创建客户端的Socket对象 Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); bool blFlag = true; //自己定一个方法 private void TcpClientFunction() { //连接服务器 lsbMsg.Items.Add(this,"开始连接服务器...."); try { client.Connect(point); lsbMsg.Items.Add(this,"连接成功...."); while (blFlag) { //接收服务器的信息 byte[] data = new byte[1024]; int recv = client.Receive(data); //转换编码 string msg = Encoding.UTF8.GetString(data, 0, recv); lsbMsg.Items.Add(this, msg); } client.Shutdown(SocketShutdown.Both); client.Close(); } catch (SocketException ex) { lsbMsg.Items.Add(this,string.Format("{0}:{1}", ex.ErrorCode, ex.Message)); } } //发送信息按钮 private void btnSend_Click(object sender, EventArgs e) { if (blFlag) { string data = txtMsg.Text; client.Send(Encoding.UTF8.GetBytes(data)); } else lsbMsg.Items.Add("连接已断开"); } private void btnClose_Click(object sender, EventArgs e) { blFlag = false; }
  • 2楼网友:归鹤鸣
  • 2021-02-14 18:59
很好的吧,。,
  • 3楼网友:往事埋风中
  • 2021-02-14 17:55
等啥?服务器端用TcpListener,客户端用 TcpClient。具体的例子代码网上很多的。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯