亏求一个例子 这2天需要弄一个c#socket 服务器与多客户端的简单数据的通讯,给个针对性的实例学习学习呗,谢谢C#socket 多线程 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.csdn.net/qq542369628/article/details/8538541http://blog.csdn.net/qq542369628/article/details/8538961 www.51aspx.com 里面有很多资料呢 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Net;using System.Net.Sockets;using System.Threading;using System.Collections.Generic;namespace ConsoleApplication_socketServer{ class Program { static Socket serverSocket; static Socket clientSocket; static List<Socket> al=new List<Socket>(); static Thread thread; static Thread doSend; static void Main(string[] args) { IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 3001); serverSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(ipep); serverSocket.Listen(10); while (true) { //clientSocket = serverSocket.Accept(); al.Add(serverSocket.Accept()); //Console.WriteLine(al.Count); thread = new Thread(new ThreadStart(doWork)); thread.Start(); doSend = new Thread(send); outBufferStr = Console.ReadLine(); doSend.Start(outBufferStr); } } static Byte[] inBuffer = new Byte[16]; static Byte[] outBuffer = new Byte[16]; static String inBufferStr; static String outBufferStr; private static void send(object o){ try { while (true) { for (int i = 0; i < al.Count; i++) { outBuffer = Encoding.ASCII.GetBytes(o.ToString()); al[i].Send(outBuffer, outBuffer.Length, SocketFlags.None); } } } catch { Console.WriteLine("客户端已关闭!"); } } private static void doWork() { try { while (true) { for (int i = 0; i < al.Count; i++) { IPEndPoint ipEndPoint = (IPEndPoint)al[i].RemoteEndPoint; String address = ipEndPoint.Address.ToString(); String port = ipEndPoint.Port.ToString(); Console.WriteLine(address + ":" + port + " 连接过来了"); al[i].Receive(inBuffer, 16, SocketFlags.None);//如果接收的消息为空 阻塞 当前循环 inBufferStr = Encoding.ASCII.GetString(inBuffer); Console.WriteLine(address + ":" + port + "说:"); Console.WriteLine(inBufferStr); } } } catch { Console.WriteLine("客户端已关闭!"); } } }}-------------------------------------------------------------------using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;namespace ConsoleApplication_socketClient{ class Program { static Socket clientSocket; static void Main(string[] args) { //将网络端点表示为IP地址和端口 用于socket侦听时绑定 IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.4"), 3001); //填写自己电脑的IP或者其他电脑的IP,如果是其他电脑IP的话需将ConsoleApplication_socketServer工程放在对应的电脑上。 clientSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); //将Socket连接到服务器 try { clientSocket.Connect(ipep); String outBufferStr; Byte[] outBuffer = new Byte[16]; Byte[] inBuffer = new Byte[16]; while (true) { //发送消息 outBufferStr = Console.ReadLine(); outBuffer = Encoding.ASCII.GetBytes(outBufferStr); clientSocket.Send(outBuffer, outBuffer.Length, SocketFlags.None); //接收服务器端信息 clientSocket.Receive(inBuffer, 16, SocketFlags.None);//如果接收的消息为空 阻塞 当前循环 Console.WriteLine("服务器说:"); Console.WriteLine(Encoding.ASCII.GetString(inBuffer)); } } catch { Console.WriteLine("服务未开启!"); Console.ReadLine(); } } }}我是这么写的 可是没有到达效果 C# 提取照片 创建时间,创建地点 关于事件处理的问题 想学习.net,大家给推荐个培训学校 集合已经修改,枚举操作可能不会执行. 关于.net中的asp项目调用存储过程的问题! !!!高分求解!!! --- DATAGRID 的 KEYDOWN 和 KEYPRESS 事件怎么没有反映? --- 关于在.net中输入文本段落的问题,很简单的,请教大家 C#如何实现FTP断点上传 c# 定时关闭程序 和 定时关机 C#动态链接ORACLE 关于GIS地图问题,新人求教。 求问~定时器触发事件影响主窗体问题
http://blog.csdn.net/qq542369628/article/details/8538961
{ class Program
{ static Socket serverSocket; static Socket clientSocket;
static List<Socket> al=new List<Socket>(); static Thread thread;
static Thread doSend;
static void Main(string[] args)
{ IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 3001); serverSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(ipep); serverSocket.Listen(10); while (true)
{ //clientSocket = serverSocket.Accept(); al.Add(serverSocket.Accept());
//Console.WriteLine(al.Count);
thread = new Thread(new ThreadStart(doWork)); thread.Start(); doSend = new Thread(send);
outBufferStr = Console.ReadLine();
doSend.Start(outBufferStr); } } static Byte[] inBuffer = new Byte[16]; static Byte[] outBuffer = new Byte[16]; static String inBufferStr; static String outBufferStr; private static void send(object o){
try {
while (true) { for (int i = 0; i < al.Count; i++)
{
outBuffer = Encoding.ASCII.GetBytes(o.ToString());
al[i].Send(outBuffer, outBuffer.Length, SocketFlags.None);
}
}
}
catch
{
Console.WriteLine("客户端已关闭!");
}
}
private static void doWork()
{ try
{ while (true)
{
for (int i = 0; i < al.Count; i++)
{
IPEndPoint ipEndPoint = (IPEndPoint)al[i].RemoteEndPoint; String address = ipEndPoint.Address.ToString(); String port = ipEndPoint.Port.ToString(); Console.WriteLine(address + ":" + port + " 连接过来了");
al[i].Receive(inBuffer, 16, SocketFlags.None);//如果接收的消息为空 阻塞 当前循环 inBufferStr = Encoding.ASCII.GetString(inBuffer); Console.WriteLine(address + ":" + port + "说:"); Console.WriteLine(inBufferStr); }
} } catch
{ Console.WriteLine("客户端已关闭!"); } } }}-------------------------------------------------------------------
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;namespace ConsoleApplication_socketClient
{ class Program
{ static Socket clientSocket; static void Main(string[] args)
{ //将网络端点表示为IP地址和端口 用于socket侦听时绑定 IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.4"), 3001); //填写自己电脑的IP或者其他电脑的IP,如果是其他电脑IP的话需将ConsoleApplication_socketServer工程放在对应的电脑上。 clientSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); //将Socket连接到服务器 try
{ clientSocket.Connect(ipep); String outBufferStr; Byte[] outBuffer = new Byte[16]; Byte[] inBuffer = new Byte[16]; while (true)
{ //发送消息 outBufferStr = Console.ReadLine(); outBuffer = Encoding.ASCII.GetBytes(outBufferStr); clientSocket.Send(outBuffer, outBuffer.Length, SocketFlags.None); //接收服务器端信息 clientSocket.Receive(inBuffer, 16, SocketFlags.None);//如果接收的消息为空 阻塞 当前循环 Console.WriteLine("服务器说:"); Console.WriteLine(Encoding.ASCII.GetString(inBuffer)); } } catch
{ Console.WriteLine("服务未开启!"); Console.ReadLine(); } } }}我是这么写的 可是没有到达效果