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

📄 winstream.cs

📁 一个即时通信 Messenger C#代码
💻 CS
字号:
/*

<Aicaca Community Messenger Client>

Copyright (c) Aicaca.com, All rights reserved.

Writer                : Byung-ku, Cho (Aicaca)
E-mail                : aicaca@hanmail.net
Personal Homepage URL : http://www.aicaca.com
Last Updated Date     : Feb. 27th, 2002

Programming Language  : C#
Editor & Compiler     : VisualStudio.NET

*/

using System;
using System.IO;
using System.Threading;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace TalkClient
{
	public class WinStream
	{
		private WinCom WC = new WinCom();
		private WinClient xWinClient;
		private WinSend xWinSend;
		private WinReceive xWinReceive;
		
		private Socket FileSocket = null;

		private string localID;
		private string clientID;
		private string sendStr;
		private byte[] sendStream;

		public WinStream(WinClient tWinClient, WinSend tWinSend, string tLocalID, string tClientID, string tSendStr, byte[] tSendStream)
		{
			xWinClient = tWinClient;
			xWinSend = tWinSend;
			localID = tLocalID;
			clientID = tClientID;
			sendStr = tSendStr;
			sendStream = tSendStream;
		}

		public WinStream(WinClient tWinClient, WinSend tWinSend, Socket tFileSocket)
		{
			xWinClient = tWinClient;
			xWinSend = tWinSend;
			FileSocket = tFileSocket;
		}

		public WinStream(WinClient tWinClient, WinReceive tWinReceive, string tLocalID, string tClientID, string tSendStr, byte[] tSendStream)
		{
			xWinClient = tWinClient;
			xWinReceive = tWinReceive;
			localID = tLocalID;
			clientID = tClientID;
			sendStr = tSendStr;
			sendStream = tSendStream;
		}

		public WinStream(WinClient tWinClient, WinReceive tWinReceive, Socket tFileSocket)
		{
			xWinClient = tWinClient;
			xWinReceive = tWinReceive;
			FileSocket = tFileSocket;
		}

		public void CloseThis()
		{
			if (FileSocket != null)
			{
				FileSocket.Close();
			}
		}

		public void OnReceive(string str)
		{
			str = str.Substring(0, str.Length-1);
			string sort = WC.GetXML(str, "ms");

			switch(sort)
			{
				case "ILOVEAICACAFAC":
					xWinSend.OnILOVEAICACAFAC(str);
					break;
				case "ILOVEAICACAFRE":
					xWinSend.OnILOVEAICACAFRE();
					break;
				case "ILOVEAICACAFNO":
					xWinReceive.OnILOVEAICACAFNO(str);
					break;
				case "ILOVEAICACAFCA":
					xWinReceive.OnILOVEAICACAFCA();
					break;
				case "ILOVEAICACAFIL":
					int fileLen = Convert.ToInt32(WC.GetXML(str, "fl"));
					byte[] b = new byte[fileLen];

					int idx = 0;
					while (fileLen > idx)
					{
						idx = idx + FileSocket.Receive(b, idx, fileLen-idx, 0);
					}

					xWinReceive.OnILOVEAICACAFIL(str, b);
					break;
				case "ILOVEAICACAERR":
					if (xWinSend != null)
					{
						xWinSend.OnILOVEAICACAERR(str);
					}
					else if (xWinReceive != null) 
					{
						xWinReceive.OnILOVEAICACAERR(str);
			        }
					break;
			}
		}

		public void ServerProc()
		{
			string strOne = "";
			string str = "";
			char[] c = new char[1];

			if (FileSocket != null && FileSocket.Connected)
			{
				NetworkStream stream = new NetworkStream(FileSocket);
				TextReader SocketReader = new StreamReader(stream);

				try
				{
					while (SocketReader != null && SocketReader.Read(c,0,1) != 0)
					{
						strOne = new String(c);
						str += strOne;
					
						if (strOne == "\n")
						{
							this.OnReceive(str);
							str = "";
						}	
					}
				}
				catch (Exception e)
				{
				}

				if (FileSocket != null)
				{
					FileSocket.Close();
					FileSocket = null;
				}
			}
		}

		public void ClientProc()
		{
			string strOne = "";
			string str = "";
			char[] c = new char[1];
			string clientIP = WC.GetXML(clientID, "ci");
			int clientPort = Convert.ToInt32(WC.GetXML(clientID, "cp"));

			FileSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
			IPAddress host_addr = Dns.GetHostByAddress(clientIP).AddressList[0];
			IPEndPoint ep = new IPEndPoint(host_addr, clientPort);

			try
			{
				FileSocket.Connect(ep);
			}
			catch (Exception e)
			{
				xWinClient.SendRootMsg(sendStr);
				if (sendStream != null) xWinClient.SendRootStream(sendStream);
				return;
			}

			if (FileSocket != null && FileSocket.Connected)
			{
				NetworkStream stream = new NetworkStream(FileSocket);
				TextReader SocketReader = new StreamReader(stream);

				if (xWinSend != null)
				{
					xWinSend.pFileSocket = FileSocket;
					xWinSend.SendMsg(sendStr);
					if (sendStream != null) xWinSend.SendStream(sendStream);
				}
				else
				{
					xWinReceive.pFileSocket = FileSocket;
					xWinReceive.SendMsg(sendStr);
					if (sendStream != null) xWinReceive.SendStream(sendStream);

				}

				try
				{
					while (SocketReader != null && SocketReader.Read(c,0,1) != 0)
					{
						strOne = new String(c);
						str += strOne;
					
						if (strOne == "\n")
						{
							this.OnReceive(str);
							str = "";
						}	
					}
				}
				catch (Exception e)
				{
				}

				if (FileSocket != null)
				{
					FileSocket.Close();
					FileSocket = null;
				}
			}
		}
	}
}

⌨️ 快捷键说明

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