News:

Who uses forums anymore?

Main Menu

[JAVA] Easy-To-Use Socket Interface

Started by Joe, November 06, 2005, 05:31:31 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Joe

If you tell the socket to write "test", it will send "test", nothing more, nothing less.

I'm not really sure how buffered input streams (or whatever I'm using =p) work, but I guess each time it recieves a TCP packet, it adds that packet as a new thing on the buffer, and when you ask for something from the buffer, it returns the first thing and then bumps everything up a notch.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Joe

#16
Sorry to bump this, but I found some new use to it. After going through a semester of Java programming, I've decided to fix the way I did some formatting and whatnot, and I made it more generic (it doesn't use my console class, but rather uses System.err for what's necessary).

EDIT -
Whoops -- didn't review it carefully enough. I just remembered that I learned what staticness was in class, and when I wrote this, I was just trying to get around it. Fixed.

package networking;

/*
* Author: Joe[x86]
*
* This is just a socket wrapper I made to make sockets easier to use
* Methods:
* - bool connect(String, int)
* - bool isConnected()
* - void sendData(String)
* - void sendData(byte[])
* - bool hasData()
* - String getData()
*/

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

public class SocketWrapper
{
    private Socket sck;
    private BufferedReader sck_BR;
    private OutputStream sck_OS;
   
    public SocketWrapper()
    {
    }
   
    public SocketWrapper(Socket socket)
    {
    this.sck = socket;
    makeStreams();
    }
   
    public boolean isConnected()
    {
    return sck.isConnected();
    }
   
    public String getData() {
    try
    {
    return sck_BR.readLine();
    }
    catch(IOException e)
    {
    System.err.println("Socket threw IOException in getData():\n" + e.toString());
    return "";
    }
    }

    public boolean hasData()
    {
    try
    {
    if(sck_BR.ready() == true)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    catch(IOException e)
    {
    System.err.println("Socket threw IOException in hasData():\n" + e.toString());
    return false;
    }
   
    }
   
    public boolean connect(String hostname, int port)
    {
        try {
        sck = new Socket(hostname, port);
        }
        catch(IOException e)
        {
        System.err.println("Socket threw IOException in connect():\n" + e.toString());
    return false;
        }
        return makeStreams(); // this will return true if makeStreams() works
    }

public void sendData(String data)
{
try
{
for(int i = 0; i < data.length(); i++)
{
sck_OS.write(data.charAt(i));
}
sck_OS.flush();
}
catch(IOException e)
{
System.err.println("Socket threw IOException in sendData():\n" + e.toString());
}
}

public void sendData(byte[] data)
{
try
{
sck_OS.write(data);
sck_OS.flush();
}
catch(IOException e)
{
System.err.println("Socket threw IOException in sendData():\n" + e.toString());
   
}
}

private boolean makeStreams()
{
try
{
sck_OS = sck.getOutputStream();
sck_BR = new BufferedReader(new InputStreamReader(sck.getInputStream()));
}
catch(IOException e)
{
System.err.println("Socket threw IOException in makeStreams():\n" + e.toString());
try
{
sck.close();
}
catch(IOException ex)
{
// who cares? If we had an exception, it's closed anyhow. :)
}
return false;
}
return true;
}
}
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


AntiVirus

   
public SocketWrapper()
{
}


What is the point of that?
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Sidoh

Quote from: AntiVirus on January 31, 2007, 11:16:41 AM
What is the point of that?

It's an override of the constructor so you don't have to provide parameters to create a 'SocketWrapper' object.  I'm not sure it makes sense considering the other constructor calls private methods, though...

MyndFyre

Quote from: AntiVirus on January 31, 2007, 11:16:41 AM
   
public SocketWrapper()
{
}


What is the point of that?

The connect(String, int) method creates a new Socket object.  Creating a SocketWrapper instance with a Socket parameter does not require connect to be called; creating one without a parameter requires connect to be called.

If that constructor wasn't there, he couldn't create a SocketWrapper without a socket parameter.

Quote from: Sidoh on January 31, 2007, 11:41:32 AM
I'm not sure it makes sense considering the other constructor calls private methods, though...
The connect method calls the private method you're referring to.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Sidoh

Quote from: MyndFyrex86] link=topic=3608.msg107620#msg107620 date=1170261819]
The connect method calls the private method you're referring to.

