Archived

This topic is now archived and is closed to further replies.

Idej-Avaj

[java] Sockets and Threads

Recommended Posts

I've got a program that connects to a socket, and watches for updates from the socket's inputstream... My problem is that the thread only seems to run twice. here's the code:
   import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SocketTest extends javax.swing.JFrame implements Runnable{
    Socket s;
    InputStream is;
    BufferedInputStream bis;
    DataInputStream dis;
    OutputStream os;
    BufferedOutputStream bos;
    DataOutputStream dos;
    /** Creates new form SockTest */

    public SocketTest() {
        try{
            initComponents();
            setSize(600,400);
            connect();
            t.start();
            
        } catch (Exception e){
        }
    }
    
    public void connect() throws Exception {
        s = new Socket("pop3.ij.net",110);
        is = s.getInputStream();
        bis = new BufferedInputStream(is);
        dis = new DataInputStream(bis);
        os = s.getOutputStream();
        bos = new BufferedOutputStream(os);
        dos = new DataOutputStream(bos);
    }
    
    // lots of other init stuff was here... bla bla bla.... 

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        SocketTest st =  new SocketTest();
        st.show();
    }
    
    Thread t = new Thread(this,"threadt");
    public void run() {
        while(true){
            System.out.println("done");
            try{
                console.append("\n" + dis.readLine());
            }catch(Exception e){e.printStackTrace();}
        }
    }
    
  
    private javax.swing.JTextArea console;
    
}    
Any ideas? Edited by - idej-avaj on March 21, 2002 12:53:42 PM

Share this post


Link to post
Share on other sites
The reason that the Thread only seems to run twice is because only two lines of data will be sent. It seems you are using SMTP to connect to a POP3 server (not sure how this will work), but via SMTP, you will only get these two lines. More data will be sent, but only when you send data through the OutputStreams. Unfortunately, I don''t know SMTP well enough to help you, but:

[url]http://javaboutique.internet.com/articles/SMTP/[/url]

should help you.

It''''s over for now... until yesterday begins again... tomorrow... tomorrow... tomorrow...

[Powerman 5000 - Watch the Sky for Me]

Share this post


Link to post
Share on other sites
Funny, I am currently working on the same sort of thing. Using sockets to connect to a webserver to pull data from a page. I found an awesome tutorial on the subject with full source code. I haven''t followed the other URL posted to see what it is about, but I highly recommend looking at this one:

http://www.quickreferences.yucom.be/P&T/Java%20sockets.pdf

good luck.

-Just when you think things are starting to look up, life grabs you by the jaws makes you open up wide and sh*ts down your throat.

Share this post


Link to post
Share on other sites