写socket服务报错,请贵人相助
问题描述:System.Net.Sockets.SocketException: 由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。
at System.Net.Sockets.Socket.get_RemoteEndPoint()
at UtilitesCEBSocket.ClientSocket.ConnectCallback(IAsyncResult ar)
详细代码:
/// <summary>
/// 客户端连接远程服务入口
/// </summary>
/// <param name="argMsg">发送的信息</param>
/// <returns>发送成功返回0</returns>
public string StartClient(string argMsg)
{
LogBLL.Debug("客户端开始:");
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.SendTimeout = SendTimeout;
client.ReceiveTimeout = ReceiveTimeout;
LogBLL.Debug("客户端连接到远程服务开始:");
IPAddress ipAddress = IPAddress.Parse(ClientIP);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, ClientPort);
// Connect to the remote endpoint. 连接到远程端点
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client, argMsg);
sendDone.WaitOne();
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
LogBLL.Debug("客户端结束:");
//Console.Read();
return "0";
}
/// <summary>
/// 异步连接
/// </summary>
/// <param name="ar"></param>
private void ConnectCallback(IAsyncResult ar)
{
//处理线程的异常
try
{
LogBLL.Info("客户端连接到1:{ 开始 }");
//判断网络ip和端口号是否正常
IsOpenPort();
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
LogBLL.Info("客户端连接到2:{" + client.RemoteEndPoint.ToString() + "}");
// Complete the connection.
client.EndConnect(ar);
//Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString());
LogBLL.Info("客户端连接到3:{ 结束}");
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception ex)
{
LogBLL.Error("ConnectCallback Call接口有问题:" + ex.ToString());
throw ex;
}
}
//检测对方端口是否通
public void IsOpenPort()
{
string ip = ClientIP;
int port = ClientPort;
bool tcpListen = false;
bool udpListen = false;//设定端口状态标识位
System.Net.IPAddress myIpAddress = IPAddress.Parse(ip);
System.Net.IPEndPoint myIpEndPoint = new IPEndPoint(myIpAddress, port);
try
{
System.Net.Sockets.TcpClient tcpClient = new TcpClient();
tcpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出TCP连接请求
tcpListen = true;
System.Net.Sockets.UdpClient udpClient = new UdpClient();
udpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出UDP连接请求
udpListen = true;
if (tcpListen == false && udpListen == false)//7411端口关闭!","提示
{
LogBLL.Error("失败连接ip为:" + ip + " 端口为:" + port);
}
else
{
LogBLL.Info("成功连接至对方!" + ip + " 端口为:" + port);
}
}
catch (Exception ex)
{
LogBLL.Error("连接对方报错信息如下:"+ex);
}
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="client">客户端SOCKET</param>
/// <param name="data">发送的数据</param>
private void Send(Socket client, String data)
{
LogBLL.Info("客户端开始发送数据:" + data);
// Convert the string data to byte data using ASCII encoding.
Encoding encode = Encoding.GetEncoding(936); //gbk
byte[] byteData = encode.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
LogBLL.Info("发送报文的长度:" + byteData.Length + "发送内容:" + data);
LogBLL.Info("客户端发送数据结束");
}
/// <summary>
/// 发送回调
/// </summary>
/// <param name="ar"></param>
private void SendCallback(IAsyncResult ar)
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
//Console.WriteLine("Sent {0} bytes to server.", bytesSent);
LogBLL.Info("客户端发送 {" + bytesSent + "} bytes 远程服务端.");
// Signal that all bytes have been sent.
sendDone.Set();
}
补充:.NET技术 , C#