Socket通信简单实例(WCF调用Socket)
服务端:
控制台程序监听
1 /// <summary> 2 /// Server 3 /// </summary> 4 class Program 5 { 6 static Socket serverSocket; 7 static Socket clientSocket; 8 static Thread thread; 9 10 static void Main(string[] args) 11 { 12 IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 3001); 13 serverSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); 14 serverSocket.Bind(ipep); 15 serverSocket.Listen(10); 16 while (true) 17 { 18 clientSocket = serverSocket.Accept(); 19 thread = new Thread(new ThreadStart(doWork)); 20 thread.Start(); 21 } 22 23 } 24 25 private static void doWork() 26 { 27 Socket s = clientSocket;//客户端信息 28 IPEndPoint ipEndPoint = (IPEndPoint)s.RemoteEndPoint; 29 String address = ipEndPoint.Address.ToString(); 30 String port = ipEndPoint.Port.ToString(); 31 Console.WriteLine(address + ":" + port + " 连接过来了"); 32 Byte[] inBuffer = new Byte[1024]; 33 Byte[] outBuffer = new Byte[1024]; 34 String inBufferStr; 35 String outBufferStr; 36 try 37 { 38 while (true) 39 { 40 s.Receive(inBuffer, 1024, SocketFlags.None); 41 inBufferStr = Encoding.ASCII.GetString(inBuffer); 42 Console.WriteLine(address + ":" + port + "说:"); 43 Console.WriteLine(inBufferStr); 44 outBufferStr = Console.ReadLine(); 45 outBuffer = Encoding.ASCII.GetBytes(outBufferStr); 46 s.Send(outBuffer, outBuffer.Length, SocketFlags.None); 47 } 48 } 49 catch 50 { 51 Console.WriteLine("客户端已关闭!"); 52 } 53 } 54 }
客户端
业务逻辑层
1 namespace CloudTraPlatWcf.Business 2 { 3 /// <summary> 4 /// 5 /// </summary> 6 public class BOutSide_Bond 7 { 8 private string Path = @"\Side_Bond"; //日志存放路径 9 static Socket _ClientSocket; 10 11 #region 私有方法 12 /// <summary> 13 /// 14 /// </summary> 15 /// <param name="outBufferStr">退还信息</param> 16 /// <returns>退还结果</returns> 17 public string HelpSendAndReceiveMessage(string outBufferStr) 18 { 19 string Ip = ConfigHelper.GetReturnBondIP(); 20 int Port = ConfigHelper.GetReturnBondPort(); 21 IPEndPoint _IPEndPoint = new IPEndPoint(IPAddress.Parse(Ip), Port); 22 try 23 { 24 if (_ClientSocket == null) 25 { 26 _ClientSocket = new Socket(_IPEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); 27 _ClientSocket.Connect(_IPEndPoint); //将Socket连接到服务器 28 } 29 Byte[] outBuffer = new Byte[1024]; 30 Byte[] inBuffer = new Byte[1024]; 31 //发送消息 32 outBuffer = Encoding.ASCII.GetBytes(outBufferStr); 33 _ClientSocket.Send(outBuffer, outBuffer.Length,SocketFlags.None); 34 //接收服务器端信息 35 _ClientSocket.Receive(inBuffer, 1024, SocketFlags.None); 36 string LogMsg = ""; 37 Logger.Write(LogMsg,"",Path); 38 return Encoding.ASCII.GetString(inBuffer); 39 } 40 catch (Exception ex) 41 { 42 throw ex; 43 } 44 } 45 #endregion 46 } 47 }
WCF契约
1 namespace CloudTraPlatWcf.Contracts 2 { 3 /// <summary> 4 /// 短信接口 5 /// </summary> 6 [ServiceContract(Name = "IOutSide_SMSContract")] 7 public interface IOutSide_SMSContract 8 { 9 /// <summary> 10 /// 11 /// </summary> 12 /// <returns></returns> 13 [OperationContract] 14 string Test(); 15 } 16 }
WCF服务
#region IOutSide_SMSContract 成员 public string Test() { string res = new BOutSide_Bond().HelpSendAndReceiveMessage("Hello"); return res; } #endregion