mig16

ok,, i have the next problem... i am creating a client server program ,,, i allready know how to send text trough tcp with a networkstream.. then a streamreader and a streamwriter... it works good with text... but the problem is when i try to send a byte [] ,,, i have a image and i use a memorystream to convert it to a byte [] and i want to create the image in the server when i got he image,, i allready know how to do that... what i dont know to do,, is how can i send a byte []


im trying to send the image this way,, when i allready have the byte [] i do this:

//here i call a function that returns me a byte[] of the image
byte[] content = ReadBitmap2ByteArray(img);
//now i send it to my server
                        streamWriter.Write(content);

i dont get compilation errores.. but thr problem is when reading it,, i try to do this:
//i get an error when trying to read it this way
byte [] img = reader.Read();
//i also get an error if i try to convert it
byte [] img = (byte[])reader.Read();

so i think i dont know how to send a byte [] :P hehe.. hope somebody can helps

thx

mig16


Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

shakalama

hi

streamreader or streamwriters uses the networkstream, the networkstream using byte arrays , where stream reader or writer convert it to characters , so why don't you use the networkstream in first place instread of conversion

hope this helps






Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

mig16

hi , thx for the answer , could u pls give me some code i cant do it.. i dont know why ... :s

thx

mig16




Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

shakalama

hi,

right after you creat your sockets you do something like this in your client part


//data holders

byte[] data = new byte[1024];
int recv;

NetworkStream ns = new NetworkStream(srvrSkt);
//start to recieve and send messages

if (ns.CanRead)
{
recv = ns.Read(data, 0, data.Length);
strdata =
Encoding.ASCII.GetString(data);
Console.WriteLine(strdata);
}
else

{
Console.WriteLine("stream can't read ...");
ns.Close();
srvrSkt.Close();
return;
}


and in your server part you replace the read ability to write something like this


if (ns.CanWrite)
{
ns.Write(
data, 0, data.Length);
ns.Flush();
}


you can use any byte array instead of data byte array, anyway i guess hte best place to ask about teh networking programming in this forum

http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=40&SiteID=1

hope this helps






Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

mig16

thx man :D it worked when sending it ;) .. now i have a probelm recieving it... look.. what im trybng to do is the next... on the client i take a screen shot,, and from that screen shot i get an image and convert it to a byte [] ,, now i send it and every thig is fine when i do it like u said,, but when i receive it i try this:

int recv = 0;
byte[] data = new byte[1024];
writer.WriteLine("GETIMAGES");
recv = stream.Read(data, 0, data.Length);

MemoryStream streamImage = new MemoryStream(data, true);
streamImage.Write(data, 0, data.Length);
Bitmap bmp = new Bitmap(streamImage);
picImage.Image = bmp;

stream.Close();

but i just receive a pice of the image... i imagine that this could be fixed with a loop.. but i have no clue of where to use the loop ... do u know




Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

shakalama

hi,

man the data array is just 1024 byte that could be the problem , then you can make your sender send 2 messages 1 for the size and the second for the data

and your reciever will recieve the data size first , and creat array with that size and loop till you fill the entire array

i have example for that but i use the socket to send and recieve data you can change the socket in the code to networkstream, send and recive to read and write


private static int SendVarData(Socket skt, byte[] buffer)
{
int size = buffer.Length;
int leftData = size;
int totalSent = 0;
int sent;
//new part

//send the data size b4 sending data

byte[] datasize = new byte[4];
datasize =
BitConverter.GetBytes(size);
skt.Send(datasize);
while (totalSent<size)
{
sent = skt.Send(buffer, totalSent, leftData,
SocketFlags.None);
totalSent += sent;
leftData -= sent;
}
return totalSent;
}
public static byte[] RecieveVarData(Socket skt)
{
int recv;
int totaldata = 0;

//get the predefined datasize

byte[] datasize = new byte[4];
skt.Receive(datasize, 0, 4,
SocketFlags.None);
int size = BitConverter.ToInt32(datasize, 0);
int leftdata = size;
byte[] buffer = new byte[size];
while (totaldata<size)
{
recv = skt.Receive(buffer, totaldata, leftdata,
SocketFlags.None);
if (recv == 0)
{
buffer =
Encoding.ASCII.GetBytes("exit");
break;
}
totaldata += recv;
leftdata -= recv;
}
return buffer;
}

 

//you can call those methods like that

int sent = SendVarData(Clientskt, data);

Byte[] recvdata = RecieveVarData(Clientskt);


 

also in your case i would prefer to use generics instead of fixed size array

hope this helps






Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

mig16

thanks man . that worked me for send images from server to client. but now i need to send from client to server so i did the next,,, whe my client recieves the order to send an image makes this:

//this is a custom class for getting the screenshot
Image img = CaptureScreen.CaptureScreen.GetDesktopImage();
//this a function where i get the image to a byte []
                        byte[] content = ReadBitmap2ByteArray(img);
//i first send the lenght of the byte[]
                        streamWriter.WriteLine(content.Length.ToString());
streamWriter.Flush();
//then i use the network stream for send the byte []
                        networkStream.Write(content, 0, content.Length);
                        networkStream.Flush();

and this is how i receive it:

try
            {
                int recv = 0;
//here i 1st read the lenght of the byte []
                string readed = reader.ReadLine();
                int x = 0;
                try
                {
//i parse the string to an int
                     x = int.Parse(readed);
                }
                catch (Exception)
                {
                }
//create my byte with the lenght of the byte that was sent to me
                byte[] data = new byte[x];
//read the byte[]
                recv = stream.Read(data, 0, data.Length);
//create a  memorystream for store the byte[]
                MemoryStream streamImage = new MemoryStream(data, true);
//weite my stream with my byte []
                streamImage.Write(data, 0, data.Length);
//create a bitmap
                Bitmap bmp = new Bitmap(streamImage);
//add my image to a picturebox
                picImage.Image = bmp;
                streamImage.Flush();
                streamImage.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

ok,, the funny thing is that this works perfect when i do it on localhost...but when i run it online i start getting an excpetion every time i receive an image after i received 10 or 11 images... i dont know why :(

i think i have a clue of why this happens... i added a messagebox each time i received an image,, and it showed me up the byte [] lenght... the 1st image was 59 000 or so... the next 80000 ,, and it started growing till i got this error.. so i think is that im not closing somethign and the bytes are growing.. (i might tell u that this was using the same image)  ... but i dont have a clue whats wrong

thx

mig16




Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

shakalama

hi,

thats nice at least to hear it worked but i want to tell you somethings

1) i don't know what was the exception message that you got and its related to what

2) what will work for client will work for server but you just arrange the steps who send and who reciever or other wise you will get a deadlock

server.send >> must find >> client.read in the other side

client.send >> must find on the other side >> server.read

2) i don't know much about networking you can ask your questions in this forum http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=40&SiteID=1 

hope this helps






Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

mig16

hi ppl. i have a problem with sending a byte [] the problem is that i have a client and a server, i need that the client sends an image to the server so i get an image convert it to byte [] and when i receive it on the server i create a memorystream and convert the byte [] received to an image,, this works fine but just with the 1st image,, when i try to retreive another the server freeze and also the client... it does not get any exceptions but stop working ,, heres the code

when the server ask me to send an image i do the next

//create a byte []
byte[] imageContent = null;

//with a class i take a screenshot of the client scren
Image img = CaptureScreen.CaptureScreen.GetDesktopImage();
//with a function that i will post after this i get the image into my byte []
imageContent = ReadBitmap2ByteArray(img);
//with a function i send the byte []
SendImage(imageContent);

//this method gets the image into a byte []
static byte[] ReadBitmap2ByteArray(Image image)
{
//declare memory stream
MemoryStream stream = new MemoryStream();

//write the image
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Flush();
stream.Close();
//returns the array
return stream.ToArray();
}

//this is the way i send it

