日期:2014-05-17 浏览次数:21083 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace Network {
public class TcpClient {
private Socket _Socket = null; //定义Socket
private IPEndPoint ipEndPoint = null; //定义IP端口
public byte[] buffer = new byte[1]; //字符缓冲区
public bool IsConnected { //判断网络是否已连接
get {
if (_Socket == null) {
return false;
}
return true;
}
}
public TcpClient() {
}
public TcpClient(DataStructure.IPContext _ipContext) {
InitSocket(_ipContext.Ip, _ipContext.Port); //调用初始化Socket函数
}
public TcpClient(IPAddress _ip, int _port) {
InitSocket(_ip, _port); //调用初始化Socket函数
}
public void InitSocket(IPAddress _ip, int _port) { //初始化Socket
ipEndPoint = new IPEndPoint(_ip, _port);
_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //TCP Socket
_Socket.SendTimeout = 2000; //发送时间
_Socket.ReceiveTimeout = 3000; //接收时间
_Socket.ReceiveBufferSize = 1; //缓冲区大小
}
/*
* 连接初始化后的_Socket
* 连接成功返回true
* 连接失败返回false