Streaming MP3 to Android from C# server - c#

I have set up a C# server which at present serves up one test mp3 file over TCP. The code to send the file is as follows
public void RunStreamer()
{
log.MakeLog("Starting streamer");
Run = true;
//Need to look into how to thread this to not block the main window
TcpListener listen = new TcpListener(localAddr, _port);
listen.Start(); //startlistening to client requests
//blocks until a client request comes in
for (; ; )
{
Socket socket = listen.AcceptSocket();
if (socket.Connected)
{
SendFileToClient(socket);
socket.Disconnect(false);
}
}
}
void SendFileToClient(Socket socket)
{
log.MakeLog("Connection made");
NetworkStream netStream = new NetworkStream(socket);
StreamWriter writer = new StreamWriter(netStream);
//Todo - set specfified file - this file just for test
FileStream filestream = File.Open(#"C:\MusicTest\Test.mp3", FileMode.Open, FileAccess.Read, FileShare.Read);
filestream.CopyTo(netStream);
netStream.Flush();
netStream.Close();
}
In my test android set up I am making a call to the server on a button click:
public void btngo_click(View v)
{
final TcpClient client = new TcpClient();
new Thread(new Runnable(){
#Override
public void run() {
final MediaPlayer mediaPlayer = new MediaPlayer();
client.GetStream();
runOnUiThread(new Runnable(){
public void run()
{
int length = client.GetLength();
if(length > 0)
{
byte[] result = client.GetResult();
try {
// create temp file that will hold byte array
File tempMp3 = File.createTempFile("test", "mp3", getCacheDir());
tempMp3.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempMp3);
fos.write(result);
fos.close();
mediaPlayer.reset();
FileInputStream fis = new FileInputStream(tempMp3);
mediaPlayer.setDataSource(fis.getFD());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException ex) {
String s = ex.toString();
ex.printStackTrace();
}
}
}
});
}
}).start();
}
the stream is received in the TcpClient class which is as follows:
public class TcpClient {
public final static String SERVER_ADDRESS = "127.0.0.1";
public final static int SERVER_PORT = 65000;
public String TotalResult;
public int Length;
byte[] result = new byte[21000000];
public TcpClient()
{
}
public int GetLength()
{
return Length;
}
public byte[] GetResult()
{
return result;
}
public void GetStream()
{
try
{
final Socket socket = new Socket("192.0.0.5", 85000);
final InputStream input = new BufferedInputStream(socket.getInputStream());
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nread;
while((nread = input.read(result, 0, result.length)) != -1)
{
buffer.write(result, 0, nread);
}
buffer.flush();
//input.read(result);
Length = result.length;
input.close();
socket.close();
} catch (UnknownHostException e) {
String exc = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
String exc2 = e.getMessage();
e.printStackTrace();
}
}
}
With apologies for all the code here is my problem.
I am receiving the stream. The temp MP3 file is created and the media player starts. I then only get a short snippet of the test MP3 file (which is a full song). It also jumps about a bit. The length is not the same and the section of the song played is different each time.
How do I receive the full file in a ordered way such that it will provide full play back of the song.
I have tried to route around for this and have an idea that I need to tell my client what file size it should suspect and then perform some loop until all data is received although I have no idea how to successfully implement this if that is the correct solution.
Any pointers on where I am going wrong or what I can do to rectify would be greatly appreciated!!

Having received no answers on this I dug around a bit more. Two things were wrong:
Firstly I had not included the size of the stream as a int sized header in my stream. I understand that for smaller files this will not be a problem but as file sizes grow it is necessary to make sure that the whole stream has been received.
This in turn raised another issue. The int I was sending as byte[] form c# was not returning the correct value in Java. Turns out Java uses sbytes -128 to 127 range as opposed to byte. This then involved a bit of code to convert to an int. then I could instruct the reader to readfully passing in the byte[] buffer with the actual size of the expected stream = voila it worked. MP3 files is received and plays just fine.

Related

How receive a complete screenshot in Async socket?

