查看: 1308|回复: 1
打印 上一主题 下一主题

[其他] C# 客户端服务器Socket

[复制链接]

711

主题

10

听众

5805

积分

高级设计师

Rank: 6Rank: 6

纳金币
2954
精华
3

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-7-31 00:49:34 |只看该作者 |倒序浏览
  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. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

0

主题

2

听众

1752

积分

助理设计师

Rank: 4

纳金币
19
精华
0
沙发
发表于 2017-5-5 13:57:10 |只看该作者
水平一般般
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-24 19:30 , Processed in 0.093133 second(s), 26 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部