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;
}
}