I have a Java android code that sends data (image or text) to a C# application, to receive these data I'm using Async socket. But exists a problem that is relative to BeginReceive() function is not receiving the complete data when is sent an image.. Then how I can make a kind of "loop" to receive full data and after show the image on Picturebox (for example)?
Form
private Listener listener;
private Thread startListen;
private Bitmap _buffer;
public frmMain()
{
InitializeComponent();
}
private void serverReceivedImage(Client client, byte[] image)
{
try
{
byte[] newImage = new byte[image.Length - 6];
Array.Copy(image, 6, newImage, 0, newImage.Length);
using (var stream = new MemoryStream(newImage))
{
using (var msInner = new MemoryStream())
{
stream.Seek(2, SeekOrigin.Begin);
using (DeflateStream z = new DeflateStream(stream, CompressionMode.Decompress))
{
z.CopyTo(msInner);
}
msInner.Seek(0, SeekOrigin.Begin);
var bitmap = new Bitmap(msInner);
Invoke(new frmMain.ImageCompleteDelegate(ImageComplete), new object[] { bitmap });
}
}
}
catch (Exception)
{
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
}
private delegate void ImageCompleteDelegate(Bitmap bitmap);
private void ImageComplete(Bitmap bitmap)
{
if (_buffer != null)
_buffer.Dispose();
_buffer = new Bitmap(bitmap);
pictureBox1.Size = _buffer.Size;
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (_buffer == null) return;
e.Graphics.DrawImage(_buffer, 0, 0);
}
private void startToolStripMenuItem_Click(object sender, EventArgs e)
{
startListen = new Thread(listen);
startListen.Start();
}
private void listen()
{
listener = new Listener();
listener.BeginListen(101);
listener.receivedImage += new Listener.ReceivedImageEventHandler(serverReceivedImage);
startToolStripMenuItem.Enabled = false;
}
Listener
class Listener
{
private Socket s;
public List<Client> clients;
public delegate void ReceivedImageEventHandler(Client client, byte[] image);
public event ReceivedImageEventHandler receivedImage;
private bool listening = false;
public Listener()
{
clients = new List<Client>();
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public bool Running
{
get { return listening; }
}
public void BeginListen(int port)
{
s.Bind(new IPEndPoint(IPAddress.Any, port));
s.Listen(100);
s.BeginAccept(new AsyncCallback(AcceptCallback), s);
listening = true;
}
public void StopListen()
{
if (listening == true)
{
s.Close();
listening = false;
}
}
void AcceptCallback(IAsyncResult ar)
{
Socket handler = (Socket)ar.AsyncState;
Socket sock = handler.EndAccept(ar);
Client client = new Client(sock);
clients.Add(client);
sock.BeginReceive(client.buffer, 0, client.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), client);
client.Send("REQUEST_PRINT" + Environment.NewLine);
handler.BeginAccept(new AsyncCallback(AcceptCallback), handler);
}
void ReadCallback(IAsyncResult ar)
{
Client client = (Client)ar.AsyncState;
try
{
int rec = client.sock.EndReceive(ar);
if (rec != 0)
{
string data = Encoding.UTF8.GetString(client.buffer, 0, rec);
if (data.Contains("SCREEN"))
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
receivedImage(client, bytes);
}
else // not is a image, is a text
{
// prepare text to show in TextBox
}
}
else
{
Disconnected(client);
return;
}
client.sock.BeginReceive(client.buffer, 0, client.buffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), client);
}
catch
{
Disconnected(client);
client.sock.Close();
clients.Remove(client);
}
}
}
Client
class Client
{
public Socket sock;
public byte[] buffer = new byte[8192];
public Client(Socket sock)
{
this.sock = sock;
}
public void Send(string data)
{
byte[] buffer = Encoding.ASCII.GetBytes(data);
sock.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback((ar) =>
{
sock.EndSend(ar);
}), buffer);
}
}
Android code
private byte[] compress(byte[] data) {
Deflater deflater = new Deflater();
deflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
deflater.finish();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count = deflater.deflate(buffer);
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte[] output = outputStream.toByteArray();
return output;
}
public static DataOutputStream dos;
public static byte[] array;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
array = compress(bos.toByteArray());
//...
dos = new DataOutputStream(SocketBackgroundService.clientSocket.getOutputStream());
byte[] header = ("SCREEN").getBytes(StandardCharsets.UTF_8);
byte[] dataToSend = new byte[header.length + array.length];
System.arraycopy(header, 0, dataToSend, 0, header.length);
System.arraycopy(array, 0, dataToSend, header.length, array.length);
dos.writeInt(dataToSend.length);
dos.write(dataToSend, 0, dataToSend.length);
dos.flush();
EDITION
i'm always getting the error Invalid Parameter in this line
var bitmap = new Bitmap(msInner);
and using compression also happens the same here
z.CopyTo(msInner);
IvalidDataException
on ServerReceivedImage() method respectively.
using this
File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "image.png"), newImage);
i noted that is receiving only 15KB (size of file without use compression).
I was writing a comment but it does not give me enough space to express my frustration with your code.
My main points are
You try to recompress and perfectly compressed image. PNG is portable network graphics. It was designed for network transfers. If it is acceptable you should use something like jpeg.
You just decode received buffer using UTF8.GetString and search for a text, then re-encode that string and try to decompress and read an image from it, by starting from index 6 which is pretty meaningless considering you added a two byte size field to the start of stream and you really do not know position of "SCREEN".
You do not check if you have received ALL of the stream data.
All of the code looks like you have scoured the SO questions and answers and created a copy pasta.
Now my recommendations.
When transferring data from network, do not try to invent wheels. Try something like gRPC which has both android java and c# packages.
If you will use raw data, please, please know your bytes.
I assume you will extend your code by adding new command pairs. Since you have no magic markers of some kind of signal system, it will be very hard for you to distinguish data from header. For a simple implementation add some kind of magic data to your header and search for that data, then read header and then read data. You may need to read from socket again and again until you receive all of the data.
424A72 0600 53435245454E 008E0005 ..... 724A42
B J r 6 S C R E E N 36352 ..... rJB
this sample data shows that we have a valid stream by looking at "BJr". Then read a 2 byte unsigned integer to read command size which is 6 for SCREEN. Read command and then read four bytes unsigned length for command data. For our sample it is 36352. Just to be safe I've added an end of command marker "rJB".
For a bonus point try reducing memory allocations / copies, you can look at System.Span<T>

c# TCP NetworkStream is not receiving data

I'm trying to send files after sending their's information via TCP connection. At the end of receiver host, the data packets are received. I used Wireshark to confirm it. However the data couldn't be received in NetworkStream.
public class FileTransporter
{
public void ReceiveFiles(IPAddress IP, int port)
{
TcpListener tcpListener = new TcpListener(IP, port);
tcpListener.Start();
using (TcpClient tcpClient = tcpListener.AcceptTcpClient())
{
if (tcpClient.Connected)
{
using (NetworkStream networkStream = tcpClient.GetStream())
{
int pointer = 0;
byte[] fileNameLengthBytes = new byte[sizeof(int)];
networkStream.Read(fileNameLengthBytes, pointer, fileNameLengthBytes.Length);
int fileNameLength = BitConverter.ToInt32(fileNameLengthBytes, pointer);
// code to read fileName and it's size
networkStream.Close();
}
}
tcpClient.Close();
}
tcpListener.Stop();
}
public void SendFiles(IPAddress IP, int port, string[] paths)
{
for(int i=0; i<paths.Length; i++)
{
FilePackage filePackage = new FilePackage(paths[i]);
byte[] infoBytes = filePackage.EncodeInfoToByte();
using (TcpClient tcpClient = new TcpClient())
{
tcpClient.Connect(IP, port);
using (NetworkStream networkStream = tcpClient.GetStream())
{
networkStream.Write(infoBytes, 0, infoBytes.Length);
networkStream.Close();
}
tcpClient.Close();
}
}
}
}
public class FilePackage
{
public FilePackage(string fileName)
{
this.Info = new FileInfo(fileName);
}
public byte[] EncodeInfoToByte()
{
List<byte> infoByte = new List<byte>();
infoByte.AddRange(BitConverter.GetBytes(this.Info.Name.Length));
infoByte.AddRange(Encoding.UTF8.GetBytes(this.Info.Name));
infoByte.AddRange(BitConverter.GetBytes(this.Info.Length));
return infoByte.ToArray();
}
I am going to Assume that ReadFiles is invoked somehow / somewhere else.
I have a very similar implementation, however my "Read" of the stream is much smaller:
// Read the first batch of the TcpServer response bytes.
var bytes = new byte[512];
// Loop to receive all the data sent by the Server.
var sb = new StringBuilder();
do
{
var i = stream.Read(bytes, 0, bytes.Length);
sb.AppendFormat("{0}", Encoding.ASCII.GetString(bytes, 0, i));
} while (stream.DataAvailable);
Perhaps using "stream.DataAvailable" will allow you to test for data existence and that way you can make sure that you only process when data is available.
Also I don't see where "pointer" is being initialized in your code.
If it is not set to 0 each time, you will not be processing from the beginning of the data packet.
Your problem is probably being caused by the nagle algorithm, prevent small amounts of data being sent to reduce congestion. You could try disabling it by setting the following properties on your tcpClient
tcpClient.NoDelay = true;
tcpClient.Client.NoDelay = true

TCP IP Client in WPF for Ubuntu 14 Server

I am trying to create TCP IP Client in WPF GUI / C#.NET for a Ubunu Server.
Problem: I am able to connect to the server machine , the connection work correctly, send the message correctly and the ubuntu console show as well , Client Connected and sent this command like Start Video Feed on the server but When it comes to Read the Response nothing happens - It does not read the byte array that should be returned by the Ubuntu Server. Actually on message - 102 it should start the video feed on the server and return back the video feed byte array which should be read further and display the video. No code written yet to display the video feed as I am unable to read the feed from the server however, the client sends the commands(messages) to the server correctly as mentioned can view it on a console of the Ubuntu server machine. Please suggest thanks !!
Below is the code please have a look and suggest me what I am doing wrong :
namespace POC_TCP_Listener
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private int WhichEventFired = 0;
private void Button_Click_1(object sender, RoutedEventArgs e)
{
try
{
// string message = "{Site: 1}";
WhichEventFired = 1; //Start Video Feed
Thread ClientThread = new Thread(new ThreadStart(ConnectToServerAndRetrieveBytes));
ClientThread.Start();
}
catch (Exception ex)
{
string st = ex.Message;
}
}
private void ConnectToServerAndRetrieveBytes()
{
TcpClient TCP = new TcpClient();
TCP.Connect("IPAddress", 5001);
byte[] packet;
var size = 9;
var header = 102;
var siteId = 1;
var state = 1;
if (WhichEventFired == 1)
{
header = 102; // Start Video Feed
}
else if (WhichEventFired == 2)
{
header = 114; // Stop Video Feed
}
else
{
header = 115; // query Temperature
}
// <8> <115> <1>
packet = BitConverter.GetBytes(size).Reverse().Concat(BitConverter.GetBytes(header).Reverse()).Concat(BitConverter.GetBytes(siteId).Reverse()).Concat(BitConverter.GetBytes(state).Reverse()).ToArray();
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = packet;
// Get a client stream for reading and writing.
NetworkStream stream = TCP.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
byte[] buffer = new byte[64 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
// In the below line - it stops and nothing happens after it - Please Suggest
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
// return ms.ToArray();
}
stream.Close();
TCP.Close();
}
}
}
strong text
Please suggest why it stops working or let me know if I am doing anything wrong.
I think the problem is that if there are no more packages left, it escapes the while-loop. If you want to check it endlessly you should try using a for. But i am not sure.
Yeah that should be it. If there are no Packages in the moment the while-loop is starting, it terminates itself because there is nothing to read. So you could check if the Ubuntu Server returns you something.

