求!!!用C#中的Socket套接字实现一个服务器与多个客户端的通信代码
1个回答
展开全部
//一个最简单的Socket服务端程序,不知道是否对你有用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SocketServer1
{
class Program
{
static bool ServiceStartFlag = false;
static Socket socket;
static Thread thread;
static void Main(string[] args)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry ieh = Dns.GetHostEntry("localhost");
IPAddress localServerIP = ieh.AddressList[1];
IPEndPoint localIPEndPoint = new IPEndPoint(localServerIP, 8080);
socket.Bind(localIPEndPoint);
socket.Listen(600);
thread = new Thread(new ThreadStart(AcceptClient));
thread.IsBackground = true;
thread.Start();
Console.ReadLine();
}
static void AcceptClient()
{
ServiceStartFlag = true;
while (ServiceStartFlag)
{
try
{
Socket newSocket = socket.Accept();
string onemessge = "<cross-domain-policy><allow-access-from domain=\"" + "*" + "\" to-ports=\"8080\"/></cross-domain-policy>\0";
byte[] tmpBytes = Encoding.UTF8.GetBytes(onemessge);
newSocket.Send(tmpBytes);
Thread newThread = new Thread(new ParameterizedThreadStart(ReadMsg));
newThread.IsBackground = true;
object obj = newSocket;
newThread.Start(obj);
}
catch (SocketException ex)
{
}
}
}
static void ReadMsg(object obj)
{
Socket socket = (Socket)obj;
byte[] byteMessage = null; ;
while (ServiceStartFlag)
{
try
{
if (socket.Connected)
{
byteMessage = new byte[1000];
int len = socket.Receive(byteMessage);
if (len > 0)
{
string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime + ":" + "Message from: ";
msg += socket.RemoteEndPoint.ToString() + " << " + Encoding.UTF8.GetString(byteMessage);
Console.WriteLine(msg);
byteMessage = null;
byte[] tmpBytes = Encoding.UTF8.GetBytes("Sended Sucessed!\0");
socket.Send(tmpBytes);
}
}
}
catch (SocketException ex)
{
//Console.WriteLine(ex.Message);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SocketServer1
{
class Program
{
static bool ServiceStartFlag = false;
static Socket socket;
static Thread thread;
static void Main(string[] args)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry ieh = Dns.GetHostEntry("localhost");
IPAddress localServerIP = ieh.AddressList[1];
IPEndPoint localIPEndPoint = new IPEndPoint(localServerIP, 8080);
socket.Bind(localIPEndPoint);
socket.Listen(600);
thread = new Thread(new ThreadStart(AcceptClient));
thread.IsBackground = true;
thread.Start();
Console.ReadLine();
}
static void AcceptClient()
{
ServiceStartFlag = true;
while (ServiceStartFlag)
{
try
{
Socket newSocket = socket.Accept();
string onemessge = "<cross-domain-policy><allow-access-from domain=\"" + "*" + "\" to-ports=\"8080\"/></cross-domain-policy>\0";
byte[] tmpBytes = Encoding.UTF8.GetBytes(onemessge);
newSocket.Send(tmpBytes);
Thread newThread = new Thread(new ParameterizedThreadStart(ReadMsg));
newThread.IsBackground = true;
object obj = newSocket;
newThread.Start(obj);
}
catch (SocketException ex)
{
}
}
}
static void ReadMsg(object obj)
{
Socket socket = (Socket)obj;
byte[] byteMessage = null; ;
while (ServiceStartFlag)
{
try
{
if (socket.Connected)
{
byteMessage = new byte[1000];
int len = socket.Receive(byteMessage);
if (len > 0)
{
string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime + ":" + "Message from: ";
msg += socket.RemoteEndPoint.ToString() + " << " + Encoding.UTF8.GetString(byteMessage);
Console.WriteLine(msg);
byteMessage = null;
byte[] tmpBytes = Encoding.UTF8.GetBytes("Sended Sucessed!\0");
socket.Send(tmpBytes);
}
}
}
catch (SocketException ex)
{
//Console.WriteLine(ex.Message);
}
}
}
}
}
追问
这是对一个客户端的吧???
追答
是的这是对一个客户端的,但是如果想对多客户端的,我的想法是建一个线程数组吧,能理解我的想法吧?和这一个客户端好像也不大区别.我也研究这个很皮毛,所以只能帮你想想:)
威孚半导体技术
2024-08-19 广告
2024-08-19 广告
威孚(苏州)半导体技术有限公司是一家专注生产、研发、销售晶圆传输设备整机模块(EFEM/SORTER)及核心零部件的高科技半导体公司。公司核心团队均拥有多年半导体行业从业经验,其中技术团队成员博士、硕士学历占比80%以上,依托丰富的软件底层...
点击进入详情页
本回答由威孚半导体技术提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询