TCP/IP data transfer freezes Unity while transferring the data - c#

I am working on a unity project where user can send data from client to server using TCP/IP. I am able to send the data. But my unity stalls or freeze until the data got read from the networkstream.
Server Code:Read Data.
public void StartServer (){
// Start TcpServer background thread
tcpListenerThread = new Thread (new ThreadStart(ListenForIncomingRequests));
tcpListenerThread.IsBackground = true;
tcpListenerThread.Start();
startServerButton.interactable = false;
path = UnityEditor.EditorUtility.SaveFolderPanel ("Any type of File.", "", ""); // Path.Combine(path,"D:\\Siva\\TCPDownloadfiles");
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
SendMessage();
}
}
/// <summary>
/// Runs in background TcpServerThread; Handles incoming TcpClient requests
/// </summary>
private void ListenForIncomingRequests () {
Thread threadForDownloading = new Thread (new ThreadStart(DownloadingStreamData));
threadForDownloading.IsBackground = true;
threadForDownloading.Start();
//DownloadingStreamData ();
}
private void DownloadingStreamData()
{
try {
fileCount++;
int fileNameLen;
byte[] incomingData = new byte[1024];
string ext = "";
// Create listener on localhost port 8052.
tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8052);
tcpListener.Start();
Debug.Log("Server is listening");
bytes = new Byte[Int32.MaxValue]; // 2221024
while (true) {
using (connectedTcpClient = tcpListener.AcceptTcpClient()) {
// Get a stream object for reading
using (stream = connectedTcpClient.GetStream()) {
Debug.Log("Befor Stream Read..........");
IAsyncResult result = stream.BeginRead(bytes, 0, bytes.Length, MessageReceived, stream);
result.AsyncWaitHandle.WaitOne();
// Read incoming stream into byte arrary.
do{
//length = stream.Read(bytes, 0, bytes.Length);
//length = s_bytesReceived;
Debug.Log("After Stream Read.........."+length);
incomingData = new byte[length];
Array.Copy(bytes, 0, incomingData, 0, length - 4);
// Convert byte array to string message.
fileNameLen = BitConverter.ToInt32(incomingData, 0);
string clientMessage = Encoding.ASCII.GetString(incomingData,4, fileNameLen);
ext = Path.GetExtension(clientMessage);
//Debug.Log("client message received as: " + clientMessage);
//File.WriteAllBytes(path + "\\temp"+fileCount.ToString()+ext, incomingData);
BinaryWriter bWrite = new BinaryWriter(File.Open(path + "\\temp"+fileCount.ToString()+ext, FileMode.Create)); ;
bWrite.Write(incomingData, 4 + fileNameLen, length - 4 - fileNameLen);
bWrite.Flush();
bWrite.Close();
} while(stream.DataAvailable);
//Debug.Log("Bytes length2 :>>>"+" "+fileNameLen +" Incoming data length" +incomingData.Length);
//connectedTcpClient.Close();
//stream.Flush();
//stream.Close();
//Debug.Log("GETTING FILE NAME THROUGH BUFFER ::"+stream);
}
}
}
}
catch (SocketException socketException) {
Debug.Log("SocketException " + socketException.ToString());
}
}
I tried using stream.BeginRead method but that is throwing exception like Stream/socket was disposed already and trying to access it on client code.
But stream.Read method works as like i expect but it stalls/freeze the unity until it get the data.Please give me some better solution for this issue. Thanks in advance.
Client Code: Sends Data
if (stream.CanWrite) {
string path = UnityEditor.EditorUtility.OpenFilePanel ("Any type of File.", "", "");
WWW www = new WWW ("file:///" + path);
byte[] fileNameBytes = Encoding.ASCII.GetBytes(path);
byte[] fileDataBytes = www.bytes;
byte[] fileNameLength = BitConverter.GetBytes(fileNameBytes.Length);
clientData = new byte[4 + fileNameBytes.Length + fileDataBytes.Length];
fileNameLength.CopyTo(clientData, 0);
fileNameBytes.CopyTo(clientData, 4);
fileDataBytes.CopyTo(clientData, 4 + fileNameBytes.Length);
Debug.Log ("FIle sends to server path : "+path);
//stream.BeginRead(clientData, 0, clientData.Length, MessageReceived, stream);
try {
stream.Write(clientData, 0, clientData.Length);
//stream.BeginWrite(clientData, 0, clientData.Length, MessageReceived, stream);
}catch (Exception e) {
Debug.Log("SocketException " + e.ToString());
}

Related

How do I send and receive a file through tcp socket

Now I know that this question has been asked a lot but I really just don't get how to do it. I tried this but the file don't get complete I just receive just a bit of the file and the rest is just NULL here is my code in client part I first send a message to the server that contain the file size like this :
// here I send the a upload request with the size of the file that I want to send
byte[] data = Encoding.Unicode.GetBytes("uploadreq~"+new FileInfo(ofg.FileName).Length);
// here is the socket client
target.Send(data);
Then on the server side :
if (cmd.Contains(update.update_request))
{
// here I set an int var to the file size
update.update_size = int.Parse(cmd.Split('~')[1]);
// here I setup the a new byte array with the given file size
update.update_received = new byte[update.update_size];
// then I send a upload confirm command
Connection.sendCommand(Commands.update_confirme);
update.isupdate = true;
}
Again on the client side when the confirmation has been received :
if (cmd.StartsWith("updateConfirm"))
{
// reading all the bytes of the file and sending them
byte[] datatosend = File.ReadAllBytes("the file path");
Connection.send_bytes(datatosend);
}
Finally on the client side :
private void receiveInfo()
{
byte[] buffer = new byte[999999];
int received = 0;
try
{
received = Connection.clientSocket.Receive(buffer);
}
catch (SocketException)
{
Connection.clientSocket.Close();
Connection.clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Connection.makeConnection();
}
if (received == 0)
return;
byte[] data = new byte[received];
Array.Copy(buffer, data, received);
if (update.isupdate == true)
{
// this calls a method that process the data received
update.process_update(data);
}
}
public static void process_update(byte[] data)
{
int writeSize = 0;
Buffer.BlockCopy(data, 0, update_received, writeSize, data.Length);
writeSize += data.Length;
if (update_received.Length == update_size)
{
using (FileStream fs = File.Create("the path to where the file shloud go"))
{
byte[] info = update_received;
fs.Write(info, 0, info.Length);
}
Array.Clear(update_received, 0, update_received.Length);
isupdate = false;
}
}
As I was writing this question I changed the buffer size in the receive info method and that seems to change stuff a bit but still, the file won't arrive fully..
Try this for the client:
private void SendFile(String FileName,String IPAddress,int Port )
{
System.Net.Sockets.TcpClient TcpClient = new System.Net.Sockets.TcpClient(IPAddress, Port);
System.Net.Sockets.NetworkStream NetworkStream = TcpClient.GetStream();
System.IO.Stream FileStream = System.IO.File.OpenRead(FileName);
byte[] FileBuffer = new byte[FileStream.Length];
FileStream.Read(FileBuffer, 0, (int)FileStream.Length);
NetworkStream.Write(FileBuffer, 0, FileBuffer.GetLength(0));
NetworkStream.Close();
}
and this is the code for the server:
private void ReceiveFile(String FilePath, int Port)
{
System.Threading.Thread WorkerThread = new System.Threading.Thread(() =>
{
System.Net.Sockets.TcpListener TcpListener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 60000);
TcpListener.Start();
System.Net.Sockets.Socket HandlerSocket = TcpListener.AcceptSocket();
System.Net.Sockets.NetworkStream NetworkStream = new System.Net.Sockets.NetworkStream(HandlerSocket);
int BlockSize = 1024;
int DataRead = 0;
Byte[] DataByte = new Byte[BlockSize];
lock (this)
{
System.IO.Stream FileStream = System.IO.File.OpenWrite(FilePath);
while (true)
{
DataRead = NetworkStream.Read(DataByte, 0, BlockSize);
FileStream.Write(DataByte, 0, DataRead);
if (DataRead == 0)
{
break;
}
}
FileStream.Close();
}
});
WorkerThread.Start();
}
This will only transfer one file.

