⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketextend.cs

📁 SOCKET 发送和接受字符串的小程序
💻 CS
字号:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;


namespace SenderReceiver
{
	/// <summary>
	/// Class1 的摘要说明。
	/// </summary>
	public class SocketExtend
	{
		public SocketExtend()
		{
		ip="127.0.0.1";
		port="5677";
		}

		public bool Connect()
		{
			if(ip==null||port==null)
				throw new Exception("端口号或ip地址未指定 In SocketExtend--Connect()");
			else
			{
				try
				{
					Sock2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
					IPEndPoint hostEndPoint=new IPEndPoint( IPAddress.Parse(ip),int.Parse(port)); 
				    Sock2.Connect(hostEndPoint) ;
					return true;
				}
				catch(Exception ex)
				{
				 throw new Exception(ex.Message+"In SocketExtend--Connect()");
				 
				}
			}
		}
		public bool Sendstr(string str)
		{
			if(Sock2==null)
				throw new Exception("socket 未连接 In SocketExtend--Sendstr(string str)");
			else{
				try
				{
					Sock2.Send(UTF8Encoding.GetEncoding(54936).GetBytes(str));	
					return true;
				}
				catch(Exception ex)
				{
				  throw new Exception(ex.Message+"In SocketExtend--Connect()");
                
				}
			}  	
	   }
		public string  Receivestr()
		{
			try
			{
				string receiveStr=null;
				byte []buffer=new byte[1024];
				Sock2.Receive(buffer);
				receiveStr=UTF8Encoding.GetEncoding(54936).GetString(buffer);
				return  receiveStr;
			}
			catch(Exception ex)
			{
			throw new Exception(ex.Message+"In SocketExtend--Receivestr()");
			
			}
		 }
		/// <summary>
		/// lengthByte 表示接受串的前几个字节表示长度
		/// 根据协议接收时用
		/// 返回去掉“表示长度的字节”后的字符串
		/// </summary>
		/// <param name="lengthByte"></param>
		/// <returns></returns>
		public string  Receivestr(int lengthByte)
		{
			byte[] data = new byte[lengthByte];
			byte[] data2 = new byte[1024];
			string receiveStr=null;
			int offset=0;
			try
			{
				while(lengthByte>0) 
				{
					if(Sock2!=null)
					{
					Int32 bytes= Sock2.Receive(data,offset,lengthByte,SocketFlags.None); 
					offset+=bytes;
					lengthByte-=bytes;
					}
					else
					 throw new Exception("断开连接");
				}	
				
				//类型转化
				String s_num=UTF8Encoding.GetEncoding(54936).GetString(data).Trim();
				//取得要接收长度
				int num=Int32.Parse(s_num);
				
				offset=0;			
				//按长度接收数据
				while(num>0)
				{
					Int32 bs = Sock2.Receive(data2,offset,num,0);
					num-=bs;
					offset+=bs;
				}	
				receiveStr=UTF8Encoding.GetEncoding(54936).GetString(data2);
				return  receiveStr;
			}
			catch(Exception ex)
			{
			throw new Exception(ex.Message+"In SocketExtend--Receivestr(int lengthByte)");
			
			}
           
		}
		public void close()
		{
			if(Sock2 != null)
			{
				Sock2.Shutdown(SocketShutdown.Both);
				Sock2.Close();
			}
		}
	   	private string ip=null;
		private string port=null;
       
		Socket Sock2;
		
		public string IP
		{
			set
			{
             ip=value;
			}
		}

		public string PORT
		{
			set
			{
				port=value;
			}
		}
        
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -