I'm developing a windows store application, using C#. I would like to make TCP connection to receive images (for now) from a desktop server. the server is in C++ .
I have a client C++ to test the function and it is working perfectly. Now what i want is a similar client but in C# . I tried converting it but no luck, i tried to use the same logic but i had tons of errors and deleted everything.
Help is appreciated,thanks.
C++ Server
int size = 8192; //image size
char* bufferCMP;
bufferCMP = (char*)malloc(sizeof(char)* size);
FILE *p_file;
p_file = fopen("C:\\Program Files\\img1.png", "rb");
fread(bufferCMP, 1, size, p_file);
fclose(p_file);
int chunkcount = size / DEFAULT_BUFLEN;
int lastchunksize = size - (chunkcount * DEFAULT_BUFLEN);
int fileoffset = 0;
printf("Sending actual Chunk");
while (chunkcount > 0)
{
iResult = send(ClientSocket, bufferCMP + (fileoffset * DEFAULT_BUFLEN), DEFAULT_BUFLEN, 0);
fileoffset++;
chunkcount--;
if (iResult != DEFAULT_BUFLEN)
{
printf("Sending Buffer size <> Default buffer length ::: %d\n");
}
else
{
printf("Sending Buffer size = %d \n", iResult, fileoffset);
}
}
printf("Sending last Chunk", lastchunksize);
iResult = send(ClientSocket, bufferCMP + (fileoffset * DEFAULT_BUFLEN), lastchunksize, 0);
`
C++ Client (to be converted into C#)
int size = 8192;
int FileCounter = 0;
bool flg = true;
char * fileComplete;
char * filesizeBuffer;
FILE *temp;
int receiveBuffer = 0;
int desiredRecBuffer = size;
//int desiredRecBuffer = DEFAULT_BUFLEN ;
fileComplete = (char*)malloc(sizeof(char)* size);
while (desiredRecBuffer > 0)
{
iResult = recv(ConnectSocket, fileComplete + receiveBuffer, desiredRecBuffer, 0);
//iResult = recv( ClientSocket, fileComplete + receiveBuffer , fileSize , 0 );
if (iResult < 1)
{
printf("Reveive Buffer Error %d \n", WSAGetLastError());
}
else
{
receiveBuffer += iResult;
desiredRecBuffer = size - receiveBuffer;
printf("Reveived Data size : %d \n", desiredRecBuffer);
}
}
FILE *File = fopen("C:\\Users\\amirk_000\\Pictures\\img1b.png", "wb");
fwrite(fileComplete, 1, size, File);
//flg = true;
free(fileComplete);
fclose(File);
Full example of C# client socket is available at MSDN
Modify the given SocketSendReceive method to write the received buffer (bytesReceived array) to a file stream.
Something like the following should do it:
using (var file = File.OpenWrite("myimage.png"))
{
do
{
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
file.Write(bytesReceived, 0, bytes);
}
while (bytes > 0);
}
Related
I've set up TCP client in C# and server in C.
When I want to transfer file, some packets are lost and file is not saved properly.
I've compared PDF file I tried to transfer and half of packets were not saved, so I couldn't open it on another computer.
client code:
public void SendFile(string file, string destPath = "C:\\")
{
int bufferSize = 1024;
byte[] filebuff = new byte[bufferSize];
string fileName = destPath + file;
//send to TcpServer request to send file
stream.Write(textToBytes("RECEIVEFILE"), 0, 11);
try
{
FileStream streamFile = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader binReader = new BinaryReader(streamFile);
//send file name to TcpServer
stream.Write(textToBytes(file), 0, file.Length);
//get file size
long filesize = streamFile.Length;
//send file size to TcpServer
//sendData(stream, BitConverter.GetBytes(filesize));
//if file doesn't exist
if (file == null)
{
Console.WriteLine("Error.");
}
//if file is empty
if (filesize == 0)
{
Console.WriteLine("File size: 0");
return;
}
int totalLength = Convert.ToInt32(filesize);
Console.WriteLine("Totallength: " + totalLength);
long numberOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(streamFile.Length) / bufferSize));
int currentPacketLength;
for (int i = 0; i < numberOfPackets; i++)
{
if (filesize > bufferSize)
{
currentPacketLength = bufferSize;
totalLength = totalLength - currentPacketLength;
}
else
currentPacketLength = totalLength;
filebuff = new byte[currentPacketLength];
//streamFile.Read(filebuff, 0, currentPacketLength);
binReader.Read(filebuff, 0, currentPacketLength);
stream.Write(filebuff, 0, filebuff.Length);
}
streamFile.Close();
}
catch
{
Console.WriteLine("There's no file...");
}
}
public void DownloadFile(string fileName)
{
byte[] recBuffer = new byte[1024];
byte[] fileNameBytes;
long received = 0;
long receivedAll = 0;
byte[] fileData = new byte[1024];
stream.Write(textToBytes("SENDFILE"), 0, 8);
fileNameBytes = Encoding.UTF8.GetBytes(fileName);
stream.Write(fileNameBytes, 0, fileNameBytes.Length);
byte[] fileSizeBytes = new byte[4];
stream.Read(fileSizeBytes, 0, fileSizeBytes.Length);
int bytes = BitConverter.ToInt32(fileSizeBytes, 0);
Console.WriteLine("I'm downloading file...");
while (receivedAll < bytes)
{
received = stream.Read(fileData, 0, fileData.Length);
if (received < 0)
{
Console.WriteLine("Error");
break;
}
BinaryWriter bWrite = new BinaryWriter(File.Open("C:\\" + fileName, FileMode.Append));
bWrite.Write(fileData);
bWrite.Close();
receivedAll += received;
}
if(receivedAll == bytes)
{
Console.WriteLine("File downloaded successfuly.");
}
}
server code:
void ReceiveFile(int client_socket)
{
const int buffsize = 1024;
char buff[buffsize];
long filesize, received = 0, receivedall;
char filenamechar[512];
std::string filename, fullFilename;
memset(filenamechar, 0, 512);
/*
if(recv(client_socket, filenamechar, 512, 0) != 512)
{
printf("Error - filename.\n");
return;
}
fullFilename = "/Users/xxx/" + std::string(filenamechar);
*
if(recv(client_socket, &filesize, sizeof(long), 0) != sizeof(long))
{
printf("Child process: error.\n");
return;
}*/
filesize = 331776;
std::fstream fileFromClient;
fullFilename = "/Users/xxx/sockets.pdf";
fileFromClient.open(fullFilename, std::ios::binary | std::ios::out);
receivedall = 0;
while (receivedall < filesize)
{
memset(buff, 0, 1024);
received = recv(client_socket, buff, 1024, 0);
if(received <= 0)
{
std::cout << "error" << std::endl;
break;
}
receivedall += received;
fileFromClient << buff;
fputs(buff, stdout);
}
fileFromClient.close();
std::cout << "\nreceivedall: " << receivedall << std::endl;
std::cout << "filesize: " << filesize << std::endl;
if(receivedall != filesize)
printf("Error\n");
else
printf("File saved successfuly\n");
}
void SendFile(int client_socket)
{
char path[512];
char fileName[512];
char fullFileName[512];
long fileLen, sent, sentAll, read;
struct stat fileinfo;
FILE* file;
unsigned char bufor[1024];
memset(path, 0, 512);
strcpy(path, "/Users/xxxx/");
if (recv(client_socket, fileName, 512, 0) <= 0)
{
printf("Child process: error\n");
return;
}
strcpy(fullFileName, strcat(path, fileName));
printf("Child process: client wants file: %s\n", fullFileName);
if (stat(fullFileName, &fileinfo) < 0)
{
printf("Child process: can't get file info\n");
return;
}
if (fileinfo.st_size == 0)
{
printf("Child process: file size: 0\n");
return;
}
fileLen = fileinfo.st_size;
if (send(client_socket, &fileLen, sizeof(long), 0) != sizeof(long))
{
printf("Child process: error\n");
return;
}
sentAll = 0;
file = fopen(fullFileName, "rb");
if (file == NULL)
{
printf("Error\n");
return;
}
while (sentAll < fileLen)
{
read = fread(bufor, 1, 1024, file);
sent = send(client_socket, bufor, read, 0);
if (read != sent)
break;
sentAll += sent;
printf("Child process: sent %ld bytes\n", sentAll);
}
if (sentAll == fileLen)
printf("Child process: file sent successfuly\n");
else
printf("Child process: error\n");
fclose(file);
return;
}
How to guarantee that each packet will be saved properly?
One problem is with this line:
fileFromClient << buff;
This will write out the contents of buff until a zero byte is found. Since your are transferring a binary file, you can expect these bytes to be frequent. The rest of the received packet will not be written.
Rather than using the << operator, you should use
fileFromClient.write(buff, received);
Hi I wanted to proxy multicast video to unicast like udpxy does: http://www.udpxy.com, but in C#
since I could not find any suitable RTP library that I could use (they were eather too complex or I could't understand how to use them), I decided to port over one that udpxy uses rtp.c: https://github.com/pcherenkov/udpxy/blob/master/chipmunk/rtp.c
everything went fine (almost, as I didn't want to use pointers), until I wanted to translate RTP_Process to C#
RTP_Process in C
RTP_process( void** pbuf, size_t* len, int verify, FILE* log )
{
int rtp_padding = -1;
size_t front_skip = 0, back_skip = 0, pad_len = 0;
char* buf = NULL;
size_t pkt_len = 0;
assert( pbuf && len && log );
buf = *pbuf;
pkt_len = *len;
if( verify && !RTP_verify( buf, pkt_len, log ) )
return -1;
if( 0 != RTP_hdrlen( buf, pkt_len, &front_skip, log ) )
return -1;
rtp_padding = buf[0] & 0x20;
if( rtp_padding ) {
pad_len = buf[ pkt_len - 1 ];
}
back_skip += pad_len;
if( verify && (pkt_len < (front_skip + back_skip)) ) {
(void) tmfprintf( log, "RTP_process: invalid header "
"(skip [%lu] exceeds packet length [%lu])\n",
(u_long)(front_skip + back_skip), (u_long)pkt_len );
return -1;
}
/* adjust buffer/length to skip heading and padding */
/*
TRACE( (void)tmfprintf( log, "In: RTP buf=[%p] of [%lu] bytes, "
"fskip=[%ld], bskip=[%lu]\n",
(void*)buf, (u_long)pkt_len,
(u_long)front_skip, (u_long)back_skip ) );
*/
buf += front_skip;
pkt_len -= (front_skip + back_skip);
/*
TRACE( (void)tmfprintf( log, "Out RTP buf=[%p] of [%lu] bytes\n",
(void*)buf, (u_long)pkt_len ) );
*/
*pbuf = buf;
*len = pkt_len;
return 0;
}
RTP_Process in C#
public byte[] RTP_process(int verify)
{
/* process RTP package to retrieve the payload: set
* pbuf to the start of the payload area; set len to
* be equal payload's length
*
* #param pbuf address of pointer to beginning of RTP packet
* #param len pointer to RTP packet's length
* #param verify verify that it is an RTP packet if != 0
* #param log log file
*
* #return 0 if there was no error, -1 otherwise;
* set pbuf to point to beginning of payload and len
* be payload size in bytes
*/
int rtp_padding = -1;
int front_skip = 0, back_skip = 0, pad_len = 0;
int pkt_len = 0;
//assert(pbuf && len && log);
//buf = *pbuf;
pbuf = buf;
//pkt_len = *len;
len = pkt_len;
/*
if (verify != 1 && RTP_verify() != 1)
RTPOK = - 1;
if (0 != RTP_hdrlen(buf, pkt_len, front_skip)) //?????
RTPOK = - 1;
*/
rtp_padding = buf[0] & 0x20;
if (rtp_padding != -1) //???????
{
pad_len = buf[pkt_len - 1];
}
back_skip += pad_len;
if ((verify != -1) && (pkt_len < (front_skip + back_skip))) //???????
{
Console.WriteLine("RTP_process: invalid header (skip {0} exceeds packet length {1})\n", (long)(front_skip + back_skip), (long)pkt_len);
RTPOK = - 1;
}
/* adjust buffer/length to skip heading and padding */
/*
TRACE( (void)tmfprintf( log, "In: RTP buf=[%p] of [%lu] bytes, "
"fskip=[%ld], bskip=[%lu]\n",
(void*)buf, (u_long)pkt_len,
(u_long)front_skip, (u_long)back_skip ) );
*/
//buf += front_skip;
//pkt_len -= (front_skip + back_skip);
/*
TRACE( (void)tmfprintf( log, "Out RTP buf=[%p] of [%lu] bytes\n",
(void*)buf, (u_long)pkt_len ) );
*/
pbuf = buf;
len = pkt_len;
RTPOK = 0;
return pbuf;
}
here the problems started
1. buf += front_skip; complained that operator += cannot be applied to operands of type byte[] and int
then why did it work in RTP_Process in C and what is a C# equivalent of that
2. in
if (rtp_padding != -1) //???????
{
pad_len = buf[pkt_len - 1]; //There is an exeption trown: System.IndexOutOfRangeException: Index was outside the bounds of the array.
its clear that I interpreted and translated something the wrong way, but the onlything I would like to do is to get MPEG-TS frame out of RTP stream to then forward it to a TCP socket, so if anyone can suggest a better way of doing that I would love to hear it
Thanks for Anwsering and Best Regards
}
First, I suggest to read carefully RFC-3550, it has all information about RTP-packet structure (mostly you need Section #5: RTP Fixed Header and extensions).
Then you have to implement RTP_hdrlen to calculate RTP header size, it must return front_skip value as RTP header size including extensions. So, you don't have to use buf += front_skip;, RTP payload starts from byte buf[front_skip].
You have wrong packet length parameter here: int pkt_len = 0;, that's why the exception is thrown here pad_len = buf[pkt_len - 1];.
im trying to send file over a socket,but i would like also to send the file's name for example.
this is the send code:
int count = 0;
int size;
private int SendVarData(Socket s, byte[] data)
{
total = 0;
int size = data.Length;
int dataleft = size;
int sent;
datasize = BitConverter.GetBytes(size);
sent = s.Send(datasize);
sent = s.Send(data, total, dataleft, SocketFlags.None);
total += sent;
dataleft -= sent;
// MessageBox.Show("D");
return total;
}
this is the call
byte[] arr = File.ReadAllBytes(file);
SendVarData(sc, arr);//sc is a socket ofcourse.
i would like to send also the file name as i said. should i send it once before the file send itself? or there's another efficient way to send both?
this is the receive code:
private byte[] ReceiveVarData(Socket s)
{
int total = 0;
int recv;
byte[] datasize = new byte[4];
recv = s.Receive(datasize, 0, 4, 0);
size = BitConverter.ToInt32(datasize, 0);
int dataleft = size;
//MessageBox.Show(size.ToString());
byte[] data = new byte[size];
while (total < size)
{
recv = s.Receive(data, total, dataleft, 0);
if (recv == 0)
{
break;
}
total += recv;
dataleft -= recv;
}
return data;
}
any help would be apperciated.
I don't think it matters whether you send it before or after the file. You need a protocol, e.g., first send length of file name, then file name, then length of file and file itself, or other way around. On the receiving side you can differentiate the messages now-because you know length of each chunk (assuming the two length variables are always 4 byte integers say).
If I send plain text there is no problem. Everything is ok.
However If I try to send from the C# client an image, the server receives correct bytes number, but when I save the buffer to a file (in binary mode - wb), it always has 4 bytes.
I send it by the C# client by using the function File.ReadAllBytes().
My saving code looks like
FILE * pFile;
char *buf = ReceiveMessage(s);
pFile = fopen (fileName , "wb");
fwrite(buf, sizeof(buf[0]), sizeof(buf)/sizeof(buf[0]), pFile);
fclose (pFile);
free(buf);
My receiving function looks like
static unsigned char *ReceiveMessage(int s)
{
int prefix;
recv(s, &prefix, 4, 0);
int len = prefix;
char *buffer= (char*)malloc(len + 1);
int received = 0, totalReceived = 0;
buffer[len] = '\0';
while (totalReceived < len)
{
if (len - totalReceived > BUFFER_SIZE)
{
received = recv(s, buffer + totalReceived, BUFFER_SIZE, 0);
}
else
{
received = recv(s, buffer + totalReceived, len - totalReceived, 0);
}
totalReceived += received;
}
return buffer;
}
Your C code needs to pass len back from the ReceiveMessage() function.
char *buf = ReceiveMessage(s); // buf is a char*
... sizeof(buff) // sizeof(char*) is 4 or 8
So you'll need something like
static unsigned char *ReceiveMessage(int s, int* lenOut)
{
...
*lenOut = totalReceived ;
}
You do a beginners mistake of using sizeof(buf). It doesn't return the number of bytes in the buffer but the size of the pointer (which is four or eight depending on if you run 32 or 64 bit platform).
You need to change the ReceiveMessage function to also "return" the size of the received data.
You do not get size of array by sizeof. Change to i.e.:
int len = 0;
char *buf;
buf = ReceiveMessage(s, &len);
/* then use len to calculate write length */
static unsigned char *ReceiveMessage(int s, int *len)
/* or return len and pass ptr to buf */
{
...
}
I have a simple file transfer app written in c# using TCP to send the data.
This is how I send files:
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] fileName = Encoding.UTF8.GetBytes(fName); //file name
byte[] fileData = new byte[1000*1024];
byte[] fileNameLen = BitConverter.GetBytes(fileName.Length); //length of file name
FileStream fs = new FileStream(textBox1.Text, FileMode.Open);
try
{
clientData = new byte[4 + fileName.Length];
}
catch (OutOfMemoryException exc)
{
MessageBox.Show("Out of memory");
return;
}
fileNameLen.CopyTo(clientData, 0);
fileName.CopyTo(clientData, 4);
clientSock.Connect("172.16.12.91", 9050);
clientSock.Send(clientData, 0, clientData.Length, SocketFlags.None);
progressBar1.Maximum = (int)fs.Length;
while (true)
{
int index = 0;
while (index < fs.Length)
{
int bytesRead = fs.Read(fileData, index, fileData.Length - index);
if (bytesRead == 0)
{
break;
}
index += bytesRead;
}
if (index != 0)
{
clientSock.Send(fileData, index, SocketFlags.None);
if ((progressBar1.Value + (1024 * 1000)) > fs.Length)
{
progressBar1.Value += ((int)fs.Length - progressBar1.Value);
}
else
progressBar1.Value += (1024 * 1000);
}
if (index != fileData.Length)
{
progressBar1.Value = 0;
clientSock.Close();
fs.Close();
break;
}
}
}
In taskmanager,the Release version of this app's memory usage goes 13 MB when I use the OpenFileDialog, then goes up to 16 MB when it sends data then stays there. Is there anything I can do to reduce the memory usage? Or is there a good tool that I can use to monitor the total allocation memory in the app?
And while we're there, is 16 MB really that high?
16MB doesn't sound that much of memory usage.
You could use the inbuilt visual studio profiler to see what's costing the most performance. See link below for more information about the profiler:
http://blogs.msdn.com/b/profiler/archive/2009/06/10/write-faster-code-with-vs-2010-profiler.aspx
I noticed that when I minimize any app, the memory usage goes down quite significantly. I eventually looked for a way to replicate this effect programmatically and this is what I found:
[DllImport("kernel32.dll")]
public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);
public void ReleaseMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
I don't know the cons in using this but so far, it managed to save at least 13MB of memory.