How to receive continuous images using UDP C# - c#

I am trying to develop an video chat application via UDP C#. I have an picture box that shows the webcam of the user continuously and sending to a specific port. But I don't know how to receive the Image
I am using a byte to image / image to byte converter class (which normally works fine) but when I put this code in a timer sequence I get this error
'A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself'
here is my code :
private void timer1_Tick(object sender, EventArgs e)
{
Image bitmap = pictureBox1.Image;
byte[] sndcam = imageToByteArray(bitmap);
string Ip = "127.0.0.1";
int port = 1111;
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(Ip), port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.SendTo(sndcam, ep);
}
I've seen few questions like this on Stackoverflow but I cloudn't find a satisfying answer
Should I use RTP instead ? If so , How ?
Thanks in advance

Related

Select printer tray

I have a Question about printing files via a Socket Connection.
Here is m Simple Code so send a file via socket to a IP Printer ->
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.NoDelay = true;
IPAddress ip = IPAddress.Parse("192.168.192.6");
IPEndPoint ipep = new IPEndPoint(ip, 9100);
clientSocket.Connect(ipep);
byte[] fileBytes = File.ReadAllBytes("test.txt");
clientSocket.Send(fileBytes);
clientSocket.Close();
The Problem:
We need to print invoices on a different paper. This paper is on the second printer tray.
Example:
IP1 need to print on tray 1
IP2 need to print on tray 3
Is there a option so send a file via socket connection to print a file on the second printer tray?

Send a stream obj through UDP client with Avro framework in C#

I am trying to send a stream through UDP socket. The "SendTo" takes byte[] buffer argument. Not sure how to do this if I have a stream object (buffer). Please help! Thanks. The ByteBufferOutputStream does not seem to have a funciton to convert the stream to bytes.
ByteBufferOutputStream buffer = new ByteBufferOutputStream();
Avro.IO.Encoder ej = new BinaryEncoder(buffer);
ej.WriteInt(Convert.ToInt32(testEvent["schemaId"]));
var dwrd = new DefaultWriter(schema);
dwrd.Write<GenericRecord>(testEvent, ej);
buffer.Flush();
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp);
IPAddress serverAddr = IPAddress.Parse("192.168.1.1");
IPEndPoint endPoint = new IPEndPoint(serverAddr, 2190);
clientSocket.SendTo(buffer, endPoint);
Actually it does. ByteBufferOutputStream has a method named GetBufferList, which returns an
IEnumerable of System.IO.MemoryStream. You can take these MemoryStreams, and concatenate them to a single buffer, with an integer header specifying how many streams are there (denoted by X), followed by X more integers specifying the length of the ordered streams, followed by the streams themselves.
You can send that entire buffer via UDP to your server, where it will reconstruct the streams, and
use ByteBufferInputStream (from Avro) which has a constructor that accepts them.

How to send JPG over TCP from one Windows Phone to another? I already managed sending text

How to send JPG over TCP from one Windows Phone to another?
I found that in the other SO topic(below) showing how to send text over TCP, but how to turn JPG to bytes and send it? Most of jpges are bigger than 4kb how to deal with that?:
private void sendMessage() {
connectArgs = new SocketAsyncEventArgs { RemoteEndPoint = new DnsEndPoint(localIP, Int32.Parse(port)) };
connectArgs.Completed += connectArgs_Completed;
connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connection.ConnectAsync(connectArgs);
}
void connectArgs_Completed(object sender, SocketAsyncEventArgs e) {
if (e.SocketError == SocketError.Success && firstLoop) {
firstLoop = false;
var sendArgs = new SocketAsyncEventArgs();
var buffer = Encoding.UTF8.GetBytes("MESSAGE STRING" + Environment.NewLine);
sendArgs.SetBuffer(buffer, 0, buffer.Length);
sendArgs.Completed += sendArgs_Completed;
e.ConnectSocket.SendAsync(sendArgs);
} else {
//blad
}
}
You should probably read the raw image data and send that, along with a special "code" or prefix, and then on the receiving end, save the raw image data to a .jpg file and display that. You can do that with any file.
TCP doesn't limit the size of the packets to 4KB, so that shouldn't be a problem for you. You simply need to read all the image bytes either from file using File.ReadAllBytes or a stream reader, and you then send those bytes across to the receiver.
Alternatively, if you have the image as an object, you could use Marshal.StructureToPtr on the sending end and then use Marshal.PtrToStructure to re-create the image object from the bytes you have received, if you want to show the image directly for example.

UDP client bug - Unable to send data bytes

I am writting a C# P2P Video Chat (Part of my exam on Faculty),a i am little stuck at send Data via udp. So here how it works. I have a Web_Capture library, and every time image is captured it sets PictureBox image to the captured one
private void webCamCapture_ImageCaptured(object source, WebCam_Capture.WebcamEventArgs e)
{
myCamera.Image = e.WebCamImage;
sendData(ref ipep2); // send it immediately
}
So then the sendData method starts sending...
private void sendData(ref IPEndPoint sender)
{
byte[] data;
if (friendsClient == null)
{
friendsClient = new UdpClient();
}
MemoryStream myStream = new MemoryStream();
myCamera.Image.Save(myStream, System.Drawing.Imaging.ImageFormat.Jpeg);
data = myStream.GetBuffer();
friendsClient.Send(data, data.Length,sender);
}
When i debug, the socket exception pops out:
System.Net.Sockets.SocketException was unhandled by user code
Message=The requested address is not valid in its context
Source=System
ErrorCode=10049
NativeErrorCode=10049
So, does anyone have any idea, i will be thankfull if he support that idea with code, because i am nooby at c# :) thanks in advance. Marjan
You should specify reciever's ip and port, Here is a complete example
so you should change your implementation to
private void sendData(ref IPEndPoint reciever)
{
byte[] data;
Socket sending_socket = new Socket(AddressFamily.InterNetwork, ocketType.Dgram, ProtocolType.Udp);
MemoryStream myStream = new MemoryStream();
myCamera.Image.Save(myStream, System.Drawing.Imaging.ImageFormat.Jpeg);
data = myStream.GetBuffer();
sending_socket.SendTo(data, reciever);
}

Cannot receive Image over TCP socket?

I have problem with receiving an image over TCP socket [.net 4.0]
Server:
Socket s = null;
Socket client;
private void button1_Click(object sender, EventArgs e)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(new IPEndPoint(IPAddress.Any, 9988));
s.Listen(1);
client = s.Accept();
pictureBox1.Image = Image.FromStream(new NetworkStream(client));
//Server freezes here and waiting for the image .. but in the Client side.. it tells that it sent.
Console.WriteLine("Received.");
}
Client:
Socket s = null;
private void button1_Click(object sender, EventArgs e)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9988));
Rectangle bounds = Screen.GetBounds(Point.Empty);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
bitmap.Save(new NetworkStream(s), ImageFormat.Png);
Console.WriteLine("sent.");
}
Edit:
im making a big application .. image was receiving just fine .. then i did some changes on the code so it got complicated to know what did i exactly change .. now it's not working .. so i made new projects and tried the code up.. still doesn't work .. i know that there's another ways to do it.. but i prefer to do this way.
Anyone knows how to fix it ??
i think you need to convert the image to byte and then get the byte's size and send it to the server, the server prepares the buffer size and then the client send the image's bytes, you can find s video on how to do it Right Here
Most probably you need to close the socket after sending the data.
Image.FromStream() probably waits until the NetworkStream indicates there are no more bytes to process, but since you declared the Socket on the form's class level, it stays connected and the server waits for more data.

Categories

Resources