日期:2014-05-17 浏览次数:20955 次
/*
*
* 通信主程序:发送和接收
*
*/
private string SendRecieve(string strToSend)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress myIP = IPAddress.Parse(strAppServerIP);
IPEndPoint EPhost = new IPEndPoint(myIP, iAppServerPort);
try
{
socket.Connect(EPhost);
}
catch (SocketException e)
{
return "ERR" + e.Message;
}
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 50000);//50秒超时
byte[] sendBytes = System.Text.Encoding.Default.GetBytes(strToSend + "\rbye\r");
socket.Send(sendBytes, sendBytes.Length, SocketFlags.None);
//接收数据
string strResult = string.Empty;
byte[] buf = new byte[1024];
//byte[] buf = new byte[100];
int iRcv = 0;
while (true)
{
try
{
iRcv = socket.Receive(buf);
}