2024年6月1日发(作者:)
1.简单服务器端
/*
using ;
using s;
using ;
using ing;
private static int port = %%2;
private static Thread thThreadRead;
private static TcpListener TcpListen;
private static bool bListener = true;
private static Socket stRead;
*/
private static void Listen()
{
try
{
TcpListen = new TcpListener(port);
();
stRead = Socket();
EndPoint tempRemoteEP = EndPoint;
IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP;
IPHostEntry host = tByAddress(s);
string sHostName = me;
while (bListener)
{
(es(%%1));
string sTime = tTimeString();
Byte[] byRead = new Byte[1024];
int iRead = eFrom(byRead, ref tempRemoteEP);
Byte[] byText = new Byte[iRead];
(byRead, 0, byText, 0, iRead);
string line = ing(byRead);
}
}
catch (tyException)
{
//监听失败
}
}
thThreadRead = new Thread(new ThreadStart(Listen));
();
2.简单客户端
/*
using ;
using s;
using ;
private static IPEndPoint dateTimeHost;
*/
string hostIPString=%%1;
string hostPortString=%%2;
IPAddress hostIP=(hostIPString);
dateTimeHost=new IPEndPoint(hostIP,(hostPortString));
Socket conn=new Socket(etwork,,);
t(dateTimeHost);
int bytes=0;
file:///C|/Documents and Settings/ITboy/桌面/新建文件夹 (2)/C_Socket网络编程.txt[2010-4-23 0:33:53]
Byte[] RecvBytes=new Byte[256];
bytes=e(RecvBytes,,0);
string RecvString=ing(RecvBytes,0,bytes);
ine(RecvString);
wn();
();
3.获得本机IP
//using ;
IPAddress[] addressList = tByName(tName()).AddressList;
string %%1=null;
for (int i = 0; i < ; i++)
{
%%1 += addressList[i].ToString();
}
4.端对端通信
/*
using ;
using s;
*/
UdpClient client=new UdpClient(%%2);
IPAddress a=("127001");
IPEndPoint receivePoint=new IPEndPoint(a,%%2);
IPAddress HostIP=null;
byte[] sendData=es(%%3);
byte[] recData;
try{
HostIP=(%%1);
}
catch {
recData=e(ref receivePoint);
%%3=ing(recData);
(sendData,,%%4,%%2);
();
}
IPEndPoint host=new IPEndPoint(HostIP,%%2);
recData=e(ref receivePoint);
%%3=ing(recData);
();
5.点对点通信
/*
using ;
using s;
using ;
using ing;
*/
Thread th;
TcpListener tpListen1;
bool listenerRun=true;
NetworkStream tcpStream;
StreamWriter reqStreamW;
TcpClient tcpc;
Socket skSocket;
protected void Listen()
{
file:///C|/Documents and Settings/ITboy/桌面/新建文件夹 (2)/C_Socket网络编程.txt[2010-4-23 0:33:53]
发布者:admin,转转请注明出处:http://www.yc00.com/web/1717178013a2734885.html
评论列表(0条)