设为首页 加入收藏

TOP

【C#】 Socket通讯客户端程序(二)
2014-11-24 14:34:32 来源: 作者: 【 】 浏览:2
Tags:Socket 通讯 客户端 程序
e );
}

}
public class SocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1024];
}

public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState ;
int iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
richTextRxMessage.Text = richTextRxMessage.Text + szData;
WaitForData();
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
private void UpdateControls( bool connected )
{
buttonConnect.Enabled = !connected;
buttonDisconnect.Enabled = connected;
string connectStatus = connected "Connected" : "Not Connected";
textBoxConnectStatus.Text = connectStatus;
}
void ButtonDisconnectClick(object sender, System.EventArgs e)
{
if ( m_clientSocket != null )
{
m_clientSocket.Close();
m_clientSocket = null;
UpdateControls(false);
}
}
//----------------------------------------------------
// This is a helper function used (for convenience) to
// get the IP address of the local machine
//----------------------------------------------------
String GetIP()
{
String strHostName = Dns.GetHostName();

// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

// Grab the first IP addresses
String IPStr = "";
foreach(IPAddress ipaddress in iphostentry.AddressList){
IPStr = ipaddress.ToString();
return IPStr;
}
return IPStr;
}


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C# 邮件发送方法【NetMail方式】 下一篇AMF序列化为对象和AMF序列化为二..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: