client.cs

来自「.net2003 C# 基于pda的winsock通讯」· CS 代码 · 共 73 行

CS
73
字号
using System;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace SmartDeviceApplication5
{

	public  class Client:SocketBase
	{
		TcpClient client; 
		public Client()
		{
			client = new TcpClient();

		}
		public Connection StartClient(IPAddress ipaddress,int port) 
		{
            
			
			try
			{	
				if ( this.linewnum == 0)
				{
					client.Connect(ipaddress,port);
					this.Connection = new Connection(client.GetStream());
					this.linewnum = 1;
					this.StartListenAndSend();
                    
				}
				else
				{
					client.Connect(ipaddress,port);
					this.Connection = new Connection(client.GetStream());
					this.linewnum = 1;

				}
		        
				//this._connections.Add(new Connection(client.GetStream()));
				
			}
			catch (SocketException e)
			{

				if (e.ErrorCode == 10061 )
				{
					MessageBox.Show("服务器没有启动!");
					Application.Exit();
					return this.Connection;
				}
				if (e.ErrorCode == 10056 )	

				{

					client = new TcpClient();
					client.Connect(ipaddress,port);
					this.Connection = new Connection(client.GetStream());
					this.linewnum = 1;
					this.StartListenAndSend();
				}

			}
                
                
//                MessageBox.Show("连接成功!");
				return this.Connection;
		}
		public void StopClient() 
		{
			this.EndListenAndSend();	 
		}
	}
}

⌨️ 快捷键说明

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