Dear Friends at Epic,
I have succeed in sending data from python to UE4, and posted that code for the community:
A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums,Receive_Binary_Data_From_an_IP/Port_Into_UE4,(Full_Code_Sample)
#Sending Data to Python From UE4
I’ve tried sending data from UE4 to Python, using UDP DGram and TCP Stream
When I use DGram, python gets the message but looks like garbage, and UE4 says 16 bytes were sent (size of FString)
When I use TCP NAME_Stream, UE4 always says that -1 bytes were sent, and python does not get the message.
I am using SOCK_STREAM on the python end of things.
Here is my python TCP code:
from socket import *
import thread
BUFF = 1024
HOST = '127.0.0.1'
PORT = 8890
def response(key):
return 'Recieved'
def handler(clientsock,addr):
while 1:
data = clientsock.recv(BUFF)
print 'data:' + repr(data)
if not data: break
if __name__=='__main__':
ADDR = (HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serversock.bind(ADDR)
serversock.listen(5)
while 1:
print 'waiting for connection...'
clientsock, addr = serversock.accept()
print '...connected from:', addr
thread.start_new_thread(handler, (clientsock, addr))
#UE4 Code
Here is my UE4 code where I have the option built in to use UDP or TCP
As I mentioned, TCP version is not working and I have no idea why!
Any ideas?
The socket is valid, but it is always sending -1 bytes!
#Thanks!
Thanks!
Rama
bool ARamaTCPSender::StartTCPSender(
const FString& YourChosenSocketName,
const FString& TheIP,
const int32 ThePort,
bool UDP
){
IsUDP = UDP;
//Create Remote Address.
RemoteAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
bool bIsValid;
RemoteAddr->SetIp(*TheIP, bIsValid);
RemoteAddr->SetPort(ThePort);
if(!bIsValid)
{
ScreenMsg("Rama TCP Sender>> IP address was not valid!", TheIP);
return false;
}
//Create Socket
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// NAME_Stream
// Creates a stream (TCP) socket
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(UDP)
{
SenderSocket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket( NAME_DGram, TEXT( "RamaTCPSender" ) );
}
else
{
SenderSocket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket( NAME_Stream, TEXT( "RamaTCPSender" ) );
}
//Set Send Buffer Size
int32 SendSize = 2*1024*1024;
SenderSocket->SetSendBufferSize(SendSize,SendSize);
SenderSocket->SetReceiveBufferSize(SendSize, SendSize);
if(UDP)
{
UE_LOG(Victory,Log,TEXT("\n\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"));
UE_LOG(Victory,Log,TEXT("Rama ****UDP**** Sender Initialized Successfully!!!"));
UE_LOG(Victory,Log,TEXT("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n"));
}
else
{
UE_LOG(Victory,Log,TEXT("\n\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"));
UE_LOG(Victory,Log,TEXT("Rama TCP Sender Initialized Successfully!!!"));
UE_LOG(Victory,Log,TEXT("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n"));
}
return true;
}
bool ARamaTCPSender::RamaTCPSender_SendString(FString ToSend)
{
if(!SenderSocket) return false;
//~~~~~~~~~~~~~~~~
//Add the Line delimiter
ToSend = TCPSender::LineSeparator + ToSend;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~
int32 BytesSent = 0;
//SEND TO
SenderSocket->SendTo((uint8*)&ToSend, sizeof(ToSend), BytesSent, *RemoteAddr);
if(BytesSent <= 0)
{
const FString Str = "ARamaTCPSender::RamaTCPSender_SendString>>> Socket is valid but the receiver received 0 bytes, make sure it is listening properly!";
UE_LOG(Victory,Error,TEXT("%s %s"),*Str, *ToSend);
ScreenMsg(Str);
}
if(IsUDP)
{
ScreenMsg("UDP~ Send Succcess! Bytes Sent = ",BytesSent );
}
else
{
ScreenMsg("TCP~ Send Succcess! Bytes Sent = ",BytesSent );
}
return true;
}