tcp/ip recieve bytes and convert to texture

I have a small problem with this. I have another program / software which converts image to bytes and sends them on. So what I need to do now is catch those bytes in Unity and convert them back to image and set that as a texture.
I've already established the connection to the other software via TCP/IP system, connection is working, other software is sending data, but I've got no idea how to convert those bytes to img.
Debug.Log("client message received as: " + clientMessage);
is just a test so I can see that data is coming through.
Here is my code
img.LoadRawTextureData(Loader);
img.Apply();
GameObject.Find("Plane").GetComponent<Renderer>().material.mainTexture = img;
//
private void ListenForIncommingRequests()
{
try
{
tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 35800);
tcpListener.Start();
Debug.Log("Server is listening");
Byte[] bytes = new Byte[1024];
while (true)
{
using (connectedTcpClient = tcpListener.AcceptTcpClient())
{
using (NetworkStream stream = connectedTcpClient.GetStream())
{
int length;
while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
{
var incommingData = new byte[length];
Array.Copy(bytes, 0, incommingData, 0, length);
Loader = incommingData;
string clientMessage = Encoding.ASCII.GetString(incommingData);
Debug.Log("client message received as: " + clientMessage);
}
}
}
}
}
catch (SocketException socketException)
{
Debug.Log("SocketException " + socketException.ToString());
}
}
Have you tried passing the bytes into LoadRawTextureData?
private byte[] ListenForIncommingRequests()
{
try
{
tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 35800);
tcpListener.Start();
Debug.Log("Server is listening");
Byte[] bytes = new Byte[1024];
while (true)
{
using (connectedTcpClient = tcpListener.AcceptTcpClient())
{
using (NetworkStream stream = connectedTcpClient.GetStream())
{
int length;
while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
{
var incommingData = new byte[length];
Array.Copy(bytes, 0, incommingData, 0, length);
Loader = incommingData;
string clientMessage = Encoding.ASCII.GetString(incommingData);
Debug.Log("client message received as: " + clientMessage);
}
}
}
}
}
catch (SocketException socketException)
{
Debug.Log("SocketException " + socketException.ToString());
}
return bytes;
}
And call it like this.
var result = ListenForIncommingRequests();
img.LoadRawTextureData(result);
img.Apply();
GameObject.Find("Plane").GetComponent<Renderer>().material.mainTexture = img;

