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

各位大神,小弟菜鸟一枚,刚刚接触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

0

2个回答

各位大神求帮忙啊

0

现在客户端有什么问题哪???把具体的错误和问题列出来。。。

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!