Named pipes async

Im trying to set up a named pipe server and client to send data between two programs.
My issue is that when i data is recived eg. BeginRead command om server triggers after i have serialized an object from the client it triggers the callback like 20 times for the same message. The goal is that the client program will send commands to the server program. And when the server processes tasks it will send status updates back to the client when there is one connected.
Here is my current test program.
class Program
{
static void Main(string[] args)
{
var server = new PipeServer();
server.Init();
var client = new PipeClient();
if (client.Connect())
{
Console.WriteLine("Connected to server.");
}
else
{
Console.WriteLine("Connection failed.");
return;
}
while (true)
{
Console.Write(" \\> ");
string input = Console.ReadLine();
if (string.IsNullOrEmpty(input)) break;
var arr = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int value = 0;
if (arr.Length != 2) break;
if (!int.TryParse(arr[1], out value)) break;
var obj = new PipeObject { Name = arr[0], Value = value };
client.Send(obj);
//string result = f.Deserialize(client) as string;
//Console.WriteLine(result);
}
}
}
internal class PipeServer
{
IFormatter Formatter = new BinaryFormatter();
public NamedPipeServerStream Instance { get; internal set; }
public bool IsConnected { get; internal set; }
byte[] buffer = new byte[65535];
public object Message { get; set; }
StreamReader sr;
StreamWriter sw;
internal PipeServer()
{
IsConnected = false;
}
public void Init()
{
var ps = new PipeSecurity();
ps.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().User, PipeAccessRights.FullControl, AccessControlType.Allow));
ps.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));
Instance = new NamedPipeServerStream("Levscan4Pipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous | PipeOptions.WriteThrough, 65535, 65535, ps);
sr = new StreamReader(Instance);
sw = new StreamWriter(Instance);
Instance.BeginWaitForConnection(OnClientConnected, Instance);
Thread t = new Thread(Run);
t.Start();
}
void Run()
{
int index = 0;
if (IsConnected)
{
try
{
Instance.BeginRead(buffer, 0, buffer.Length, OnRead_Completed, Instance);
//index += Instance.Read(buffer, 0, buffer.Length);
//try
//{
// using (var ms = new MemoryStream(buffer))
// {
// Message = Formatter.Deserialize(ms);
// index = 0;
// }
//}
//catch (Exception e)
//{
// Debug.WriteLine(e.Message);
// Debug.WriteLine(e.StackTrace);
//}
}
catch (IOException)
{
IsConnected = false;
Instance.Disconnect();
}
}
Thread.Sleep(Timeout.Infinite);
//Instance.WaitForConnection();
//Thread t = new Thread(Run);
//t.Start();
}
void OnClientConnected(IAsyncResult ar)
{
Instance.EndWaitForConnection(ar);
IsConnected = true;
}
void OnRead_Completed(IAsyncResult ar)
{
var bytes = Instance.EndRead(ar);
Debug.WriteLine("{1} > Read completed - bytes read: {0}".FormatWith(bytes, DateTime.Now.ToString()));
//try
//{
// using (var ms = new MemoryStream(buffer))
// {
// Message = Formatter.Deserialize(ms);
// }
//}
//catch (Exception e)
//{
// Debug.WriteLine(e.Message);
// Debug.WriteLine(e.StackTrace);
//}
}
}
internal class PipeClient
{
IFormatter f = new BinaryFormatter();
public NamedPipeClientStream Instance { get; internal set; }
StreamWriter sw;
StreamReader sr;
public PipeClient()
{
Instance = new NamedPipeClientStream(".", "Levscan4Pipe", PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.WriteThrough);
sr = new StreamReader(Instance);
sw = new StreamWriter(Instance);
}
public bool Connect()
{
try
{
Instance.Connect(5000);
Instance.ReadMode = PipeTransmissionMode.Message;
Instance.WaitForPipeDrain();
return true;
}
catch
{
return false;
}
}
public void Send(object obj)
{
f.Serialize(Instance, obj);
Instance.Flush();
Instance.WaitForPipeDrain();
}
}
Edit
Changed the while loop to a if, to start the BeginRead. This solves the multiple callbacks but i stil dont get a complete message.
If the server is writing to the stream like:
write field 1
write field 2
write field 3
etc.
There is some time between the writes, and the receiver (your program) can be reading the first three fields while the server is still writing the others. The pipe stream doesn't know when the server is finished writing, so it can't buffer everything and send it to you all in one big chunk.
When the server writes everything to a memory stream first and then copies the memory stream to the pipe stream, your program can get it all at once. Maybe. If the server is sending a very large packet, you might read just part of it.
The pipe stream is just a stream of bytes. It doesn't impose any format on the data. It doesn't have any concept of records or anything like that. So you have to treat it like a stream of bytes and do your own composing of records, etc.
If you need to know the size of the record sent from the server, the server has to put that information in the stream for you. Typically, the server will write the length and then the data. The receiver can then read the length, convert it to an integer, and then read that many bytes from the stream. And, yes, it might take multiple reads in order to get all of the bytes. That's just the nature of a byte stream.
The other way to handle this is to have an end-of-record marker. So the server sends its data and your program reads until it finds the byte sequence that signifies the end of the record. You have to be careful, though, because the server could be sending multiple records and your read could grab the end of one record as well as the beginning of the next.
Working with byte streams can be a lot of work because you have to reconstruct records after reading the bytes. It's much easier to use an existing framework (like WCF, as mentioned in one of the comments) if you can.

