110,539
社区成员
发帖
与我相关
我的任务
分享
public string receiveContent(String destinationNo, String content)
{
String stringdata;
//将目标号码与短信内容拼接成一条String语句
stringdata = destinationNo + content + "<EOF>";
IPAddress ipAddress = IPAddress.Parse("192.168.0.50");
int port = 1234;
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// 生成一个TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Thread.Sleep(100);
// 与目标终端连接.
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
Thread.Sleep(100);
// 发送数据到远程终端.
Send(client, stringdata);
sendDone.WaitOne();
//收到返回的数据
// Receive(client);
// receiveDone.WaitOne();
client.Shutdown(SocketShutdown.Both);
client.Close();
return stringdata;
}
private void ConnectCallback(IAsyncResult ar)
{
// 从state对象获取socket.
Socket client = (Socket)ar.AsyncState;
// 完成连接.
client.EndConnect(ar);
connectDone.Set();
}
private void Send(Socket client, String data)
{
// 格式转换.
byte[] byteData = Encoding.Unicode.GetBytes(data);
// 开始发送数据到winform.
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
}
private void SendCallback(IAsyncResult ar)
{
// 从state对象中获取socket
Socket client = (Socket)ar.AsyncState;
// 完成数据发送.
int bytesSent = client.EndSend(ar);
client.Shutdown(SocketShutdown.Both);
client.Close();
sendDone.Set();
}