c# Socket Randomly Stops Receiving

This question may be very hard to answer or perhaps somebody knows a common reason.
I have 1 server, 5 clients;
Socket client
Socket listener
Every 5000 MS the client checks for a file on the server (Which is Windows 7)via socket, if found the client received the file via socket.
I understand only Windows Server can process more than two asynch file transfers at a time.
My issue is that, randomly the client begins to receive the file. All clients generally work fine. Once in a while, a client will stop receiving the file partthrough and not complete the file transfer and my client program just hangs and never finishes.
I can NOT reproduce this on development environment. I have tested it with half-gig files, 100's of sends and I never get an error.
My question is , is it possible that there is some protocol on Windows 7 that is stopping my file send from the server.
My Receive call back for the client is quite lengthy, but here it is. I do not believe the problem to be within the code.
Any help would be greatly appreciated. My boss is about to have my neck :(
private static void ReceiveCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
state.totalBytesRead += bytesRead;
if (bytesRead > 0)
{
if (state.flag == 0)
{
if (state.totalBytesRead >= 8)
{
// we know we put the msgLen / prefixLen as the first 8 bytes on the stream
state.msgLen = BitConverter.ToInt32(state.buffer, 0);
state.prefixLen = BitConverter.ToInt32(state.buffer, 4);
state.flag = 1;
// good to process the first 2 integer values on the stream
//state.sb.Append(Encoding.ASCII.GetString(state.buffer, 8, bytesRead));
int prefixRequestBytes = state.prefixLen;
if (prefixRequestBytes > StateObject.BufferSize)
prefixRequestBytes = StateObject.BufferSize;
state.lastSendByteCount = prefixRequestBytes;
state.totalBytesRead = 0;
// start re-writing to the begining of the buffer since we saved
client.BeginReceive(state.buffer, 0, prefixRequestBytes, 0, new AsyncCallback(ReceiveCallback), state);
return;
}
else
{
int bytesToSend = state.lastSendByteCount - bytesRead;
state.lastSendByteCount = bytesToSend;
// need to receive atleast first 8 bytes to continue
// Get the rest of the data.
client.BeginReceive(state.buffer, state.totalBytesRead, bytesToSend, 0, new AsyncCallback(ReceiveCallback), state);
return;
}
}
if (state.flag == 1)
{
// we are expexing to process the prefix
if (state.totalBytesRead >= state.prefixLen)
{
// we are good to process
// Lets always assume that our prefixMsg can fit into our prefixbuffer ( we wont send greater than prefixbuffer)
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, state.prefixLen));
string prefixMsg = state.sb.ToString();
if (!String.IsNullOrEmpty(prefixMsg))
{
state.cmd = parseXml("CMD", prefixMsg);
state.fName = parseXml("FNAME", prefixMsg);
// File requester should never get a del command
//if (state.cmd == "DEL")
//{
// Console.WriteLine("Processing 'DEL' command..");
// // delete the file
// string filePath = "C:\\" + locationFolder + "\\" + state.fName;
// if (System.IO.File.Exists(filePath))
// {
// System.IO.File.Delete(filePath);
// }
// Console.WriteLine("Deleted file");
// // receiveDone.Set();
// requestTimer.Start();
// return;
//}
//else
if (state.cmd == "SND")
{
Console.WriteLine("Processing 'SND' command..");
// let it rip
if (state.msgLen == 0) // no files
{
Console.WriteLine("No files on server");
requestTimer.Start();
return;
}
}
}
state.receivedPath = importTempFolder + state.fName;
// receive the rest of the file
if (System.IO.File.Exists(state.receivedPath))
{
Console.WriteLine("Deleting temp file: " + state.receivedPath + " for re-write");
System.IO.File.Delete(state.receivedPath);
}
state.flag++;
int msgRequestBytes = state.msgLen;
if (msgRequestBytes > StateObject.BufferSize)
msgRequestBytes = StateObject.BufferSize;
state.lastSendByteCount = msgRequestBytes;
state.totalBytesRead = 0;
// should be good to process the msg now
// start re-writing to the begining of the buffer since we saved
client.BeginReceive(state.buffer, 0, msgRequestBytes, 0, new AsyncCallback(ReceiveCallback), state);
return;
}
else
{
int bytesToSend = state.lastSendByteCount - bytesRead;
state.lastSendByteCount = bytesToSend;
// request the rest of the prefix
// Get the rest of the data.
client.BeginReceive(state.buffer, state.totalBytesRead, bytesToSend, 0, new AsyncCallback(ReceiveCallback), state);
return;
}
}
// we are expecting to process the file
if (state.flag > 1)
{
if (state.totalBytesRead >= state.msgLen)
{
Console.WriteLine("Writing final {0} bytes to server", bytesRead);
using (FileStream fs = new FileStream(state.receivedPath, FileMode.Append, FileAccess.Write))
using (BinaryWriter writer = new BinaryWriter(fs))
{
//BinaryWriter writer = new BinaryWriter(File.Open(state.receivedPath, FileMode.Append, FileAccess.Write, FileShare.None));
writer.Write(state.buffer, 0, bytesRead);
fs.Flush();
// writer.Close();
}
// GC.Collect();
// if temp folder exists, import will exists because its inside it
string destFile = importFolder + state.fName;
if (System.IO.File.Exists(destFile))
{
Console.WriteLine("Deleting file for re-write in importin: \n" + destFile);
System.IO.File.Delete(destFile);
}
Console.WriteLine("Moving file: \n" + state.receivedPath);
System.IO.File.Copy(state.receivedPath, destFile);
Console.WriteLine("Deleting file from temp: \n" + state.receivedPath + importTempFolder + state.fName);
System.IO.File.Delete(state.receivedPath);
if (state.cmd == "SND")
{
Console.WriteLine("Sending 'DEL' command.");
SendDeleteResponse(client, state.fName);
Console.WriteLine("Finished reading to file: " + state.receivedPath);
// receiveDone.Set();
requestTimer.Start();
return;
}
Console.WriteLine("Finished reading file");
client.Shutdown(SocketShutdown.Both);
client.Close();
// receiveDone.Set();
requestTimer.Start();
}
else
{
//Console.WriteLine("Reading {0} bytes from server...", bytesRead);
// Padd these bytes
using (FileStream fs = new FileStream(state.receivedPath, FileMode.Append, FileAccess.Write))
using (BinaryWriter writer = new BinaryWriter(fs))
{
//BinaryWriter writer = new BinaryWriter(File.Open(state.receivedPath, FileMode.Append, FileAccess.Write, FileShare.None));
writer.Write(state.buffer, 0, bytesRead);
fs.Flush();
// writer.Close();
}
// GC.Collect();
// get how many more bytes are left to read
int bytesToSend = state.msgLen - bytesRead;
if (bytesToSend > StateObject.BufferSize)
bytesToSend = StateObject.BufferSize;
client.BeginReceive(state.buffer, 0, bytesToSend, 0, new AsyncCallback(ReceiveCallback), state);
return;
}
}
}
else
{
// All the data has arrived;
}
}
catch (Exception e)
{
Console.WriteLine("HERE5 " + e.Message);
client.Close();
// receiveDone.Set();
requestTimer.Start();
return;
}
}

