Home > other >  Based on the TCP socket asynchronous communication for the second time send the file to the client b
Based on the TCP socket asynchronous communication for the second time send the file to the client b

Time:09-27

I posted the code, please everyone a great god help me to see where the problem is,
 private void OnStartServer () 
{
Try
{
//port
IPEndPoint endPoint=new IPEndPoint (IPAddress. Any, 50223);
//listen
SocketWatch=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
SocketWatch. Bind (endPoint);
//one-time can listen up to 500 client
SocketWatch. Listen (500);
Console. WriteLine (" the service side to start listening... ");
//create a socket for and client communication the first parameter to delegate type
SocketWatch. BeginAccept (new AsyncCallback (AcceptInfo), socketWatch);
}
The catch (Exception ex)
{
Console. WriteLine (ex);
}
}
//close service
Public void OnStop ()
{
Try
{
SocketWatch. Shutdown (SocketShutdown. Both);
SocketWatch. Close ();
}
The catch (Exception ex)
{
Console. WriteLine (ex);
}
}
///& lt; Summary>
///waiting for the client connection, and create the communication with the Socket
///& lt;/summary>
///& lt; Param name="aResult" & gt;
Private void AcceptInfo (IAsyncResult aResult)
{
The Socket Socket=aResult. AsyncState as Socket;
//create the communicate with the client socket
The Socket client=Socket. EndAccept (aResult);
SocketSend=client;
//get the remote host endpoints (IP + port)
String clientIp=((System.Net.IPEndPoint) client. RemoteEndPoint). Address. The ToString ();
String clientPort=((System.Net.IPEndPoint) client. RemoteEndPoint) Port. The ToString ();
Console. WriteLine (" remote host: "+" : "+ clientPort + + clientIp" connection success ");
Try
{
//receiving the client message
Client. BeginReceive (buffer, 0, buffer Length, SocketFlags. None, new AsyncCallback (ReceiveAndSendMessage), the client);
}
The catch (Exception ex)
{
Console. WriteLine (ex);
}
//ready for the next client request
Socket. BeginAccept (new AsyncCallback (AcceptInfo), socket);
}

Private void ReceiveAndSendMessage (IAsyncResult aResult)
{
//receiving messages from the client
The Socket Socket=aResult. AsyncState as Socket;
Try
{
//returns the length of the received data
Int strLength=0;

StrLength=socket. EndReceive (aResult);

If (strLength==0)
{
Socket. Shutdown (SocketShutdown. Both);
Socket. The Close ();
return;
}
//to convert byte array into a string
. String strText=Encoding UTF8. Get string (buffer, 0, strLength);
Console. WriteLine (" receives the string: "+ strText);
//the server to the client to send images, here thought it would be under the optimization (in the case of not open thread under test)
If (strText=="2")
{
//Thread sendThread=new Thread (SendFileToClient);
//sendThread IsBackground=true;
//sendThread. Start (socket);
SendFileToClient (socket);
}
}
The catch (Exception ex)
{
Console. WriteLine (ex);
}
Try
{
//receiving the client message
Socket. BeginReceive (buffer, 0, buffer Length, SocketFlags. None, new AsyncCallback (ReceiveAndSendMessage), socket);
}
Catch
{
}
}
///& lt; Summary>
///send the image to the client
///& lt;/summary>
///& lt; Param name="obj" & gt;
Private void SendFileToClient (object obj)
{
The Socket Socket=obj as Socket;
If (socket. Connected)
{
Socket. BeginSend (buffer, 0, buffer Length, SocketFlags. None, new AsyncCallback (SendFile), socket);
}
The else
{
Socket. Shutdown (SocketShutdown. Both);
Socket. The Close ();
}
}
///& lt; Summary>
///send
///& lt;/summary>
///& lt; Param name="aResult" & gt;
Private void SendFile (IAsyncResult aResult)
{
The Socket Socket=aResult. AsyncState as Socket;
//send the path of the image file
//string filePath=@ "D: \ imagesTest";
String filePath=@ "D: \ image";
List The fileList=new List (a);
DirectoryInfo DirectoryInfo=new DirectoryInfo (filePath);
FileStream fs=null;
Using (NetworkStream ns=new NetworkStream (socket, FileAccess ReadWrite, false))
{

Foreach (the FileInfo fileName in directoryInfo. GetFiles ())
{
Try
{
String imagePath=null;
ImagePath + "\ " + fileName.=filePath ToString ();
Fs=new FileStream (imagePath as string, FileMode OpenOrCreate, FileAccess. Read, FileShare. ReadWrite);
//ns=new NetworkStream (socket, FileAccess ReadWrite, false);
if (! File. The Exists (imagePath. ToString ()))
return;
Byte [] fileBytes=null;
Int64 fileTotalLen=fs. Length + 132;
If (fileTotalLen & lt;=maxBufferSize)//small files to send
{
FileBytes=new byte [fileTotalLen];
Array. Copy (BitConverter. GetBytes (fileBytes. Length - 132), fileBytes, 4);
Byte [] fileNameBytes=System. Text. Encoding. The Default. The GetBytes (fileName. The ToString ());
FileNameBytes. CopyTo (fileBytes, 4);

Using (fs)
{
//the image byte information into byte array
Fs. Read (fileBytes, 132, fileBytes. Length - 132);
Fs. The Close ();
}
IAsyncResult result=ns. BeginWrite (fileBytes, 0, fileBytes Length, null, null);
Ns. EndWrite (result);
}

Ns. Flush ();

}
Ns. Flush ();
}

}
The catch (Exception ex)
{


Console. WriteLine (ex);
}
nullnullnullnullnullnullnullnullnullnullnullnull
  • Related