Java Video Chat applicataion input output stream not workingHow does the Java 'for each' loop work?Streaming video from Android camera to serverGetting the Current Working Directory in Javaadd KeyListener to JLabelJava ImageIO.write sends more data than ImageIO.read receives?Send a stream of images using ImageIO?Saving an image with transparent background to webdav gives white backgroundDrawing an image in JScrollPane within scaleWorking on a java based chatting application using threadingHow to Convert a Java 8 Stream to an Array?

Was Mohammed the most popular first name for boys born in Berlin in 2018?

Libertine font numbers have a different height than text

How to handle DM constantly stealing everything from sleeping characters?

"Estrontium" on poster

how to find out if there's files in a folder and exit accordingly (in KSH)

Add Columns to .csv from Multiple Files

What happens when the drag force exceeds the weight of an object falling into earth?

Do Monks gain the 9th level Unarmored Movement benefit when wearing armor or using a shield?

Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019

Has there been evidence of any other gods?

A Latin text with dependency tree

Examples where existence is harder than evaluation

Are wands in any sort of book going to be too much like Harry Potter?

How to avoid making self and former employee look bad when reporting on fixing former employee's work?

Gift for mentor after his thesis defense?

Locked my sa user out

Why is it wrong to *implement* myself a known, published, widely believed to be secure crypto algorithm?

How did Captain Marvel know where to find these characters?

Fee negotiations in Lightning

What are these round pads on the bottom of a PCB?

Are double contractions formal? Eg: "couldn't've" for "could not have"

What is a good way to allow only one non null field in an object

What's an appropriate age to involve kids in life changing decisions?

How do I minimise waste on a flight?



Java Video Chat applicataion input output stream not working


How does the Java 'for each' loop work?Streaming video from Android camera to serverGetting the Current Working Directory in Javaadd KeyListener to JLabelJava ImageIO.write sends more data than ImageIO.read receives?Send a stream of images using ImageIO?Saving an image with transparent background to webdav gives white backgroundDrawing an image in JScrollPane within scaleWorking on a java based chatting application using threadingHow to Convert a Java 8 Stream to an Array?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am making a video chat app which uses java networking (aka. sockets) to send images of the webcam to another client.



My code sends first the length of the buffered image data then the actual data. The Server also reads first a int then the data itself. The first image worked but after it, the data input stream read a negative number as the length.



Server side:



frame = new JFrame();
while (true)
try

length = input.readInt();
System.out.println(length);
imgbytes = new byte[length];
input.read(imgbytes);
imginput = new ByteArrayInputStream(imgbytes);
img = ImageIO.read(imginput);
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);


catch(IOException e)
e.printStackTrace();




Client side:



while(true) 
try

currentimg = webcam.getImage();
ImageIO.write(currentimg, "jpg", imgoutputstream);
imgbytes = imgoutputstream.toByteArray();
out.writeInt(imgbytes.length);
out.write(imgbytes);

catch (IOException e)
e.printStackTrace();











share|improve this question
























  • You are running into an infinite loop without a breaking condition at the client side. What will make you break from the inner while loop to read a new image from the webcam inside the outer loop?

    – Islam El-Rougy
    Mar 23 at 8:17











  • I corrected the error but still it doesn't work. I added a debug System.out.println() in the Server side to print the length and here is the output 4465 8917 13388 17869 22323 26786 31233 35694 40168 44637 49116 53596 58062 62546 67038 -657982985 Exception in thread "main" java.lang.NegativeArraySizeException at ServerSide.ServerWorker.run(ServerWorker.java:39) at ServerSide.Server.<init>(Server.java:42) at ServerSide.ServerMain.main(ServerMain.java:7)

    – Clement Hui
    Mar 23 at 8:21


















1















I am making a video chat app which uses java networking (aka. sockets) to send images of the webcam to another client.



My code sends first the length of the buffered image data then the actual data. The Server also reads first a int then the data itself. The first image worked but after it, the data input stream read a negative number as the length.



Server side:



frame = new JFrame();
while (true)
try

length = input.readInt();
System.out.println(length);
imgbytes = new byte[length];
input.read(imgbytes);
imginput = new ByteArrayInputStream(imgbytes);
img = ImageIO.read(imginput);
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);


catch(IOException e)
e.printStackTrace();




Client side:



while(true) 
try

currentimg = webcam.getImage();
ImageIO.write(currentimg, "jpg", imgoutputstream);
imgbytes = imgoutputstream.toByteArray();
out.writeInt(imgbytes.length);
out.write(imgbytes);

catch (IOException e)
e.printStackTrace();











share|improve this question
























  • You are running into an infinite loop without a breaking condition at the client side. What will make you break from the inner while loop to read a new image from the webcam inside the outer loop?

    – Islam El-Rougy
    Mar 23 at 8:17











  • I corrected the error but still it doesn't work. I added a debug System.out.println() in the Server side to print the length and here is the output 4465 8917 13388 17869 22323 26786 31233 35694 40168 44637 49116 53596 58062 62546 67038 -657982985 Exception in thread "main" java.lang.NegativeArraySizeException at ServerSide.ServerWorker.run(ServerWorker.java:39) at ServerSide.Server.<init>(Server.java:42) at ServerSide.ServerMain.main(ServerMain.java:7)

    – Clement Hui
    Mar 23 at 8:21














1












1








1








I am making a video chat app which uses java networking (aka. sockets) to send images of the webcam to another client.



My code sends first the length of the buffered image data then the actual data. The Server also reads first a int then the data itself. The first image worked but after it, the data input stream read a negative number as the length.



Server side:



frame = new JFrame();
while (true)
try

length = input.readInt();
System.out.println(length);
imgbytes = new byte[length];
input.read(imgbytes);
imginput = new ByteArrayInputStream(imgbytes);
img = ImageIO.read(imginput);
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);


catch(IOException e)
e.printStackTrace();




Client side:



while(true) 
try

currentimg = webcam.getImage();
ImageIO.write(currentimg, "jpg", imgoutputstream);
imgbytes = imgoutputstream.toByteArray();
out.writeInt(imgbytes.length);
out.write(imgbytes);

catch (IOException e)
e.printStackTrace();











share|improve this question
















I am making a video chat app which uses java networking (aka. sockets) to send images of the webcam to another client.



My code sends first the length of the buffered image data then the actual data. The Server also reads first a int then the data itself. The first image worked but after it, the data input stream read a negative number as the length.



Server side:



frame = new JFrame();
while (true)
try

length = input.readInt();
System.out.println(length);
imgbytes = new byte[length];
input.read(imgbytes);
imginput = new ByteArrayInputStream(imgbytes);
img = ImageIO.read(imginput);
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);


catch(IOException e)
e.printStackTrace();




Client side:



while(true) 
try

currentimg = webcam.getImage();
ImageIO.write(currentimg, "jpg", imgoutputstream);
imgbytes = imgoutputstream.toByteArray();
out.writeInt(imgbytes.length);
out.write(imgbytes);

catch (IOException e)
e.printStackTrace();








java networking video datainputstream dataoutputstream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 8:24







Clement Hui

















asked Mar 23 at 8:01









Clement HuiClement Hui

175




175












  • You are running into an infinite loop without a breaking condition at the client side. What will make you break from the inner while loop to read a new image from the webcam inside the outer loop?

    – Islam El-Rougy
    Mar 23 at 8:17











  • I corrected the error but still it doesn't work. I added a debug System.out.println() in the Server side to print the length and here is the output 4465 8917 13388 17869 22323 26786 31233 35694 40168 44637 49116 53596 58062 62546 67038 -657982985 Exception in thread "main" java.lang.NegativeArraySizeException at ServerSide.ServerWorker.run(ServerWorker.java:39) at ServerSide.Server.<init>(Server.java:42) at ServerSide.ServerMain.main(ServerMain.java:7)

    – Clement Hui
    Mar 23 at 8:21


















  • You are running into an infinite loop without a breaking condition at the client side. What will make you break from the inner while loop to read a new image from the webcam inside the outer loop?

    – Islam El-Rougy
    Mar 23 at 8:17











  • I corrected the error but still it doesn't work. I added a debug System.out.println() in the Server side to print the length and here is the output 4465 8917 13388 17869 22323 26786 31233 35694 40168 44637 49116 53596 58062 62546 67038 -657982985 Exception in thread "main" java.lang.NegativeArraySizeException at ServerSide.ServerWorker.run(ServerWorker.java:39) at ServerSide.Server.<init>(Server.java:42) at ServerSide.ServerMain.main(ServerMain.java:7)

    – Clement Hui
    Mar 23 at 8:21

















You are running into an infinite loop without a breaking condition at the client side. What will make you break from the inner while loop to read a new image from the webcam inside the outer loop?

– Islam El-Rougy
Mar 23 at 8:17





You are running into an infinite loop without a breaking condition at the client side. What will make you break from the inner while loop to read a new image from the webcam inside the outer loop?

– Islam El-Rougy
Mar 23 at 8:17













I corrected the error but still it doesn't work. I added a debug System.out.println() in the Server side to print the length and here is the output 4465 8917 13388 17869 22323 26786 31233 35694 40168 44637 49116 53596 58062 62546 67038 -657982985 Exception in thread "main" java.lang.NegativeArraySizeException at ServerSide.ServerWorker.run(ServerWorker.java:39) at ServerSide.Server.<init>(Server.java:42) at ServerSide.ServerMain.main(ServerMain.java:7)

– Clement Hui
Mar 23 at 8:21






I corrected the error but still it doesn't work. I added a debug System.out.println() in the Server side to print the length and here is the output 4465 8917 13388 17869 22323 26786 31233 35694 40168 44637 49116 53596 58062 62546 67038 -657982985 Exception in thread "main" java.lang.NegativeArraySizeException at ServerSide.ServerWorker.run(ServerWorker.java:39) at ServerSide.Server.<init>(Server.java:42) at ServerSide.ServerMain.main(ServerMain.java:7)

– Clement Hui
Mar 23 at 8:21













1 Answer
1






active

oldest

votes


















2














On client side you always write the new image to the existing stream. That leads to an increasing array size in every iteration. In java int has a maximum of 2147483647. If you increase this integer it skips to the minimum value auf int which is negative (see this article).



So to fix this error you need to clear your stream before writing the next image so the size is never greater than integer's max value.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55311825%2fjava-video-chat-applicataion-input-output-stream-not-working%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    On client side you always write the new image to the existing stream. That leads to an increasing array size in every iteration. In java int has a maximum of 2147483647. If you increase this integer it skips to the minimum value auf int which is negative (see this article).



    So to fix this error you need to clear your stream before writing the next image so the size is never greater than integer's max value.






    share|improve this answer



























      2














      On client side you always write the new image to the existing stream. That leads to an increasing array size in every iteration. In java int has a maximum of 2147483647. If you increase this integer it skips to the minimum value auf int which is negative (see this article).



      So to fix this error you need to clear your stream before writing the next image so the size is never greater than integer's max value.






      share|improve this answer

























        2












        2








        2







        On client side you always write the new image to the existing stream. That leads to an increasing array size in every iteration. In java int has a maximum of 2147483647. If you increase this integer it skips to the minimum value auf int which is negative (see this article).



        So to fix this error you need to clear your stream before writing the next image so the size is never greater than integer's max value.






        share|improve this answer













        On client side you always write the new image to the existing stream. That leads to an increasing array size in every iteration. In java int has a maximum of 2147483647. If you increase this integer it skips to the minimum value auf int which is negative (see this article).



        So to fix this error you need to clear your stream before writing the next image so the size is never greater than integer's max value.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 23 at 9:32









        embie27embie27

        965




        965





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55311825%2fjava-video-chat-applicataion-input-output-stream-not-working%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해