Image streaming from Java to C#

Java (client side)
relevant code
// Create the encoder and decoder for targetEncoding
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
byte[] underlyingBuffer = new byte[100000];
ByteBuffer buffer = ByteBuffer.wrap(underlyingBuffer);
System.out.println("first buffer remaining" + buffer.remaining() + " "
+ buffer.limit());
buffer.order(ByteOrder.LITTLE_ENDIAN);
try {
Socket client = new Socket("localhost", 8080);
OutputStream oStream = client.getOutputStream();
InputStream iStream = client.getInputStream();
//String secondFilePath = "C:\\Users\\pedge\\Desktop\\readData.csv";//this is csv file
//long secondFileLength = ReadFileToCharArray(buffer,secondFilePath , encoder);
String inputImage = "C:\\Users\\pedge\\Desktop\\Desert.jpeg";// image to transfer
ReadFileToCharArray(buffer,inputImage , encoder);
//imageWrite(oStream, inputImage);
buffer.flip();
int dataToSend = buffer.remaining();
int remaining = dataToSend;
while (remaining > 0) {
oStream.write(buffer.get());
--remaining;
}
public static long ReadFileToCharArray(ByteBuffer buffer, String filePath,
CharsetEncoder encoder) throws IOException {
fileCount++;
System.out.println("second buffer remaining" + buffer.remaining() + " "
+ buffer.limit());
StringBuilder fileData = new StringBuilder(100);
File file = new File(filePath);
System.out.println("Size of file ["+fileCount+"] sent is ["+file.length()+"]");
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[10000000];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
System.out.println(numRead);
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1000000];
}
reader.close();
CharBuffer charBuffer = CharBuffer.wrap(fileData.toString()
.toCharArray());
System.out.println("char buffer " + charBuffer.remaining());
ByteBuffer nbBuffer = null;
try {
nbBuffer = encoder.encode(charBuffer);
} catch (CharacterCodingException e) {
throw new ArithmeticException();
}
buffer.putInt(nbBuffer.limit());
System.out.println("buffer information" + buffer.position() + " "
+ buffer.limit() + " nbBuffer information" + nbBuffer.position()
+ " " + nbBuffer.limit());
buffer.put(nbBuffer);
return file.length();
}
C# (Server Side)
static void Main(string[] args)
{
try
{
Run(args);
}
catch (Exception e)
{
Console.Error.WriteLine(e);
}
}
static void Run(string[] args)
{
TcpListener listener = new TcpListener(8080);
listener.Start();
while (true)
{
using (TcpClient client = listener.AcceptTcpClient())
{
try
{
Read(client);
}
catch (Exception e)
{
Console.Error.WriteLine(e);
}
}
}
}
static void Read(TcpClient client)
{
String csvFile4Parsing = "C:\\Users\\pedge\\Desktop\\newReadData.csv";// csv file created from byte stream
Console.WriteLine("Got connection: {0}", DateTime.Now);
NetworkStream ns = client.GetStream();
BinaryReader reader = new BinaryReader(ns);
int length = reader.ReadInt32();
Console.WriteLine("Length of csv is [" + length + "]");
byte[] fileArray = reader.ReadBytes(length);
File.WriteAllBytes(csvFile4Parsing, fileArray);//bytes from stream, written to csv`
Now i am able to transfer the image from java to C# and a file is then created by c#(works perfect)
But when i try to send the image(same as like CSV), it doesn't work at all and even the bytes received at the server end are different from that of client end. I have spent hours now on this with no success. :/
and help will be much appreciated. thanks.
CSV data is text and might require the CharsetEncoder you are using to process the stream before sending it to the C#, but image data is binary and you only want to send a bit-for-bit copy of the data. The problem is likely to be that the stream encoder mistakenly interprets some binary sequences in the image data as text characters that need to be encoded and mangles them.
Just use plain binary stream read and write on both sides (you are already doing it in C#) and confirm that the data is exactly the same. If it isn't, post a hex sample of how it is different.
Finally the solved this issue.
Java(Client)
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class ImageServer {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8000);
//code to send image
File file = new File("C:\\Users\\ashish\\Desktop\\image.jpg");
FileInputStream fileInputStream = new FileInputStream(file);
int count=0;
byte[] fileBytes = new byte[(int)file.length()];
int content;
OutputStream outputStream = socket.getOutputStream();
while((content = fileInputStream.read(fileBytes)) != -1){
outputStream.write(fileBytes, 0, (int)file.length());
}
System.out.println("file size is "+ fileBytes.length);
for(byte a : fileBytes){System.out.println(a);}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
C#(Server)
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Test.SendImageClient
{
public class Program
{
static void Main(string[] args)
{
TcpListener tcpListener = null;
try
{
IPAddress ipadress = IPAddress.Parse("127.0.0.1");
tcpListener = new TcpListener(ipadress, 8000);
/* Start Listeneting at the specified port */
tcpListener.Start();
byte[] bytes = new byte[6790];
byte[] finalBytes;
while(true){
TcpClient client = tcpListener.AcceptTcpClient();
Console.WriteLine("Connected!");
NetworkStream stream = client.GetStream();
int i;
do
{
i = stream.Read(bytes, 0, bytes.Length);
finalBytes = new byte[i];
Console.WriteLine("Size dynamically is "+i);
for (int n = 0; n < i; n++ )
{
finalBytes[n] = bytes[n];
}
}while(stream.DataAvailable);
File.WriteAllBytes("C:\\Users\\ashish\\Desktop\\newImage.jpg", finalBytes);
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
tcpListener.Stop();
}
}
}
}

Streaming image over ssl socket doesn't come across correctly

I'm trying to securely transfer files between 2 devices, so I'm using an SslStream attached to a TcpClient. Documents and text files come across just fine, but image files don't show up correctly. The following is the server code:
listener = new TcpListener(IPAddress.Any, 1337);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(CertificateValidationCallback), new LocalCertificateSelectionCallback(CertificateSelectionCallback));
var certificate = Connection.GetClientCertificate(((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
try
{
sslStream.AuthenticateAsServer(certificate, true, SslProtocols.Default, true);
sslStream.ReadTimeout = 5000;
sslStream.WriteTimeout = 5000;
var messageData = ReadMessage(sslStream);
var mode = messageData[0];
var tokenBytes = messageData.Splice(1, 16);
var fileNameBytes = messageData.Splice(17, 128);
var fileBytes = messageData.Splice(146);
var fileName = Encoding.ASCII.GetString(fileNameBytes).TrimEnd('\0');
using (var tempFile = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
{
tempFile.Write(fileBytes, 0, fileBytes.Length);
tempFile.Flush();
}
if (mode == 0)
tempFiles.Add(fileName);
Process.Start(fileName);
}
catch (AuthenticationException e)
{
MessageBox.Show("The other side failed to authenticate.");
}
finally
{
sslStream.Close();
client.Close();
}
}
And ReadMessage is defined as follows:
private static byte[] ReadMessage(SslStream sslStream)
{
byte[] buffer = new byte[2048];
MemoryStream stream = new MemoryStream();
int bytes = -1;
while (bytes != 0)
{
bytes = sslStream.Read(buffer, 0, buffer.Length);
stream.Write(buffer, 0, bytes);
}
return stream.ToArray();
}
And then the client code is this:
TcpClient client = new TcpClient();
client.Connect(new IPEndPoint(IPAddress.Parse(ip), 1337));
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(CertificateValidationCallback), new LocalCertificateSelectionCallback(CertificateSelectionCallback));
var certificate = Connection.GetClientCertificate(ip);
try
{
sslStream.AuthenticateAsClient(ip, new X509CertificateCollection() { certificate }, SslProtocols.Default, false);
sslStream.ReadTimeout = 5000;
sslStream.WriteTimeout = 5000;
sslStream.Write(data);
}
catch (AuthenticationException e)
{
MessageBox.Show("The other side failed to authenticate.");
}
finally
{
sslStream.Close();
client.Close();
}
And the code that calls into it just does:
var fileBytes = File.ReadAllBytes(file);
var tokenBytes = Encoding.UTF8.GetBytes(token);
var fileNameBytes = Encoding.UTF8.GetBytes(Path.GetFileName(file));
var buffer = new byte[145 + fileBytes.Length];
buffer[0] = 1;
for (int i = 0; i < 16; i++)
{
buffer[i + 1] = tokenBytes[i];
}
for (int i = 0; i < fileNameBytes.Length; i++)
{
buffer[i + 17] = fileNameBytes[i];
}
for (int i = 0; i < fileBytes.Length; i++)
{
buffer[i + 145] = fileBytes[i];
}
SocketConnection.Send(ip, buffer);
Is there anything inherently wrong with what I'm doing, or do I need to do something different for images?
EDIT: I have changed it to reflect the current code, and also, after doing a dump of the raw bytes on both ends, it looks like for some reason the bytes are getting rearranged when they come over the wire. Is there any way to ensure that the bytes come across in the original order?
In ReadMessage you're writing bytes.Length bytes to the stream, regardless of the number of bytes that were actually read. Try:
private static byte[] ReadMessage(SslStream sslStream)
{
byte[] buffer = new byte[2048];
MemoryStream stream = new MemoryStream();
int bytes = -1;
while (bytes != 0)
{
bytes = sslStream.Read(buffer, 0, buffer.Length);
// Use "bytes" instead of "buffer.Length" here
stream.Write(buffer, 0, bytes);
}
return stream.ToArray();
}
Based on your follow-up, you're also taking the file data from the wrong point in the buffer, and so you're losing the first byte of the file.
Your code should be:
var fileBytes = messageData.Splice(145); // File data starts at 145, not 146
Is this possibly a conflict between endianness? If the bytes from the server are ABCDEF and the client is seeing the image bytes as BADCFE then that's the issue.
I haven't worked with image files, but when I read a short or an int instead of a String from the bytes coming in over the wire, I do something like this:
int intFromStream = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(byteArrayWithLength4, 0));

Categories

Resources