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

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


}*/


}
...全文
2081 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
马少华 2017-08-24
  • 打赏
  • 举报
回复
可以接收,一般注意一下字符集保持一致就行了。
zhujinqiang 2017-05-15
  • 打赏
  • 举报
回复
然后,有没有解决呢?

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