wadewu123 2016-12-09 06:02 采纳率: 0%
浏览 1640
已结题

VC++的Windows Socket与Unity3d C# 之间的通信问题

各位大神,小弟菜鸟一枚,刚刚接触VC和Unity3d现在在做两者之间的通信,想先实现简单的Windows API socket 与 C#的通信,后期再MFC来实现通信,网上看到过相关的帖子但没有合适能用的,代码如下,现阶段感觉可以连接上,unity3d 的后台程序可以显示连接成功,但是在数据发送部分有问题,不清楚Windows API socket send()函数能否发送数据到C#中区,主要是客户端发送Unity3d 上接收,请大神帮忙解决下,万分感激,急急急!!!!!
Vc++客户端:
#include
#include
#include
using namespace std;

void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}

if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);

SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("xxx.22.xxx.86");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(10001);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));

char recvBuf[100];
recv(sockClient,recvBuf,100,0);
printf("%s\n",recvBuf);
send(sockClient,"This is ",strlen("This is ")+1,0);

closesocket(sockClient);
WSACleanup();
}
服务器unity 3d:
using UnityEngine;
using System ;
using System.Collections;
using System .Collections .Generic ;
using System .Threading ;
using System .Net ;
using System .Net .Sockets ;
using System .Text ;

public class Serve : MonoBehaviour {

// Use this for initialization

//private int i = 0;
private Socket Soc_temp;//reve Port
private TcpListener tcpLister;//Listener P_
private bool live = true;
private bool abort_Start = false, abort_Get = false;//abort_Send = false;

public string send_Mes = "send_Mes";
public string get_Mes = "get_Mes";

Thread Thread_Start;
Thread Thread_getData;
//Thread Thread_sendData;

/*public void Server_Start()
{
begin();
}*/
public void begin()
{
abort_Start = true;
IPAddress ip = IPAddress.Parse ("xxx.22.xx.86");

tcpLister = new TcpListener (ip, 10001);
tcpLister.Start();
Thread_Start = new Thread(Running);
Thread_Start.IsBackground = true;
Thread_Start.Start();
Debug.Log("开始监听" );
//Debug.Log("线程开起状态Thread_Start=" + Thread_Start.IsAlive);
}
public void setAbort()
{

if (abort_Start)
{
Thread_Start.Abort();
Debug.Log("开起状态Thread_Start=" + Thread_Start.ThreadState);
Debug.Log("开起状态Thread_Start=" + Thread_Start.IsAlive);
abort_Start = false;
}
if (abort_Get)
{
Thread_getData.Abort();
Debug.Log("接收状态Thread_getData=" + Thread_getData.ThreadState);
Debug.Log("接收状态Thread_getData=" + Thread_getData.IsAlive);
abort_Get = false;
}
/*if (abort_Send)
{

Thread_sendData.Abort();
Debug.Log("发送状态Thread_sendData" + Thread_sendData.ThreadState);
Debug.Log("发送状态Thread_sendData" + Thread_sendData.IsAlive);
abort_Send = false;
}*/

}

void Running()
{
while (live)
{
try
{
if (tcpLister.Pending())//是否有挂起连接的请求
{
abort_Get = true;
Soc_temp = tcpLister.AcceptSocket();
Thread_getData = new Thread(getData);
Thread_getData.IsBackground = true;
Thread_getData.Start();
Debug.Log("连接成功" );
//Debug.Log("线程接收状态Thread_getData=" + Thread_getData.IsAlive);

}
}
catch (Exception e)
{
Soc_temp.Close();
Soc_temp = null;
live = false;
tcpLister.Stop();
Thread_getData.Abort();
Debug.Log ("通信连接有问题"+e);

}
}
}

void getData()
{

while (live)
{ Debug.Log ("开始接收");
try
{
byte[] bytes = new byte[1024];
Soc_temp.Receive(bytes);
Debug.Log(Encoding.UTF8.GetString(bytes));
get_Mes = Encoding.UTF8.GetString(bytes);

}

catch (Exception e)
{
live = false;
Soc_temp.Close();
Soc_temp = null;
Thread_getData.Abort();
Debug.Log ("GET信息有问题"+e);
}
}

}
/* public void SendM()
{
abort_Send = true;
Thread_sendData = new Thread(sendData);
Thread_sendData.IsBackground = true;
Thread_sendData.Start();
Debug.Log("线程发送状态Thread_sendData=" + Thread_sendData.ThreadState);
Debug.Log("线程发送状态Thread_sendData=" + Thread_sendData.IsAlive);
}
void sendData()
{
try
{
i++;
byte[] send_bytes = Encoding.UTF8.GetBytes(send_Mes + i);
Soc_temp.Send(send_bytes);
}

catch (Exception e)
{
live = false;
Soc_temp.Close();
Soc_temp = null;
Thread_getData.Abort();
}

}*/

}
更多 0

  • 写回答

2条回答

  • wadewu123 2016-12-09 07:08
    关注

    各位大神求帮忙啊

    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