Missed that.  Unfortunately, I don't have the patience to sift through Joe's code. :(

Joe

Quote from: Sidoh on January 31, 2007, 01:33:03 PM
Quote from: MyndFyrex86] link=topic=3608.msg107620#msg107620 date=1170261819]
The connect method calls the private method you're referring to.

Missed that.  Unfortunately, I don't have the patience to sift through Joe's code. :(

I'm not sure if you were trying to insult my code or your patience by saying that, but if you ask me, my code is just about as readable as JavaOp2, which is pretty decent.

And yeah, MyndFyre is correct.. originally I didn't have any constructor at all, but now that I allowed it to be constructed with a Socket object passed, I needed to manually put a no-args constructor in there. When you don't have any constructor at all, Java automagically gives it a no-args constructor.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Sidoh

Quote from: Joex86] link=topic=3608.msg107653#msg107653 date=1170302191]
I'm not sure if you were trying to insult my code or your patience by saying that, but if you ask me, my code is just about as readable as JavaOp2, which is pretty decent.

It'd be obvious if I was trying to insult your code.  I'm not trying to insult it; I'm merely saying I don't have the patience to read it.

warz

#23
If I were wanting to read it, i'd probably drop it also. Nothing looks worse to me than code designating an entire line to a single opening french bracket ({).
http://www.chyea.org/ - web based markup debugger

Sidoh

Quote from: warz on January 31, 2007, 11:59:21 PM
If I were wanting to read it, i'd probably drop it also. Nothing looks worse to me than code designating an entire line to a single opening french bracket ({).

I prefer adding curly brackets (I prefer that name for them :P) after statements as well.  Since it's a completely subjective decision, though, I don't think it really matters which way you prefer...

Joe

I have an ungodly huge resolution, and I don't like code that looks clumped together. The brainchild of these two factors is the braces getting their line.

And what's the real name for them? To me, ( ) has always been parenthesis, [ ] are brackets, and { } are braces.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Sidoh

Quote from: Joex86] link=topic=3608.msg107697#msg107697 date=1170368815]
I have an ungodly huge resolution, and I don't like code that looks clumped together. The brainchild of these two factors is the braces getting their line.

And what's the real name for them? To me, ( ) has always been parenthesis, [ ] are brackets, and { } are braces.

Every CS professor here refers to them as "curly brackets."  I've never heard them called "French Brackets."

MyndFyre

Quote from: Joex86] link=topic=3608.msg107697#msg107697 date=1170368815]
I have an ungodly huge resolution, and I don't like code that looks clumped together. The brainchild of these two factors is the braces getting their line.

And what's the real name for them? To me, ( ) has always been parenthesis, [ ] are brackets, and { } are braces.
Those are the names I've used, except () are parentheses, and one is a parenthesis.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

rabbit

Quote from: Sidoh on February 01, 2007, 08:21:25 PM
Quote from: Joex86] link=topic=3608.msg107697#msg107697 date=1170368815]
I have an ungodly huge resolution, and I don't like code that looks clumped together. The brainchild of these two factors is the braces getting their line.

And what's the real name for them? To me, ( ) has always been parenthesis, [ ] are brackets, and { } are braces.

Every CS professor here refers to them as "curly brackets."  I've never heard them called "French Brackets."
Ditto!

iago

Yeah, they're generally called curly brackets or braces here, usually braces. 

And I greatly prefer putting the opening brace on its own line.  I find it makes it easier to match up braces.  Also, it keeps code more spaced out which makes it look cleaner.  From marking assignments, I can tell you that I, as a marker, greatly prefer it on the next line. 

One of my profs was telling me that the on-the-same-line convention was created because of text-books, which have limited space.  In the olden days (think Pascal), it had to be on the next line. 

But you're right, it's completely subjective.  Those are just my opinions.  Of course, anybody who doesn't agree with me is very much wrong (kidding, of course :P)