.net Socket服务器端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TCPServer
{
    
class Program
    {
        
static void Main(string[] args)
        {
            LL();
        }

        
private static void LL()
        {
            Thread thr 
= new Thread(ServerListen.Run);
            thr.Start();
        }
    }
}


 using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TCPServer
{
    
public class ServerListen
    {
        
public static void Run()
        {
            IPEndPoint serverIP 
= new IPEndPoint(IPAddress.Any, 1234);//本机预使用的IP和端口
            Socket skServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            skServer.Bind(serverIP);
            skServer.Listen(
100);

            
while (true)
            {
                Socket skClient;
                
try
                {
                    
//当有可用的客户端连接尝试时执行,并返回一个新的socket,用于与客户端之间的通信
                    skClient = skServer.Accept();
                }
                
catch (Exception ex)
                {
                    LogBLL.Err(
"接收用户连接失败 ServerListen.Run Socket.Accept", ex);
                    
continue;
                }

                ThrClient thrC 
= new ThrClient(skServer, skClient);
                Thread t 
= new Thread(thrC.Run);
                t.Start();
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using W.Common;
using System.Threading;

namespace TCPServer
{
    
public class ThrClient
    {
        
private static readonly StringBuilder sb = new StringBuilder();
        
private Socket skServer;
        
private Socket skClient;
        
private IPEndPoint clientIP;

        
//每次接收发送的临时信息
        private byte[] sendData;//发送的信息
        private byte[] receiveData = new byte[128];//接收信息
        private int receiveN;
        
//--------

        
//---------
        private int netID;

        
public ThrClient(Socket pSkServer, Socket pSkClient)
        {
            
this.skServer = pSkServer;
            
this.skClient = pSkClient;

            
this.clientIP = (IPEndPoint)this.skClient.RemoteEndPoint;
            Console.WriteLine(
"IP:" + clientIP.Address + ":" + clientIP.Port);
            
this.sendData = Encoding.UTF8.GetBytes("success");
            
try
            {
                
this.skClient.Send(sendData, sendData.Length, SocketFlags.None);//发送信息
            }
            
catch (Exception ex)
            {
                LogBLL.Err(
"发送第一次连接成功信息 ThrClient.ThrClient Socket.Send", ex);
            }
        }

        
public void Run()
        {
            
while (true)
            {
                
try
                {
                    
this.receiveN = skClient.Receive(this.receiveData);//接收
                    if (this.receiveN == 0)
                    {
                        
break;
                    }
                }
                
catch (Exception ex)
                {
                    LogBLL.Err(
"接收 ThrClient.Run Receive", ex);
                    
break;
                }

                
this.sendData = this.GetData();

                
try
                {
                    skClient.Send(
this.sendData);//发送
                }
                
catch (Exception ex)
                {
                    LogBLL.Err(
"发送 ThrClient.Run Send", ex);
                    
break;
                }
            }
        }

        
private byte[] GetData()
        {
            
string s = Encoding.UTF8.GetString(receiveData, 0this.receiveN);
            
string[] s1 = s.Split(':');
            
string sendStr = "null";
            
if (s1.Length >= 2)
            {
                
switch (s1[0])
                {
                    
case "netid":
                        
this.netID = s1[1].ToInt();
                        sendStr 
= "1";
                        
break;

                    
default:
                        sendStr 
= this.netID + " " + s;
                        
break;
                }
            }
            
else
            {
                sendStr 
= s;//需要删除
            }

            
if (s.Split('-').Length > 2)
            {
                
//若执行到这里,说明并发有问题
                Console.Write(sendStr + " | ");
            }

            Console.Write(sendStr 
+ " | ");//需要删除
            return Encoding.UTF8.GetBytes(sendStr + " | ");
        }
    }

} 

 using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TCPServer
{
    
public class LogBLL
    {
        
public static readonly object objLock = new object();
        
public static void Err(string msg, Exception ex)
        {
            
string s = "err:" + msg + ":" + ex.Message;
            Console.WriteLine(
"err:" + msg + ":" + ex.Message);

            
string filename = AppDomain.CurrentDomain.BaseDirectory + "log/" + DateTime.Now.ToString("yyyy-MM-dd HH"+ ".txt";
            
lock (objLock)
            {
                
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, true))
                {
                    sw.WriteLine(s);
                    sw.WriteLine(ex.StackTrace);
                    sw.WriteLine(
"time:" + DateTime.Now.ToString());
                    sw.WriteLine(
"----------------------------------");
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/suger/p/2131864.html