|
-
March 21st, 2010, 07:42 AM
#1
[RESOLVED] Sending DateTime information with image
Hello everyone,
I want to extend my program by sending a DateTime object alongside an image object AT THE SAME TIME. Below is the working code I have thus far:
Code:
//Sends the image using a TcpClient
//I want to extend this by sending a DateTime object along side the image
public void Connect(String server, Image img)
{
try
{
//Create TcpClient
string ownPort = txtOwnPort.Text;
int port = Convert.ToInt32(ownPort);
TcpClient client = new TcpClient(server, port);
Byte[] bytes = null;
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
bytes = ms.ToArray();
NetworkStream stream = client.GetStream();
//Get current time
DateTime currentTime = DateTime.Now;
stream.Write(bytes, 0, bytes.Length);
Console.WriteLine("Image sent at {0}.", currentTime.TimeOfDay);
Console.WriteLine("Sent {0} bytes.", bytes.Length);
//Close everything
stream.Close();
client.Close();
}
catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch(SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
}
//Listen for incoming packets
//I want to extend this so it can pick up the image and the DateTime object
public void Listen()
{
TcpListener server = null;
try
{
string ownPort = txtOwnPort.Text;
int port = Convert.ToInt32(ownPort);
IPAddress localAddr = IPAddress.Parse(txtOwnIP.Text);
server = new TcpListener(localAddr, port);
//Start listening for client requests
server.Start();
//Buffer for reading data
Byte[] bytes = new Byte[32768];
Image img;
//Enter the listening loop
while(true)
{
//Perform a blocking call to accept requests
TcpClient client = server.AcceptTcpClient();
img = null;
//Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
//Loop to receive all the data sent by the client
while((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
DateTime currentTime = DateTime.Now;
Console.WriteLine("Recieved {0} bytes at {1}", bytes.Length, currentTime.TimeOfDay);
//DateTime delayTime = sentTime.Millisecond - currentTime.Millisecond;
//Convert byte array to image
img = byteArrayToImage(bytes);
//If image is not null, display it
if(img != null)
{
//Display returned image
pictureBox1.Image = img;
Console.WriteLine("Recieved image.");
}
}
//Close connection
client.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
//Stop listening for clients
server.Stop();
}
}
//Convert recieved byte array to an image
public Image byteArrayToImage(Byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
try
{
Image returnImage = Image.FromStream(ms);
return returnImage;
}
catch (Exception e)
{
Console.WriteLine("{0}", e);
//Return a null image if exception is thrown
Bitmap returnImage = null;
return returnImage;
}
}
My main problem is I'm not sure how to send the image and DateTime together in a single stream.write (if that is even possible).
Im not asking anyone to write the whole thing for me, I would just like come pointers in the write direction.
Don't let me down!
Many thanks in advance,
bassguru
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|