Send a large file over tcp connection

I need to send a few large files to a computer over the internet. As a result I opened the ports that I plan to use on the router and forward the correspoinding ip addresses. anyways let me show you the classes that I have been working on in order to achieve this. This classes work great with small files but sometimes they fail with large files.
here is the code for the server: (it is a console application)
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication20
{
//server
class Program
{
static Server s;
public static void mm()
{
s = new Server("192.168.0.196");
s.startServer();
Console.Read();
}
static void Main(string[] args)
{
// Thread t = new Thread(new ThreadStart(mm));
// t.Start();
mm();
Console.Read();
s.disconnect();
}
}
class MyTCP
{
protected const int MaxChunkSize = 4096;
protected Int32 port { get; set; }
protected string serverIP { get; set; }
protected TcpClient client { get; set; }
protected static NetworkStream stream { get; set; }
protected void sendData(NetworkStream stream, Byte[] data)
{
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
}
protected String receiveData(NetworkStream stream)
{
// Buffer to store the response bytes.
Byte[] data = new Byte[MaxChunkSize];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
return responseData;
}
protected static Byte[] textToBytes(string text)
{
return System.Text.Encoding.ASCII.GetBytes(text);
}
public virtual void disconnect() { }
public bool isServerConected { get { return client.Connected; } }
}
[Serializable]
public class FileProperties
{
public string FileName { get; set; }
public string DestPath { get; set; }
public double FileSize { get; set; }
public FileAttributes fileAttributes { get; set; }
public System.Security.AccessControl.FileSecurity FileSecurity { get; set; }
public DateTime creationTime { get; set; }
public DateTime lastAccessTime { get; set; }
public DateTime lastWriteTime { get; set; }
}
class Server: MyTCP
{
private System.IO.FileStream _FileStream;
private static TcpListener server;
private static bool disconect;
/// <summary>
/// Constructor
/// </summary>
/// <param name="localAddr">The ip address of the server</param>
/// <param name="port">on what port the server going to be listening to?</param>
/// <param name="autoStartServer">start listening for connections now? you may call the startserver() method latter...</param>
public Server(string localAddr, Int32 port = 13000, bool autoStartServer = false)
{
this.port = port;
this.serverIP = localAddr;
if (autoStartServer)
start();
}
/// <summary>
/// Start listening for connections
/// </summary>
public void startServer()
{
start();
}
public override void disconnect()
{
// Close everything.
stream.Close();
client.Close();
server.Stop();
disconect = true;
}
void start()
{
server = null;
try
{
// TcpListener server = new TcpListener(port);
server = new TcpListener(IPAddress.Parse(serverIP), port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[MaxChunkSize];
String data = null;
// Enter the listening loop.
while (disconect==false)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
// Get a stream object for reading and writing
stream = client.GetStream();
int i;
try
{
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
if (data.ToUpper().Contains("<sendFile>".ToUpper()))
{
receiveFile(bytes);
}
continue;
}
}
catch { }
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
void receiveFile(Byte[] bytes)
{
// send 1
sendData(stream, textToBytes("<1>"));
// receive 2
int length = stream.Read(bytes, 0, bytes.Length);
byte[] tempA = new byte[length];
for (int k = 0; k < length; k++)
tempA[k] = bytes[k];
Stream ms = new MemoryStream(tempA);
FileProperties p = new FileProperties();
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
try
{
p = (FileProperties)x.Deserialize(ms);
if (Directory.Exists(p.DestPath))
{
//send 3
sendData(stream, textToBytes("<3>"));
}
else
{
//send 3
sendData(stream, textToBytes("<no>"));
return;
}
}
catch
{
//send 3
sendData(stream, textToBytes("<no>"));
return;
}
int i;
string temp = Path.Combine(new string[]{ p.DestPath, p.FileName + ".temp"});
_FileStream = new System.IO.FileStream(temp, System.IO.FileMode.Create, System.IO.FileAccess.Write);
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
if (i == 11 & System.Text.Encoding.ASCII.GetString(bytes, 0, i).ToUpper().Equals("</sendFile>".ToUpper()))
{
_FileStream.Close();
Console.WriteLine("D!");
File.SetAttributes(temp, p.fileAttributes);
File.SetAccessControl(temp, p.FileSecurity);
File.SetCreationTime(temp, p.creationTime);
File.SetLastAccessTime(temp, p.lastAccessTime);
File.SetLastWriteTime(temp, p.lastWriteTime);
if(File.Exists(temp.Substring(0, temp.Length - 4)))
File.Delete(temp.Substring(0, temp.Length - 4));
File.Move(temp, temp.Substring(0, temp.Length - 4));
//sendData(stream, textToBytes("<done>"));
Console.WriteLine("Done!");
return;
}
_FileStream.Write(bytes, 0, i);
}
return;
}
}
}
and the code for my client is:
using System;
using System.Net.Sockets;
using System.Windows;
using System.IO;
namespace WpfApplication23sdfd
{
[Serializable]
public class FileProperties
{
public string FileName { get; set; }
public string DestPath { get; set; }
public double FileSize { get; set; }
public FileAttributes fileAttributes { get; set; }
public System.Security.AccessControl.FileSecurity FileSecurity { get; set; }
public DateTime creationTime { get; set; }
public DateTime lastAccessTime { get; set; }
public DateTime lastWriteTime { get; set; }
}
abstract class MyTCP
{
protected const int MaxChunkSize = 4096;
protected Int32 port { get; set; }
protected string serverIP { get; set; }
protected TcpClient client { get; set; }
protected static NetworkStream stream { get; set; }
protected void sendData(NetworkStream stream, Byte[] data)
{
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Receive the TcpServer.response.
}
protected String receiveData(NetworkStream stream)
{
// Buffer to store the response bytes.
Byte[] data = new Byte[MaxChunkSize];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
return responseData;
}
protected static Byte[] textToBytes(string text)
{
return System.Text.Encoding.ASCII.GetBytes(text);
}
public virtual void disconnect() { }
public bool isServerConected { get { return client.Connected; } }
}
//client
class Client: MyTCP
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="serverIP">the ip address of the server</param>
/// <param name="port">through what port is the connection going to be established</param>
public Client(string serverIP, Int32 port = 13000, bool autoConnect = false)
{
this.port = port;
this.serverIP = serverIP;
if (autoConnect)
connect();
}
public bool connect()
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes("connect");
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
try
{
client = new TcpClient(serverIP, port);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
stream = client.GetStream();
return true;
}
catch
{
return false;
}
}
public override void disconnect()
{
// Close everything.
stream.Close();
client.Close();
}
static void ConnectOld(String server, Byte[] data)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
TcpClient client = new TcpClient(server, port);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
public void sendFile(string file, string destPath = "c:\\")
{
//let server know what you are going to be doing...
sendData(stream, textToBytes("<sendFile>"));
FileProperties p = new FileProperties {
creationTime = File.GetCreationTime(file),
fileAttributes = File.GetAttributes(file),
FileSecurity = File.GetAccessControl(file),
lastAccessTime = File.GetLastAccessTime(file),
lastWriteTime = File.GetLastWriteTime(file),
DestPath = destPath,
FileName = Path.GetFileName(file)
};
// receive 1
if (!receiveData(stream).ToUpper().Contains("<1>".ToUpper()))
{
MessageBox.Show("Error comunicating with server");
return;
}
// send object p to server
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
x.Serialize(stream, p); // send 2
//recieve 3
if (!receiveData(stream).ToUpper().Contains("<3>".ToUpper()))
{
MessageBox.Show("Error incorrect parameters sent to server");
return;
}
System.IO.FileStream streamFile = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
while (true)
{
byte[] chunk = new byte[MaxChunkSize];
int index = 0;
// There are various different ways of structuring this bit of code.
// Fundamentally we're trying to keep reading in to our chunk until
// either we reach the end of the stream, or we've read everything we need.
while (index < chunk.Length)
{
int bytesRead = streamFile.Read(chunk, index, chunk.Length - index);
if (bytesRead == 0)
{
break;
}
if (bytesRead < MaxChunkSize)
{
byte[] temp = new byte[bytesRead];
for (var i = 0; i < bytesRead; i++)
temp[i] = chunk[i];
chunk = temp;
}
index += bytesRead;
}
if (index != 0) // Our previous chunk may have been the last one
{
sendData(stream,chunk); // index is the number of bytes in the chunk
}
if (index != chunk.Length) // We didn't read a full chunk: we're done
{
sendData(stream, textToBytes("</sendFile>".ToUpper()));
//receiveData(stream);//wait recall missing to check results
return;
}
}
}
}
}
the way I instantiate the client class is by providing the IP address of the server as:
Client c = new Client("192.168.0.196");
c.sendFile(#"A:\Users\Tono\Desktop\a.mp4");
the server has to be running first before executing that code.
I don't know why is so complicated to send a file over the internet using sockets. I don't konw WCF that's why I been loosing a lot of time creating this classes. Maybe there are already some built in classes that will enable me to send files over the internet to a different computer. I know just the basics of networking thereofore it will be nice if I could do it with a simple class. I don't undersatnd why my classes do not work all the time? if I increase the buffer size will my classes be more efficient? do I have to wait or pause my program a little bit before sending more bytes? It will be nice if someone can tell me what is wrong with this classes. They work nice with small files but with large files sometimes it does not work...
Here is my code to send a large file.
Some tips:
Check your buffer size. If it's too big it will fail.
Socket flags. The partial flag works the best.
Socket timeout is needed due to the transfer time.
Client :
string IPAddress = "";
int Port = 500;
string Filename = #"C:\Users\Ben\Desktop\TT.zip";
int bufferSize = 1024;
byte[] buffer = null;
byte[] header = null;
FileStream fs = new FileStream(Filename, FileMode.Open);
bool read = true;
int bufferCount = Convert.ToInt32(Math.Ceiling((double)fs.Length / (double)bufferSize));
TcpClient tcpClient = new TcpClient(IPAddress, Port);
tcpClient.SendTimeout = 600000;
tcpClient.ReceiveTimeout = 600000;
string headerStr = "Content-length:" + fs.Length.ToString() + "\r\nFilename:" + #"C:\Users\Administrator\Desktop\" + "test.zip\r\n";
header = new byte[bufferSize];
Array.Copy(Encoding.ASCII.GetBytes(headerStr), header, Encoding.ASCII.GetBytes(headerStr).Length);
tcpClient.Client.Send(header);
for (int i = 0; i < bufferCount; i++)
{
buffer = new byte[bufferSize];
int size = fs.Read(buffer, 0, bufferSize);
tcpClient.Client.Send(buffer,size,SocketFlags.Partial);
}
tcpClient.Client.Close();
fs.Close();
Server :
int Port = 500;
TcpListener listener = new TcpListener(IPAddress.Any, Port);
listener.Start();
Socket socket = listener.AcceptSocket();
int bufferSize = 1024;
byte[] buffer = null;
byte[] header = null;
string headerStr = "";
string filename = "";
int filesize = 0;
header = new byte[bufferSize];
socket.Receive(header);
headerStr = Encoding.ASCII.GetString(header);
string[] splitted = headerStr.Split(new string[] { "\r\n" }, StringSplitOptions.None);
Dictionary<string, string> headers = new Dictionary<string, string>();
foreach (string s in splitted)
{
if (s.Contains(":"))
{
headers.Add(s.Substring(0,s.IndexOf(":")), s.Substring(s.IndexOf(":") + 1));
}
}
//Get filesize from header
filesize = Convert.ToInt32(headers["Content-length"]);
//Get filename from header
filename = headers["Filename"];
int bufferCount = Convert.ToInt32(Math.Ceiling((double)filesize / (double)bufferSize));
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
while(filesize > 0)
{
buffer = new byte[bufferSize];
int size = socket.Receive(buffer,SocketFlags.Partial);
fs.Write(buffer,0,size);
filesize -= size;
}
fs.Close();
Hope this will help someone.
There are a few issues that I can see immediately. The one that may be causing your program to only work some of the time is the fact that sending via TCP will not guarantee that every send will result in an identically-sized receive on the other side.
Your protocol seems to assume it will, because you're waiting for a read of exactly 11 bytes for the </sendFile>, whereas it could be received in multiple separate reads. E.g.: "[file data...]". If this happens, your code will not correctly finish.
It's also worth noting that the ASCII encoding is 7-bit, and so binary files (such as the MP4) will be received incorrectly (even if you fix the above). If it is binary data, you should not attempt to convert it to a string, but instead write it to file directly from the byte[].
If you wish to contine down this route (rather than using the many existing file transfer systems already available as mentioned in another answer) then you may also want to change your protocol so that instead of delimiting the file with <sendFile>...</sendFile>, you send the file's length initially, which will allow you to send files that may contain one of these special tags.
Sending files / directories over sockets is not trivial. I would recommend using some file transfer library (over sockets, or maybe higher level protocol, i.e. rsync, ftp, http, etc), rather than trying to code the thing from scratch.
After skimming over the code - try sending a large file containing some uniform content (filled with '0's or something). If it would pass through - your xml thing doesn't work.
Basically what you are doing is correct. The main point I could suggest for improvement is setting the MaxChunkSize to a larger value, as much as 65000. This will enable the socket code to handle any fragmentation, which will be much more efficient than any splitting you will do by yourself.
Also, you are aware to the fact that sending a large file should take some time. At a 100 Mbit LAN, the bandwidth would be (theoretically) 12.5 MByte per second. Thus sending a 700 MByte file will still take 56 seconds. Of course that the actual throughput depends and many factors in the network and the computers and network hardware involved, so it would be expected to be slower.
Finally, Wireshark (or any other sniffer program) is the most basic and invaluable tool in the networking programmer toolbox. Use it on both the server and the client to see how the TCP packets are transmitted, to see if you can identify any causes for the slow trasnfer.

Categories