永发信息网

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

答案:4  悬赏:0  手机版
解决时间 2021-02-14 21:05
在线等~~~
最佳答案
服务端:
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();
}
}
}
全部回答
//客户端连接按钮 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; }
很好的吧,。,
等啥?服务器端用TcpListener,客户端用 TcpClient。具体的例子代码网上很多的。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
我家的母孔雀鱼变白、呆在缸底、肚子很大、不
关于食品安全的作文,购买具有标签的食物,少
莱西市人民医院停车场地址有知道的么?有点事
停车场(团岛中路)地址好找么,我有些事要过去
打印婚纱照一般用什么打印机?
既然选择了,就没有后悔的机会。你不勇敢,没
用C++在共享资源下载东西时,为什么说“无法打
停车场(琴岛路)地址在哪,我要去那里办事
潍坊市质量技术监督局组织机构代码办公室峡山
5除以一个数,商是24,余数应该是 A比5大的数 B
如何检查男人的贞操
看到前女友发朋友圈晒幸福,心理不平衡嫉妒的
停车场(宏东嘉苑西北)地址有知道的么?有点事
如何解决低压电机启动后增加负荷导致电压过低
在一元夺宝中个苹果货没到不小心点了确认收货
推荐资讯
一直低烧会怎么样
我是迅雷会员 为什么每次进私密空间密码都没
云柜快递柜(小溪路2号滨江花园8号云柜快递柜)
【2010刘谦春晚魔术】(2010?秦淮区一模)2010
男人会对自己的女儿有性幻想吗?
1.翻译句子:(12分)(1)方立庙门,有俊鹘抟
隋唐英雄4第几集樊梨花生子
在汕头卜蜂莲花超市买手机送话费,受骗。去投
什么样的汽车面漆才需要喷底漆
崔铭珊名字含义是什么呢!
我不喜欢现在的工作,想回家,想找一个自己喜
白鹤山居委会地址好找么,我有些事要过去
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?