Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

Sockets and OutputStreams

Thread view: 
Bari - 10 Dec 2005 22:08 GMT
Hey All,

Does java allow multiple OutputStreams to work together?

I'm creating a simple server program that sends an array through
ObjectOutputStream and sends a String through PrintWriter. However,
whenever I create an ObjectOutputStream and try to send data through
PrintWritert, funky characters come out. But when I dont create the
ObjectOutputStream then the message comes through on the client side or
telnet.

Below is the code that works:

import java.net.*;
import java.io.*;

public class Server
{
    public static void main(String[] args)
    {
        int i[] = {1, 2, 3, 4, 5, 6};
            try
            {
                ServerSocket server = new ServerSocket(8008);
                Socket client = server.accept();
                               //ObjectOutputStream o = new
ObjectOutputStream(

  // client.getOutputStream());
                PrintWriter p = new PrintWriter(newOutputStreamWriter(

     client.getOutputStream()));
                p.println("Message Test");
                o.flush();
            }
            catch(Exception e)
            {
                e.printStackTrace();
           }
    }
}

When I put this line in the code, ObjectOutputStream o = new
ObjectOutputStream(client.getOutputStream()); , then a funky character
comes out instead of the text I sent.

To see what I mean you can try this command in dos: telnet localhost
8008
Heiner Kücker - 10 Dec 2005 23:03 GMT
> When I put this line in the code, ObjectOutputStream o = new
> ObjectOutputStream(client.getOutputStream()); , then a funky character
> comes out instead of the text I sent.

The ObjectOutputStream is for srialization Objects,
not for printing strings over the xxxWriter.

Signature

Heiner
www.heinerkuecker.de

Bari - 10 Dec 2005 23:37 GMT
I made a little mistake, I meant to write:
PrintWriter p = new
PrintWriter(newOutputStreamWriter(client.getOutputStream()));
                p.println("Message Test");
                p.flush();
I want to use 'p' to send Strings over the socket, but I want to use
the ObjectOutputStream to send objects over the socket. However, when I
create an ObjectOuputStream  the object 'p' can't even send a corrent
message.

This is the output of the message  ¼fTest Message
Alan Krueger - 12 Dec 2005 04:31 GMT
> I made a little mistake, I meant to write:
> PrintWriter p = new
[quoted text clipped - 7 lines]
>
> This is the output of the message  ¼fTest Message

Wow, it reverses the order of the words?  Or are you typing from memory?

Please copy-and-paste exactly what is being run and exactly what is
being output.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.