using System;
|
using System.Net;
|
using System.Net.Sockets;
|
using System.Text;
|
using EventTool;
|
|
public class SocketTools
|
{
|
private int clientPort;
|
private NetworkStream clientStream;
|
private TcpListener listener;
|
private int listenPort;
|
private IPAddress localIp;
|
//private LogManager loger = new LogManager(Type.GetType("Vision.Socket"), Environment.CurrentDirectory + @"\Socket.log");
|
private TcpClient remoteClient;
|
private IPAddress remoteIp;
|
private NetworkStream remoteStream;
|
//电脑的监听端口,接收方端口,机械手IP,本地IP
|
public SocketTools(int listenPort, int clientPort, IPAddress remoteIp, IPAddress localIp)
|
{
|
this.remoteIp = remoteIp;
|
this.listenPort = listenPort;
|
this.clientPort = clientPort;
|
this.listener = new TcpListener(localIp, listenPort);
|
this.LocalIp = localIp;
|
}
|
|
public void CloseAll()
|
{
|
this.CloseListener();
|
}
|
|
public void CloseListener()
|
{
|
if (this != null)
|
{
|
if (this.remoteStream != null)
|
{
|
this.remoteStream.Close();
|
this.remoteStream.Dispose();
|
}
|
if (this.remoteClient != null)
|
{
|
this.remoteClient.Close();
|
this.remoteClient = null;
|
}
|
if (this.listener != null)
|
{
|
this.listener.Stop();
|
this.listener = null;
|
}
|
}
|
}
|
|
protected string GetIP()
|
{
|
IPAddress[] hostAddresses = Dns.GetHostAddresses(Dns.GetHostName());
|
for (int i = 0; i < hostAddresses.Length; i++)
|
{
|
if (hostAddresses[i].ToString().Length > 3)
|
{
|
return hostAddresses[i].ToString();
|
}
|
}
|
return "";
|
}
|
|
public void Listening(ListenEventCreater creater)
|
{
|
this.listener.Start();
|
while (true)
|
{
|
try
|
{
|
if (this.listener == null)
|
{
|
this.listener = new TcpListener(this.LocalIp, this.listenPort);
|
this.listener.Start();
|
}
|
if (!this.listener.Pending()) continue;
|
this.remoteClient = this.listener.AcceptTcpClient();
|
this.remoteStream = this.remoteClient.GetStream();
|
if (this.remoteStream.CanRead)
|
{
|
byte[] buffer = new byte[this.remoteClient.ReceiveBufferSize];
|
this.remoteStream.Read(buffer, 0, this.remoteClient.ReceiveBufferSize);
|
string str = Encoding.Default.GetString(buffer);
|
if (str.Contains("CLOSELISTENER"))
|
{
|
return;
|
}
|
EventData iData = new EventData(str.Trim(new char[1]));
|
creater.ChangeValue(iData);
|
}
|
}
|
catch (Exception)
|
{
|
//this.loger.WriteLog(string.Concat(new object[] { "监听问题:", exception.Source, exception.TargetSite, exception.ToString(), "\r\n" }), LogType.Info);
|
}
|
finally
|
{
|
if (this.remoteStream != null)
|
{
|
this.remoteStream.Close();
|
this.remoteStream.Dispose();
|
}
|
if (this.remoteClient != null)
|
{
|
this.remoteClient.Close();
|
}
|
}
|
}
|
}
|
|
public bool SendMessage(string messsge)
|
{
|
return this.SendMessage(this.remoteIp, this.clientPort, messsge);
|
}
|
|
public bool SendMessage(IPAddress ip, int port, string messsge)
|
{
|
bool flag;
|
TcpClient client = new TcpClient();
|
NetworkStream stream = null;
|
try
|
{
|
client.Connect(ip, port);
|
stream = client.GetStream();
|
if (stream.CanWrite)
|
{
|
byte[] bytes = Encoding.ASCII.GetBytes(messsge + "\r\n");
|
stream.Write(bytes, 0, bytes.Length);
|
return true;
|
}
|
flag = false;
|
}
|
catch (Exception)
|
{
|
//this.loger.WriteLog(string.Concat(new object[] { "发送Socket数据问题:", exception.Source, exception.TargetSite, exception.ToString(), "\r\n" }), LogType.Info);
|
flag = false;
|
}
|
finally
|
{
|
if (stream != null)
|
{
|
stream.Close();
|
}
|
if (client != null)
|
{
|
client.Close();
|
}
|
}
|
return flag;
|
}
|
|
//System.Threading.Thread receiveThread;
|
NetworkStream stm = null;
|
|
public bool SendAndReceiveMessage(IPAddress ip, int port, ref string messsge)
|
{
|
bool flag = false;
|
TcpClient tcpClient = new TcpClient();
|
|
try
|
{
|
//tcpClient = TimeOutSocket.TryConnect(new IPEndPoint(ip, port), 3000);// new TcpClient();
|
tcpClient.Connect(ip, port);
|
stm = tcpClient.GetStream();
|
//receiveThread = new System.Threading.Thread(ReceiveMessage);
|
//receiveThread.Start();
|
|
if (stm.CanWrite)
|
{
|
byte[] bytes = Encoding.ASCII.GetBytes(messsge + "\n");
|
byte[] readBytes = new byte[40];
|
|
stm.Write(bytes, 0, bytes.Length);
|
|
System.Threading.Thread.Sleep(100);
|
stm.Read(readBytes, 0, readBytes.Length);
|
|
messsge = Encoding.GetEncoding("gb2312").GetString(readBytes);
|
}
|
}
|
catch (Exception)
|
{
|
//this.loger.WriteLog(string.Concat(new object[] { "发送接收Socket数据问题:", exception.Source, exception.TargetSite, exception.ToString(), "\r\n" }), LogType.Info);
|
flag = false;
|
}
|
finally
|
{
|
if (stm != null)
|
{
|
stm.Close();
|
}
|
if (tcpClient != null)
|
{
|
tcpClient.Close();
|
}
|
}
|
return flag;
|
}
|
|
// 接收消息方法
|
private void ReceiveMessage()
|
{
|
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
while (true)
|
{
|
try
|
{
|
// 关闭receiveUdpClient时此时会产生异常
|
Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
|
byte[] receiveBytes;
|
byte[] tcpreceiveBytes = new byte[4096];
|
int count = 0;
|
|
|
//if (netSelected == 0)
|
//{
|
// receiveBytes = receiveUpdClient.Receive(ref remoteIpEndPoint);
|
//}
|
//else
|
{
|
|
count = stm.Read(tcpreceiveBytes, 0, 2048);
|
receiveBytes = tcpreceiveBytes;
|
|
}
|
|
string message = gb2312.GetString(receiveBytes);
|
|
// 显示消息内容
|
//ShowMessageforView(texboxMessageView, string.Format("{0}", message));
|
|
}
|
catch (Exception)
|
{
|
break;
|
}
|
}
|
}
|
|
|
public void Stop()
|
{
|
this.SendMessage(this.localIp, this.listenPort, "CLOSELISTENER");
|
}
|
|
public int ClientPort
|
{
|
get
|
{
|
return this.clientPort;
|
}
|
set
|
{
|
this.clientPort = value;
|
}
|
}
|
|
public NetworkStream ClientStream
|
{
|
get
|
{
|
return this.clientStream;
|
}
|
set
|
{
|
this.clientStream = value;
|
}
|
}
|
|
public int ListenPort
|
{
|
get
|
{
|
return this.listenPort;
|
}
|
set
|
{
|
this.listenPort = value;
|
}
|
}
|
|
public TcpListener Listerner
|
{
|
get
|
{
|
return this.listener;
|
}
|
set
|
{
|
this.listener = value;
|
}
|
}
|
|
public IPAddress LocalIp
|
{
|
get
|
{
|
return this.localIp;
|
}
|
set
|
{
|
this.localIp = value;
|
}
|
}
|
|
public TcpClient RemoteClient
|
{
|
get
|
{
|
return this.remoteClient;
|
}
|
set
|
{
|
this.remoteClient = value;
|
}
|
}
|
|
public IPAddress RemoteIp
|
{
|
get
|
{
|
return this.remoteIp;
|
}
|
set
|
{
|
this.remoteIp = value;
|
}
|
}
|
|
public NetworkStream RemoteStream
|
{
|
get
|
{
|
return this.remoteStream;
|
}
|
set
|
{
|
this.remoteStream = value;
|
}
|
}
|
}
|