Inputstream & outputstreams in network programming

Hi
I have been experimenting with inputstreams & outputstreams through sockets, and have been having some annoying problems.
First off, is it possible to have multiple outputstream types going through the same socket, eg a printwriter to send strings, and an OutputStream to send bytes (files), so that first i can send the file name, then file size, using the writers println methods, then send a byte array via the OutputStreams write method. Do i need to do some clean up etc after each transmission through the socket.
My client is set up to recieve two strings, then a byte array.
Sometimes it works, sometimes it doesnt.
I have even tried setting the buffer sizes of the sockets etc, but still to no success. I think the outputstreams are getting cluttered, but to be honest I dont know. Has anyone successfully and reliably done this type of thing before, if so can you help.
Steve

At least say flush() after having finihhed with one and before turning to the other.

Similar Messages

  • Network Programming exam papers (college exam papers)

    hello.
    my name is james mcfadden and i am a final year (4th year) undergraduate computing student at Letterkenny Institute of Technology. my e-mail address is [email protected] I have a question here for you. Would you have links to online versions of final year undergraduate Computing exam papers, specifically Networked Applications Development and or Network Management exam papers? I was only able to get access to Trinity College Dublin�s exam papers and Letterkenny IT�s exam papers.
    The 2005/06 academic year is the 1st academic year that final year undergraduate computing students at Letterkenny IT are being taught how to continue doing network programming in Java. i was taught how to do simple network programs in Java when I was in 3rd year.
    The name of the network programming subject that I�m doing in 4th year is Network Programming for Broadband Systems. It is divided into 2 parts: Programming and Networks. In past exam papers (C++ papers), a student would have had to answer 4 out of 6 questions. There would�ve been 4 programming questions and 2 networking questions on those papers. It is going to be the same for both the Summer and Autumn 2007 Broadband Network Programming exam papers, apart from fact that they will be Java papers and that a lot of the material that I have learned is new.
    In 4th year I did Multicast Sockets, Threads, Non-Blocking I/O, Remote Method Invocation, Protocol Handlers, and Content Handlers for the Programming part of the subject (Java Network Programming, 3rd Edition, by Elliote Rusty Harold).
    I did Network Security and Network Management for the Networks part of the subject (Computer Networking : A Top-Down Approach Featuring The Internet, 3rd Edition, by James F. Kurose and Keith W. Ross).
    Your help is greatly appreciated.
    Thank you.

    I have a question here for you.
    Would you have links to online versions of final year
    undergraduate Computing exam papers, specifically
    Networked Applications Development and or Network
    Management exam papers? No

  • How to write JDBC code in a java thread? for network programming

    Hii guys, i am new to java network programming. I developed small swing application for stock controlling in a shop, so i need to run the database in a server. i try the peer to peer scenario, but the response is too late and the application get stuck. there i wl put my data base java class
    please help me for this, how can i change this java class to networked JDBC
    import com.mysql.jdbc.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class ConnectionSet {
    private String severIp = "localhost";
    private String severPort = "3306";
    private String userName = "root";
    private String password = "123";
    private ResultSet rs;
    public void setSeverIp(String Ip) {
    severIp = Ip;
    public void setSeverPort(String Port) {
    severPort = Port;
    public void SetUserName(String Name) {
    userName = Name;
    public void setPassword(String passWord) {
    password = passWord;
    public ResultSet getResult(String url) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection cc = (Connection) DriverManager.getConnection("jdbc:mysql://" + getSeverIp() + ":" + getSeverPort() + "/suriyalanka", getUserName(), getPassword());
    Statement s = cc.createStatement();
    rs = s.executeQuery(url);
    // java.sql.ResultSet rs = (ResultSet) DriverManager.getConnection("jdbc:mysql://"+getSeverIp()+":"+getSeverPort()+"/suriyalanka",getUserName(), getPassword()).createStatement().executeQuery(url);
    return rs;
    public Connection getConnection() throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection cc = (Connection) DriverManager.getConnection("jdbc:mysql://" + getSeverIp() + ":" + getSeverPort() + "/suriyalanka", getUserName(), getPassword());
    return cc;
    public void setResult(String url) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection cc = (Connection) DriverManager.getConnection("jdbc:mysql://" + getSeverIp() + ":" + getSeverPort() + "/suriyalanka", getUserName(), getPassword());
    Statement s = (Statement) cc.createStatement();
    s.executeUpdate(url);
    public String getSeverIp() {
    return severIp;
    public String getSeverPort() {
    return severPort;
    public String getUserName() {
    return userName;
    public String getPassword() {
    return password;
    please help me for this, how can i change this java class to networked JDBC
    Edited by: 798670 on Sep 29, 2010 6:04 AM

    Have you verified that your mysql allows network connections?
    In order to allow network connections you have to comment or remove line "skip-networking" in my.ini(windows) or my.cnf(unix) configuration files of your mysql instance.
    Or if you have the mysql administrator installed
    MySQL Administrator / Startup Variables / Disable networking (uncheck)
    By!

  • Network Programming

    I am somewhat familiar with how to make a connection between two machines and have one application talk to another. Basically you have a ServerSocket that the server uses to "listen: for incoming connections and a Socket that a client uses in order to initiate a connetion. Once a client makes a socket connection, the ServerSocket reutrns (via accept() ) a corresponding Socket through which communication takes place. You then have a true Socket to Socket connection and are able to treat the connection as an simple I/O Stream.
    But how would I be able to simply establish a connection between a Java Application on one machine and a C application on the other. I am not that familiar with C code...but I need to be able to send strings from my Java application into an application that a team member of mine is writing.

    On the java side you use the same Socket and ServerSocket code. On the C side it really depends on which C you are using and which operating system. In VC++ version 5 or 6 you should look at the MS WSA* functions or you could use the socket implementation in MFC. If you using Ansi C on a unix or linux system you can use "berkeley sockets", to get a start check out "man socket".
    You might want to get a copy of "Unix Network Programming Volume 1" by Stevens, regardless of the C system you plan to use.
    http://www.amazon.com/exec/obidos/tg/detail/-/013490012X/104-8001926-7743921?v=glance

  • Where can I find knowledge about "java network programming"?

    I am interested in network programming in java. I need some documents and I don't where can I find it?

    http://java.sun.com/docs/books/tutorial/networking/index.html

  • InputStream - OutputStream: Reader - Writer

    What was not possible by InputStream & OutputStream that JAVA had to provide Reader and Writer. I have found out that if I use InputStream to read a Chinese HTML page, it reads it. I can create a copy of the same HTML page by using OutputStream, the data remaining un-affected. Then, why do I go for Reader and Writer?
    What happens if InputStream encounters some unicode charchters to be read? How does InputStream and OutputStream function internally?

    Well, you can go look at the source of InputStream/OutputStream.
    The reason we have Reader/Writer as well is because the streams don't deal with character encoding (or at least, they tried to and did it very badly). So you would normally use streams for dealing with binary data and reader/writer for dealing with character data. If you're using streams to copy a file and this file happens be be HTML with Chinese characters, this will work fine because you're just duplicating bytes. But if you try and use a stream to read this file into memory, you will have problems because it won't understand the Unicode characters in the file. In this case, you need to use a Reader and tell it what the character encoding of the file is.

  • Network programming beginner

    Hi All,
    I have been programming for a while in java but for the first time am doing network programming. Basically I have to connect to one of our supplier's systems. They have setup a raw TCP/IP exchange for request response. We send them requests as a string over tcp/ip and they reply back with a string. The formats of course are propritory to the supplier(a big telecom company).
    My problem is quite simple at the surface, i the hell cant figure this out!!
    We have mulitple suppliers and I have a method called sendRequest(String req) in a general SupplierConnect interface. The implementation for this supplier (other suppliers talk SOAP so its simpler :) ) opens a socket... my code is as follows
    +++++++++++++++++++++++++++++++++++++++++++++++++
    public String sendRequest(String requestData) throws Exception{
    Socket sock = new Socket("xx.xxx.xxx.xx", 15001);
    StringBuffer responseData = new StringBuffer();
    try{
    //     sock.setSoTimeout(15000);     
    //setup writer
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
    out.write(requestData);
    //setup reader
    BufferedReader in = new     BufferedReader(new InputStreamReader(sock.getInputStream()));
    int c = 0;
    while (c != -1)
         c = in.read();
         responseData.append((char)c);
    sock.close();
    sock = null;
    }catch(Exception e){
                   System.out.println(e.getMessage());
              }finally{
                   if (sock != null)
                        System.out.println("Closing sock");
                        sock.close();
                        sock = null;
              return responseData.toString();
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    The problem is that the program waits indefinately in the while loop at the in.read()... it doesnot exit. I know i commented out the timeout of
    15 seconds cos that would help me exit... but i dont land up receiving any data and it just gets stuck there.
    I do not have any support from the Suppliers IT people so the last option was to check if i am doing things right... i really dont knw if i am doing sumthing worng here..
    So advise,
    Thanks :)
    manubhai

    Hey guys it works now - thanks for the byte[] idea..
    I do get the response now! however since the supplier does not return a -1 my while loop gets stuck. I have currently put a socket timeout to help break out - any clues to make this better????
    current code:
    try{
    sock.setSoTimeout(15000);     
    ByteArrayOutputStream ba = new ByteArrayOutputStream();
    ba.write(requestData.getBytes(),0,requestData.length());
    ba.writeTo(sock.getOutputStream());
    //setup reader
    BufferedReader in = new     BufferedReader(new InputStreamReader(sock.getInputStream()));
    int c = 0;
    while (c != -1)
    c = in.read();
    responseData.append((char)c);
    sock.close();
    sock = null;
    }catch(Exception e){
         System.out.println(e.getMessage());
    }

  • Desperately need Java network programming help!!!

    I need to make a Distributed File Sharing System (DFSS) using java language. The system should not make use of the central file server. The system should coordinate the concurrent access of files over the distributed environment. Caching may be used to enhance the system performance.
    It is basically network programming.
    Does any one have any idea how to make the DFSS. If you do please help!!!
    thank you in advance for you help
    cheers

    well, you're getting somewhere I guess. My original answer was intentionally vague because your original question was so vague. These fora are no good for asking questions like "how do I implement a distributed file system", they are good for asking things like "the following line(s) of code generate the following condition I didn't expect, rather I expected this condition, could someone tell me what is going on?" or something of similar specificity.
    So you are now asking how to, for instance, check to see if a text file is being shared. This is still too vague, but it's better than "how do I write a file sharing system". If you are feeling particularly industrious, go look at a project JXTA at http://www.jxta.org/ - it's open source, you can look at the code. Of course, if you're brand new, this might not help. In fact, not to discourage you, but if you're that new, this is not the project to be doing.
    Good Luck
    Lee

  • Looking into the possibilities of network programming

    Hi,
    One of my client is using a database application at their retail
    outlets. They have around 40 outlets located at various parts of
    the country. Now they want to set up a database at their Head Office
    and transfer all the data from the retail outlets to it. There are
    broadband internet connections available at all the outlets but
    there are chances of internet connectivity going down. The database
    we are using is PostgreSql 8.0
    Can this be achieved through a background process running at each
    retail outlet? Sending small packets of data from retail outlet to
    HO as soon as a transaction is persisited. There are Static IP
    available at HO.
    Can this be succesfully implimented? Has anybody done a similar
    work before? Is there any other alternative? Will Java Messaging
    or Network programming be helpful? Please help.
    Thank you.
    Vishal.

    The database product ought to be able to federate itself without you having to write any application code. If not, the best solution is to chagne DB vendor to one that does.

  • Network programs behind proxy

    I am a beginner in network programing,so i anyone could help me
    how till the program the proxy settings of my computer

    Try google
    http://www.google.co.uk/search?q=java+proxy+settings

  • Missing network programming on VOD?

    Normally VOD of network programming is a day behind.  It looks like the past 2 days aren't up yet- namely Life (NBC), CSI (CBS) and Grey's Anatomy (ABC).  Is it just me?  -- or is it a system-wide problem? 

    Here in So Cal we are missing Grey's, Lipstick Jungle, and Numb3rs as of Sunday.  Its starting to get irritating waiting for some of these shows to come up on VOD... whats going on Verizon?

  • Network programming - portability ?????

    Hi,
    I m developing a network game , using a multithread model. Do i need to hardcode the inet-address and port number of the server - i.e. SocketServer .
    If there is a better option ,then please tell.... because hardcoding the server inet address removes the portability aspect of my code.
    I am not using any feature of J2EE its purely network programming in J2SE.
    Thanks in advance

    Hi,
    I m developing a network game , using a multithread
    model. Do i need to hardcode the
    inet-address and port number of the
    server - i.e. SocketServer .Why would you think that? What would be special about parameters that you pass to networking methods vs. parameters that you pass to any other methods that the networking methods would know or care where they came from? Just like any other data your program uses, that data can be provided on the command line or in a properties file or by the preferences api or from a database or input from a user or or or or or....

  • Network programming tips

    I am new to network programming can anybody give me some tips on ways to optimize speed of an application that gets data via sockets over the network?
    You can assume its just a simple program where the client that gets continuous data feeds of sales amounts from the server and I want to make this program run faster.
    I would appreciate any ideas.
    Thanks!!

    Peter__Lawrey wrote:
    kci wrote:
    I am new to network programming can anybody give me some tips on ways to optimize speed of an application that gets data via sockets over the network?Don't worry about it. Your network is much slower than your CPU so it doesn't matter so much what you do in Java.
    I suggest you test your network to see what its latency and bandwidth is e.g. ping or transfer a large file and work out what you requirements are for latency and bandwidth.
    It very likely that you won't need to anything special in Java.
    You can assume its just a simple program where the client that gets continuous data feeds of sales amounts from the server and I want to make this program run faster.You should get a better understanding of the end to end process which needs to be faster and where you can make the most improvement. You are likely to find the GUI doesn't handle large tables or you are missing an index in your database.
    In Summary: determine where you can make the most difference and optimise that.
    I would appreciate any ideas. Actually there are loads of things you can do to make the network faster, but I would only do them if you actually need to.
    If you have done this analysis already what conclusions have you come to?Thanks a lot Peter!!
    All the posts in this thread have been helpful but yours definitely is the main direction I am looking for so I appreciate.
    Please if you can go a little further and help me with these issues....
    I am designing an application that is suppose to get real time data feeds over the network. Now as you said the network is slower than the CPU... But what if I am aiming for a latency of 5ms (just for the sake of discussion) but I ping the network and I see the latency is 10ms.
    Is there a way in this case you suggest I can optimize the network to reduce the latency.
    And no I haven't done the analysis yet. I am designing an application but I have never done this type of application so I am brainstorming now and you all have really helped out a lot so far!!

  • Network programming book

    Hi friends,
    I want to know about a book related to "Multicast and multi-homing" network programming in java.
    Thanks
    Ghouse

    I would suggest you look at Java Network Programming on Amazon.
    To repeat a previous posting, you cannot multicast over the internet.
    Multi-homing is and old approach to using a PC as a firewall. The PC has two network cards and passes packets back and forth based on filtering rules. Is this what you are looking for? Why wouldn't use use a firewall applicance which is probibly cheaper?

  • InputStream/OutputStream ...

    Hello
    For my socket communications, I am thinking of using InputStream/OutputStream to read/write data. Just realized that InputStream and OutputStream are abstract classes. In particular, the read() and write () are abstract methods
    with no actual implementation.
    =================================
    public abstract int read() throws IOException
    Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
    A subclass must provide an implementation of this method.
    Returns:
    the next byte of data, or -1 if the end of the stream is reached.
    Throws:
    IOException - if an I/O error occurs.
    ============================================
    However, when I call these methodes, they actually work!
    What is going on here?
    What is your recommendation for socket I/O? should i stick with my InputStream/OutputStream, or I should use DataInputStream/DataOutputStream?
    Thanks for your help,
    Alex

    Hello
    For my socket communications, I am thinking of using
    InputStream/OutputStream to read/write data. Just
    realized that InputStream and OutputStream are
    abstract classes. In particular, the read() and write
    () are abstract methods
    with no actual implementation.
    =================================
    public abstract int read() throws IOException
    Reads the next byte of data from the input stream. The
    value byte is returned as an int in the range 0 to
    255. If no byte is available because the end of the
    stream has been reached, the value -1 is returned.
    This method blocks until input data is available, the
    end of the stream is detected, or an exception is
    thrown.
    A subclass must provide an implementation of this
    method.
    Returns:
    the next byte of data, or -1 if the end of the stream
    is reached.
    Throws:
    IOException - if an I/O error occurs.
    ============================================
    However, when I call these methodes, they actually
    work!
    What is going on here?There is nothing strange about that. The type of the object that Socket.getInputStream() returns is a subclass of class InputStream. Read a book about Java interfaces and abstract classes (or look it up in the tutorial: http://java.sun.com/docs/books/tutorial/java/index.html).
    What is your recommendation for socket I/O? should i
    stick with my InputStream/OutputStream, or I should
    use DataInputStream/DataOutputStream?That depends on what data you want to communicate over the socket. DataInputStream and DataOutputStream make it easy for you to send and receive Java primitive types over the socket. You can create a DataInputStream that wraps the input stream of your socket as follows:
    Socket s = ...;   // create the socket etc.
    InputStream sis = s.getInputStream();
    DataInputStream dis = new DataInputStream(sis);
    // ... now use dis to read dataJesper

Maybe you are looking for

  • Making Markings in .PDF File

    I'm playing a cool game.  It's an old RPG-type game that uses the manual, which I have in .PDF. In that .PDF, there's, like, a list of quests and stuff.  I'd like to be able to check stuff off on the page of the book in the .PDF.  You know, as if usi

  • J2SE Plain Adapter

    Hi, We are using J2SE Plain Adapter ... In one of my scenario i need to send a flat file from one system to another. To achieve this i have created a sender file adapter in J2SE with content conversion. file is picked successfully ..i have given a sa

  • Help needed to do port forwarding in ipfw...

    For the last weeks I have been working to get the new open-source content management system Alfresco working. It is a pure Java web application that runs under Tomcat. There is official support for Mac OS X but only Windows and Linux gets true instal

  • Using Win 7 64bit on desktop and laptop. OK on laptop; desktop does not load graphics in broswer with/without exts/addons. Any ideas?

    Browser loads text on page but not full graphics - some - sometimes; sometimes none. I use same search engine on both desktop and laptop. I've tried disabling add-ons and extensions but it makes no difference. As I didn't know which version of Shockw

  • Z77A-G43 PCIe 3.0 problem with GTX 660

    Hello, I have built a new computer with the components described above. There is no video signal output from my GTX 660 when it's connected to the PCIe 3.0. It's OK when the GTX 660 is connected to the PCIe 2.0. When GTX 660is connected to PCIe 3.0 t