RomanWare  

Go Back   RomanWare > Programming > C,C++

C,C++ Old School C and C++

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 10-24-2009, 12:41 PM
m0j0j0j0 m0j0j0j0 is offline
CORPORAL
 
Join Date: Jun 2007
Posts: 104
Rep Power: 4
m0j0j0j0 is on a distinguished road
Default Anyone Here a Java Programmer?

Hello all i havent played with java in a couple months and was recently inteested in writing a socket for java. socket read/write operations confused me for a bit, I managed to make a quik fix and get it working but then i decided to take it a step further and add a thread to handle reading data from the socket to allow read/write operations efficently.

My socket works and all now, im attaching the source to the simple socket i created, if anyone is experienced in java i would appreciate your feedback. Did i attack this read/write problem in the proper way? I have only began to scratch the surface of creating seperate threads and i understand the importance of it, just wondering if my solution by adding a thread to the socket for reading data was the right way to attack this problem.

Any insight is appreciated
Thanks..


Code:
/**
 *
 * @author mojo
 */

public class Demo
{
	public static void main(String[] args)
	{
		J_Socket sock = new J_Socket(java.net.Proxy.NO_PROXY, "yahoo.com", 80);
		sock.write_Socket_Data("GET ");
	}

}
class J_Socket extends Socket{

    private PrintWriter out = null;
    private BufferedReader in = null;
    private String response = null;

    private void showMessage(Object msg)
    {
        javax.swing.JOptionPane.showMessageDialog(
                null,String.valueOf(msg));
    }

    public J_Socket(Proxy proxy, String remoteHost, int remotePort) {
        super(proxy);
        InetSocketAddress remoteAddress = new
                InetSocketAddress(remoteHost, remotePort);
        try
        {
           this.connect(remoteAddress);
           if(this.isConnected())
           {
               out = new PrintWriter(this.getOutputStream(), true);
               in = new BufferedReader( new
                        InputStreamReader(this.getInputStream()));
               new J_sckReader("socketReader").start();
           }
        }
        catch(Exception e)
        {System.out.println(e.toString());}
    }

    public void write_Socket_Data(String Packet)
    {
        out.println(Packet);
    }

   private class J_sckReader extends Thread {

        public J_sckReader(String str) {
            super(str);
        }
        @Override
        public void run()
        {

            try
            {
                while ((response = in.readLine()) != null)
                {
                    System.out.println("Server: " + response);
                }
            } catch (Exception e)
            { showMessage(e.toString());}
        }
    }
}
Reply With Quote
  #2  
Old 11-06-2009, 10:49 AM
m0j0j0j0 m0j0j0j0 is offline
CORPORAL
 
Join Date: Jun 2007
Posts: 104
Rep Power: 4
m0j0j0j0 is on a distinguished road
Default

well after doing some reading, i found that the new nio java.nio has classes which help in creating an asynchronous non blocking sockets which not only offers more performance but flexibility as well.
socktchannel is the main class im referring to

I found a good source to learn from, i may share it here after i modify it to my liking, not that most of you care haha
java rules, too bad theres not alot of you out there that write in java.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:04 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©RomanWare 2009
BP-Grey Style by Bluepearl Design - Ver2.1