Home > other >  Unity using TCP images, but the server can only receive the first picture, don't know what is t
Unity using TCP images, but the server can only receive the first picture, don't know what is t

Time:09-26

Nonsense not much said, directly on the code

Receiver:
 using System. The Collections; 
Using System. Collections. Generic;
Using System. IO;
Using System.Net;
Using System.Net.Sockets;
Using System. The Threading;
Using UnityEngine;

Public class RecivePhoto: MonoBehaviour
{

Private Socket m_socket;
Private IPEndPoint m_ipEp;

Private Thread m_thread;
Private bool isRunningThread=false;

Private Queue
//Use this for initialization
Void the Start ()
{
M_queue=new QueueInitSocketEnv ();
}

//Update is called once per frame
Void the Update ()
{
If (m_queue. Count & gt; 0)
{
The Debug Log (m_queue. Count);

Byte [] temp=m_queue. Dequeue ();

FileStream fs=File. The Create (" D:/HoloLive/01. PNG ");
Fs. Write (temp, 0, temp. Length);
Fs. The Close ();
}


}

Void ReciveMeg ()
{
While (isRunningThread)
{
The Socket Socket=m_socket. The Accept ();

//get photo byte stream length
//byte [] dataSize=new byte [4].
//int the rect=socket. The Receive (dataSize, 0, 4, SocketFlags. None).
//int size=BitConverter. ToInt32 (dataSize, 0).

//the Debug Log (size);

Byte [] buffer=new byte (1024 * 256),
Socket. The Receive (buffer, buffer Length, SocketFlags. None).

//byte [] bufferSize=new byte [4].
//socket. The Receive (bufferSize, 0, 4, SocketFlags. None).
//int size=BitConverter. ToInt32 (bufferSize, 0).
//the Debug Log (size);

M_queue. The Enqueue (buffer);

Socket. The Close ();
}

The Debug Log (" stop ");
}

Void InitSocketEnv ()
{
M_socket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
M_ipEp=new IPEndPoint (IPAddress. Parse (" 127.0.0.1 "), 1200);
M_socket. Bind (m_ipEp);
M_socket. Listen (30);

IsRunningThread=true;
M_thread=new Thread (ReciveMeg);
M_thread. Start ();
}

Void OnDistory ()
{
IsRunningThread=false;
M_socket. Close ();
}
}



Sender:
 using System. The Collections; 
Using System. Collections. Generic;
Using System. IO;
Using System.Net;
Using System.Net.Sockets;
Using UnityEngine;

Public class SendPhoto: MonoBehaviour
{

Private Socket Socket=null;

Private IPEndPoint endPoint=null;

//Use this for initialization
Void the Start ()
{
InitSocketEnv ();
SendMegEvent ();
}


Public void SendMegEvent ()
{
SendPhotoMessage (" 1. PNG ");
}

Void SendPhotoMessage (string fileName)
{
//byte [] buffer=ReadImg (fileName);//null

FileStream fs=new FileStream (" D:/HoloLive/Photo/" + fileName, FileMode. OpenOrCreate, FileAccess. Read);
BinaryReader strread=new BinaryReader (fs);
Byte [] byt=new byte [fs. Length];
Strread. Read (byt, 0, byt. Length - 1);

//byte [] size=new byte [4].
//size=BitConverter. GetBytes (byt. Length);

Socket. The Send (byt);
//socket. Send (size);

Fs. The Close ();
}

Byte [] ReadImg (string fileName)
{
The FileInfo the FileInfo=new the FileInfo (Application. DataPath + "/" + fileName);
Byte [] buffer=new byte [the fileInfo. Length].
Using (FileStream fs=the fileInfo. OpenRead ())
{
Fs. Read (buffer, 0, buffer Length);
}

The return buffer;
}

Void InitSocketEnv ()
{
Socket=new socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
The endPoint=new IPEndPoint (IPAddress. Parse (" 127.0.0.1 "), 1200);
Socket. Connect (the endPoint);
}

Void OnDestory ()
{
Socket. The Close ();
}
}

CodePudding user response:

From the top, don't sink

CodePudding user response:

At first contact network communication this piece, don't know how to solve

CodePudding user response:

Don't c #

See code is probably after you send the first packet of data directly to the TCP connection broken
See you perform this socket. The Close ();

Then the Client side InitSocketEnv () if performed only once so after in the first picture, the server will be TCP connection from the

Unable to send the second image after the client disconnected,


The solution!

1: keep open a TCP connection, communication protocol is used to distinguish between the two images of space position

2: after the disconnected Client to perform initSocketEnv (); The new connection server send the second packet data