Skip to content

Instantly share code, notes, and snippets.

@lb5160482
Created January 15, 2018 21:58
Show Gist options
  • Save lb5160482/eb324fe34fce1241ad211a0575c5b99c to your computer and use it in GitHub Desktop.
Save lb5160482/eb324fe34fce1241ad211a0575c5b99c to your computer and use it in GitHub Desktop.
TCP/IP C#
using System.Collections;
using UnityEngine;
using System;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
public class test : MonoBehaviour
{
private Quaternion headRot;
Vector3 headPos;
//public CursorScript cursorScript;
Socket sender;
// buffer for incoming TCP/IP data
byte[] bytes;
System.Net.IPEndPoint remoteEP;
private Thread m_thread = null;
// Use this for initialization
void Awake()
{
m_thread = new Thread(() =>
{
communicate();
}
);
m_thread.Start();
}
void communicate()
{
bytes = new byte[1024];
try
{
//IPHostEntry host = Dns.Resolve("192.168.1.24");
IPHostEntry host = Dns.Resolve("127.0.0.1");
IPAddress ipAddress = host.AddressList[0];
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip;
break;
}
}
remoteEP = new IPEndPoint(ipAddress, 5556);
Debug.Log("ipAddress : " + ipAddress);
Debug.Log("remoteEP : " + remoteEP);
// Create a TCP/IP socket.
sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect to remote endpoint
sender.Connect(remoteEP);
Debug.Log("Socket connected to : " + sender.RemoteEndPoint.ToString());
//Application.targetFrameRate = 100;
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
while (true)
{
byte[] message = Encoding.ASCII.GetBytes("from client\n");
int bytesSent = sender.Send(message);
int bytesRec = sender.Receive(bytes);
string msg = Encoding.ASCII.GetString(bytes, 0, bytesRec);
Debug.Log(msg);
string[] sValues = msg.Split(',');
float[] msgValue = new float[sValues.Length];
for (int i = 0; i < sValues.Length; ++i)
{
msgValue[i] = Convert.ToSingle(sValues[i]);
}
//headPos = new Vector3(msgValue[0], msgValue[1] * -1.0f, msgValue[2]);
headPos = new Vector3(0, 0, 0);
headRot = new Quaternion(msgValue[0], msgValue[1] * -1.0f, msgValue[2] * -1.0f, msgValue[3]);
}
}
void Update()
{
this.transform.position = headPos;
this.transform.rotation = headRot;
}
}
using System.Collections;
using UnityEngine;
using System;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
public class server : MonoBehaviour {
Socket reveiver;
// buffer for incoming TCP/IP data
byte[] bytes;
IPEndPoint localEndPoint;
private Thread m_thread = null;
float height = 0;
// Use this for initialization
void Awake()
{
m_thread = new Thread(() =>
{
communicate();
}
);
m_thread.Start();
}
void communicate()
{
bytes = new byte[1024];
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
foreach (var ip in ipHostInfo.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip;
break;
}
}
localEndPoint = new IPEndPoint(ipAddress, 5566);
Debug.Log("ipAddress : " + ipAddress);
Debug.Log("localEndPoint : " + localEndPoint );
// Create a TCP/IP socket.
reveiver = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try {
reveiver.Bind(localEndPoint );
reveiver.Listen(10);
// Start listening for connections.
while (true) {
Debug.Log("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket handler = reveiver.Accept();
// An incoming connection needs to be processed.
while (true) {
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
string msg = Encoding.ASCII.GetString(bytes, 0, bytesRec);
//Debug.Log(msg);
if (msg == "c") {
Debug.Log(msg);
height += 0.5f;
}
else if (msg == "o") {
Debug.Log(msg);
height -= 0.5f;
}
byte[] sendBack = Encoding.ASCII.GetBytes("from server\n");
handler.Send(sendBack);
}
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
} catch (Exception e) {
Debug.Log(e.ToString());
}
}
// Update is called once per frame
void Update () {
Vector3 tmp = this.transform.position;
tmp.y = height;
this.transform.position = tmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment