Solved

java.net.SocketOutputStream cannot be cast to java.io.DataOutputStream

Posted on 2012-03-28
5
1,244 Views
Last Modified: 2012-03-29
I built a proxy server processing data for MineCraft.

The proxy program works by itself, and I was able to use it to transfer connections to websites or Minecraft servers.  Both worked without issue.

However, now I am trying to read the packets, and it requires that I turn the java.net.SocketOutputStream into a Java.io.DataOutputStream.  

Casting does not work, nor does:
to = new DataOutputStream(outgoing.getOutputStream()); // throws the error in the title.

I'm very skilled in C#, but just learning Java.  how can I convert this stream? - I'm presuming I just need to know the right casting path though another layer/command.
0
Comment
Question by:hpdvs2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37780200
OutputStream is for writing like say in this example:

PrintStream printStream = new PrintStream(socket.getOutputStream());
printStream.print("JOIN #channel1\r\n");
printStream.flush( );
printStream.print("JOIN #channel2\r\n");
printStream.flush( );



Please, elaborate on what you want to achieve
0
 
LVL 8

Author Comment

by:hpdvs2
ID: 37780231
Ah, thanks for the clarification.  

I have a java.net.Socket, and the object I'm using is expecting java.io.DataOutputStream.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 37780268
What is the type of "outgoing" as used in your original question? If that is indeed a java.net.Socket then the line of code that you posted should work. Socket.getOutputStream() returns an OutputStream object which is the required type of argument to the DataOutputStream constructor.

If the above is not making sense, can you post your code so that we can help further?
0
 
LVL 8

Author Closing Comment

by:hpdvs2
ID: 37781358
I have no idea why I was getting that error before, but it does appear to be working now.  There must have been another part that was affecting it, part of the code I didn't post.  But most of it is changed now, so I won't be able to figure out what the problem was.

Thanks for your help.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 37781417
Not a problem, glad to help!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 436
Convert from a json string array to a Java object 3 64
going to wrong jsp page 2 49
junit example issue 2 28
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question