123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- class ClientSocket
- {
-
- private byte[] buffers;
- private Socket clientSocket;
- private int port = 8081;
- private string host = "127.0.0.1";
-
- public ClientSocket()
- {
- this.buffers = new byte[1100];
- }
- private Socket clientConnect(int port, string host)
- {
- IPAddress ip = IPAddress.Parse(host);
- IPEndPoint ipe = new IPEndPoint(ip, port);
- Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- c.NoDelay = true;
- Console.WriteLine("Conneting...");
- try
- {
- c.Connect(ipe);
- return c;
- }
- catch (Exception e)
- {
- throw e;
-
-
-
- }
- return null;
- }
- public void Receive()
- {
- try
- {
- clientSocket = clientConnect(port, host);
-
- while (true)
- {
-
- int length = 1;
- int packageNumber = 0;
- int row = 0;
- while (length > 0)
- {
- clientSocket.Receive(buffers);
- }
- }
- }
- catch (Exception e)
- {
-
- }
- finally
- {
- clientSocket.Close();
- }
- }
- public string byteToHexStr(byte[] bytes)
- {
- string returnStr = "";
- if (bytes != null)
- {
- for (int i = 0; i < bytes.Length; i++)
- {
- returnStr += bytes[i].ToString("X2");
- }
- }
- return returnStr;
- }
- }
|