Experience after the Socket programming project

xiaoxiao2021-03-06  41

Our project is a Socket connection with DOTNET, multi-threaded process multiple users. I will talk about the problems encountered during the implementation process. Below is a simplified implementation of my server. private Socket clientSocket; public void Start () {Thread thread = new Thread (new ThreadStart (MainService)); thread.Start ();} void MainService () {listener = new TcpListener (111111); listener.Start (); while (TRUE) {socket s = listener.acceptsocket (); // Frame listening to customer connection clientsocket = s; thread clientService = new thread (New ThreadStart (ClientService)); clientService.Start (); thread.sleep (200); / / 1, waiting for the value to pass a variable thread, to prevent conflict caused by multiuser}} void ClientService () {string command; string receiveStr; string fileName, fileSize; Socket sock = clientSocket; EndPoint oldEP = sock.RemoteEndPoint; byte [] BB = new byte [200]; while (true) {ii = sock.receiveFrom (bb, ref olDep); // Receive data IF (ii == 0) // 2, the client connection is disconnected, will not stop Receive 0 bytes {Break;} receiveStr = Encoding.default.getstring (BB, 0, Bb.Length); Command = ReadlineFrom (ReceiveStr, 1) .tolowe R (); // readLineFrom reads a line of rows in the string {CACE "UPLOAD": filename = readlineFrom (ReceiveStr, 2); filesis = readlineFrom (ReceiveStr, 3); UploadFile (Sock, FileName, Int32 .Parse (filesize)); // accept data method, Break; ....................}}

转载请注明原文地址:https://www.9cbs.com/read-76024.html

New Post(0)