static private void SendImage(byte[] imageContent)
{
//i get the byte [] lenght
int size = imageContent.Length;

//create a streamwriter
StreamWriter wr = new StreamWriter(ns);
wr.AutoFlush = true;
//befoire sending the byte[] i send the byte [] lenght
wr.WriteLine(size.ToString());
wr.Close();
//now u send the byte []
server.Send(imageContent, 0, imageContent.Length,SocketFlags.None);
imageContent = null;
}


in the server i receive it this way:

string stringData = null;
int recv = 0;
byte[] data = new byte[1024];
MemoryStream streamImage = null;

//i receive the size of the byte[]
recv = socket.Receive(data, 0, data.Length,SocketFlags.None);
//parse it to a string
stringData = Encoding.ASCII.GetString(data, 0, recv);

//now my data [] (wich will receive the byte [] i resize the lenght to the lenght i just received
data = new byte[Convert.ToInt32(stringData)];

//i receive the byte[]
recv = socket.Receive(data);
//write the image
streamImage = new MemoryStream(data, true);
streamImage.Write(data, 0, recv);

//i set the a picturebox image with the streamImage
picImage.Image = Image.FromStream(streamImage);
streamImage.Flush();
streamImage.Close();


ok,,that works fine but just with the first image.. does anybody have a clue




Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

shakalama

hi,

yes it will act like that because you didn't close your streams nor your sockets , and you didn't send anything else you can send data in a loop statment like that i assume you have image1,image2...... image5

for(int i = 0; i<5;i++)

{

//send image size

//send image

//recieve finished message

}

on the other side

for(int i = 0; i<5;i++)

{

//recieve image size

//recieve image

//send finished message

}

hope this helps






Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

shakalama

i thought you said after 11th picture you get a problem

 

anyway i will merge your previous thread with this one

best regards






Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

mig16

ok,, i solved my problem.. the problem was that i was sendign with networkstream and receiving with socket.. i changed evey to send and receive with sockets and worked,,, now the problem that i have is that i can not connect via internet... i just can do it on LAN or local... but when i try to do it online it dont do it, and dont get exeptions... im trying this to connect

private static void Connect()
{
string ip = "201.135.199.242";
int port = 3562;
bool connected = false;
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ip), port);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
while (connected == false)
{
try
{
server.Connect(ipep);
connected = true;
//a method for start receiving data when the server is connected
Receive();
}
catch (Exception)
{
}
}
}

i make a while because i dont know when the server will be running and this way,, so when i start listening on the server the client connect,, this works fine on localhost and LAN but when i try it with my own ip .. but the external it wont work.. it goes to line server.Connect(ipep); and it breaks,,, the debugger doesnt get any excpetion,, it just breaks,,, and dont do the while,,, i dont know why this dont work... i allready made before client server application with my computer and they worked with the externla ip... this is the way i listen on the server:

private void Listen()
{
int port = 3562:
TcpListener listener = null;
listener = new TcpListener(IPAddress.Any,port);
try
{
listener.Start();
socket = listener.AcceptSocket();
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message);
}
}

so here u just wait for a connection.. so i dont know whats wrong with my code




Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

Mike Flasko

Have you verified via a network monitor that your connect request is making it from the client to the server (ie. the TCP SYN packet) See blogs.msdn.com/dgorti for instructions regardng how to use a network monitor. If the connect request is not making it to the server then some intermediary device or host firewall is blocking the connect request.




Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

JonCole

Sounds like you are behind a NAT or a Firewall. Why don't you read up on these two topics and see if that could be what is blocking you.




Re: .NET Framework Networking and Communication send byte[] and recive byte[] System.IO;

fafnir

Another important thing that you are obviously forgetting about is

bitmap.Dispose();

or

image.Dispose();

Otherwise, you'll quickly run out of CPU memory (memory leak).

Believe me, I had it, and I had it BAD.

Besides, if your server stops responding WITHOUT any exceptions, and keeps hanging until you reboot it, that's EXACTLY what I had until I started disposing of all images.

Pls, let me know if this helped.