otherclient.cs

来自「c#网络编程及应用-刘瑞新」· CS 代码 · 共 66 行

CS
66
字号
using System;

using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace Game2D
{
	/// <summary>
	/// ThreadProcessor 的摘要说明。
	/// </summary>
	///
 
	public class OtherClient
	{

		private Socket client;
		private byte[] bb=new Byte[256];

		public OtherClient(Socket clientSocket)
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
			client=clientSocket;

			bb=System.Text.Encoding.BigEndianUnicode.GetBytes("Sorry");
			try
			{
				client.Send(bb,bb.Length,0);
			}
			catch
			{
				Initiator.richTextBox1.AppendText("error seading sorry--mj");
			}
		}

		public void ReceiveClient()
		{
			int bytes=0;
			byte[] bb=new byte[256];
			while(true)
			{
				try
				{
					bytes=client.Receive(bb,bb.Length,0);
				}
				catch
				{
					Initiator.richTextBox1.AppendText("error reading otherclient data");
					break;
				}
				if(bytes==0) break;
				string mystring=System.Text.Encoding.BigEndianUnicode.GetString(bb,0,bytes);
				if(mystring=="GoodBye")
				{
					break;
				}
			}
			Initiator.richTextBox1.AppendText("多余人退出1个。\n");
			client.Shutdown(SocketShutdown.Both);
			client.Close();
		}
	}
}

⌨️ 快捷键说明

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