纳金网

标题: C# 客户端服务器Socket [打印本页]

作者: 刀锋狼    时间: 2015-7-31 00:49
标题: C# 客户端服务器Socket
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Net.Sockets;
  4. using System.Net;
  5. using System.Threading;
  6. using System.Text;

  7. public class ServerSocket {

  8. public string ipStr;
  9. public int port;
  10. public int maxConnections;
  11. public bool IsRun {
  12. get {
  13. return isRun;
  14. }
  15. }

  16. private Socket serverSocket;
  17. private Thread serverThread;
  18. private bool isRun;
  19. private Socket temp;

  20. public ServerSocket (string ipStr, int port = 2333, int maxConnections = 50) {
  21. isRun = false;
  22. this.ipStr = ipStr;
  23. this.port = port;
  24. this.maxConnections = maxConnections;
  25. serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  26. serverSocket.Bind (new IPEndPoint (IPAddress.Parse (ipStr), port));
  27. serverSocket.Listen (maxConnections);
  28. serverThread = new Thread (new ThreadStart (AsyncServer));
  29. }

  30. public void StartServer () {
  31. isRun = true;
  32. serverThread.Start ();
  33. }

  34. public void StopServer () {
  35. isRun = false;
  36. serverSocket.Close ();
  37. if (serverThread.IsAlive) {
  38. serverThread.Abort ();
  39. }
  40. }

  41. private ServerSocket () {

  42. }

  43. private void AsyncServer () {
  44. while (isRun) {
  45. Socket userSocket = serverSocket.Accept ();
  46. temp = userSocket;
  47. Thread tt = new Thread (new ThreadStart (XXX));
  48. tt.Start ();
  49. }
  50. }

  51. private void XXX () {
  52. Socket user = temp;
  53. while (true) {
  54. byte [] receivedData = new byte [1024];
  55. user.Receive (receivedData);
  56. string m = Encoding.UTF8.GetString (receivedData);
  57. Debug.Log (m);
  58. if (m != "") {
  59. byte [] s = Encoding.UTF8.GetBytes (m);
  60. user.Send (s);
  61. }
  62. }
  63. }
  64. }
复制代码
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Net.Sockets;
  4. using System.Net;
  5. using System.Threading;
  6. using System.Text;
  7. using UnityEngine.UI;

  8. public class Client : MonoBehaviour {

  9. public string ipStr;
  10. public int port;
  11. public InputField t;
  12. public Text text;

  13. private string str = "";

  14. Socket s;

  15. void Start () {
  16. s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  17. s.Connect (new IPEndPoint (IPAddress.Parse (ipStr), port));
  18. Thread tt = new Thread (new ThreadStart (DDD));
  19. tt.Start ();
  20. }

  21. private void DDD () {
  22. while (true) {
  23. byte [] r = new byte [1024];
  24. s.Receive (r);
  25. str = Encoding.UTF8.GetString (r);
  26. }
  27. }

  28. void Update () {
  29. text.text = str;
  30. if (Input.GetKeyDown (KeyCode.S)) {
  31. byte [] sendbuf = Encoding.UTF8.GetBytes (t.text);
  32. s.Send (sendbuf);
  33. }
  34. }
  35. }
复制代码

作者: GPSGIS3D    时间: 2017-5-5 13:57
水平一般般




欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5