日期:2014-05-16 浏览次数:20747 次
private void runServer ()
{
serverYLC = new Socket ( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
IPEndPoint localEP = new IPEndPoint ( ip, portYLC );
serverYLC.Bind ( localEP );
while ( true )
{
serverYLC.Listen ( 65535 );
Socket client = serverYLC.Accept ();
IPEndPoint remoEP = ( IPEndPoint ) client.RemoteEndPoint;
//记录用户ip列表
ClientIPYLC.Add ( remoEP.Address.ToString (), client );
BeginInvoke ( ( ThreadStart ) delegate
{
lYLC.Items.Add ( remoEP.Address.ToString () );
} );
Thread clientThread = new Thread ( new ParameterizedThreadStart ( clientFun ) );
clientThread.IsBackground = true;
clientThread.Start ( client );
}
}
private void clientFun ( object obj )
{
Socket client = ( Socket ) obj;
IPEndPoint remoEP = ( IPEndPoint ) client.RemoteEndPoint;
byte[] buffer;
int re;
string str;
try
{
while ( client.Connected )
{
buffer = new byte[100];
re = client.Receive ( buffer );
if ( re == 0 )//无数据
{
continue;
}
str = Encoding.UTF8.GetString ( buffer );
string fk = str;
FileStream fs = new FileStream ( @"d:test.txt", FileMode.Append );
StreamWriter sw = new StreamWriter ( fs );
//开始写入
sw.WriteLine ( (fk.Trim() + "---" + DateTime.Now.ToString ( "HH:mm:ss.fff" )).Replace(" ","") );
//清空缓冲区
sw.Flush ();
//关闭流
sw.Close ();
fs.Close ();
sw.Dispose ();
fs.Dispose ();
BeginInvoke ( ( ThreadStart ) delegate
&nb