News:

So the widespread use of emojis these days kinda makes forum smileys pointless, yeah?

Main Menu

[Java] Using sockets in a JavaOp plugin

Started by Ryan Marcus, December 06, 2005, 06:55:44 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ryan Marcus

Alright, after you read this it should be pretty obvious what it does:


[pre]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

import javax.swing.JComponent;

import callback_interfaces.PluginCallbackRegister;
import callback_interfaces.PublicExposedFunctions;
import callback_interfaces.StaticExposedFunctions;
import exceptions.PluginException;
import plugin_interfaces.ConnectionCallback;
import plugin_interfaces.EventCallback;
import plugin_interfaces.GenericPluginInterface;

public class PluginMain extends GenericPluginInterface implements EventCallback, ConnectionCallback
{

    private static ServerSocket serverSocket = null;

    private static Socket clientSocket = null;

    private static PrintWriter pOut = null;

    private static BufferedReader in = null;
    private static PublicExposedFunctions tOut = null;

    public void load(StaticExposedFunctions staticFuncs)
    {
       
    }

public void activate(PublicExposedFunctions out, PluginCallbackRegister register)
    {
    register.registerEventPlugin(this, null);
    register.registerConnectionPlugin(this, null);
    tOut = out;
    }
public void deactivate(PluginCallbackRegister register)
    {
        // TODO Auto-generated method stub

    }

    public String getName()
    {
        // TODO Auto-generated method stub
        return "SBCP";
    }

    public String getVersion()
    {
        // TODO Auto-generated method stub
        return "1.0";
    }

    public String getAuthorName()
    {
        // TODO Auto-generated method stub
        return "Ryan Marcus";
    }

    public String getAuthorWebsite()
    {
        // TODO Auto-generated method stub
        return "None";
    }

    public String getAuthorEmail()
    {
        // TODO Auto-generated method stub
        return "ryan@marcusfamily.info";
    }

    public String getLongDescription()
    {
        // TODO Auto-generated method stub
        return "Allows you to connect with SBCP";
    }

    public Properties getDefaultSettingValues()
    {
        // TODO Auto-generated method stub
        Properties p = new Properties();
        return p;
    }

    public Properties getSettingsDescription()
    {
        // TODO Auto-generated method stub
        Properties p = new Properties();
        return p;
    }

    public JComponent getComponent(String settingName, String value)
    {
        // TODO Auto-generated method stub
        return null;
    }

    public Properties getGlobalDefaultSettingValues()
    {
        // TODO Auto-generated method stub
        Properties p = new Properties();
        return p;
    }

    public Properties getGlobalSettingsDescription()
    {
        // TODO Auto-generated method stub
        Properties p = new Properties();
        return p;
    }

    public JComponent getGlobalComponent(String settingName, String value)
    {
        // TODO Auto-generated method stub
        return null;
    }
   
   
    public void talk(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
       pOut.println("TALK" + user + " " + statstring);
    }

    public void emote(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("EMOT" + user + " " + statstring);
    }

    public void whisperFrom(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("WSFM" + user + " " + statstring);
    }

    public void whisperTo(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("WSCN" + user + " " + statstring);
    }

    public void userShow(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("USIN" + user + " " + statstring);
    }

    public void userJoin(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("USJN" + user + " " + statstring);
    }

    public void userLeave(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("USLV" + user + " " + statstring);
    }

    public void userFlags(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("USFG" + user + " " + statstring);
    }

    public void error(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("ERRO" + user + " " + statstring);
    }

    public void info(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("INFO" + user + " " + statstring);
    }

    public void broadcast(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("BRDC" + user + " " + statstring);
    }

    public void channel(String user, String statstring, int ping, int flags) throws IOException, PluginException
    {
        pOut.println("CHAN" + user + " " + statstring);
    }
   
    public final boolean ABORT = false;
    public final boolean CONTINUE = true;
   
    /** The bot is about to connect to a server, but hasn't yet.  At this point, the connection can be stopped. */
    public boolean connecting(String host, int port, Object data) throws IOException, PluginException
    {
        return false;
    }
    /** The bot has just connected to the server. */
    public void connected(String host, int port, Object data) throws IOException, PluginException
    {
       
       
       
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

   
        try
        {
            clientSocket = serverSocket.accept();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     

       
         
         
         try
        {
            pOut = new PrintWriter(clientSocket.getOutputStream(), true);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
     
           
            try
            {
                in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
       
        pOut.println("Connected!");
       
       
           
    }
   
    /** The bot is about to disconnect from the server.  This is only called for planned disconnects. */
    public boolean disconnecting(Object data)
    {
        return false;
    }
    /** The bot has disconnected from the server. */
    public void disconnected(Object data)
    {
    }

   
    private static String inputLine = null;
   
    while ("i".equalsIgnoreCase("e"))
    {
        try
        {
            inputLine = in.readLine();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (inputLine.length() != 0)
        {
          try
        {
            tOut.sendText(inputLine);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }
   

}
}
[/pre]


The only problem Eclipse is telling me about is that private static String inputLine = null; should not end in a ;. but it should be a {?


Help!
Thanks, Ryan Marcus

Quote
<OG-Trust> I BET YOU GOT A CAR!
<OG-Trust> A JAPANESE CAR!
Quote
deadly: Big blue fatass to the rescue!
496620796F75722072656164696E6720746869732C20796F75722061206E6572642E00

MyndFyre

It's because your code is broken.  You have a while() loop outside of a method:

    public void disconnected(Object data)
    {
    }
    // function ends

    // member declaration
    private static String inputLine = null;
    // compiler: oh, there's code here.  That shouldn't have been an end-of-statement, it should have been
    // an opening brace.  Error!
    while ("i".equalsIgnoreCase("e"))
    {
        try
        {
            inputLine = in.readLine();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (inputLine.length() != 0)
        {
          try
        {
            tOut.sendText(inputLine);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }
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.

Ryan Marcus

Ah! I see. Then where do you suggest I put that code? Its still in the class, so I thought it might be OK...
Thanks, Ryan Marcus

Quote
<OG-Trust> I BET YOU GOT A CAR!
<OG-Trust> A JAPANESE CAR!
Quote
deadly: Big blue fatass to the rescue!
496620796F75722072656164696E6720746869732C20796F75722061206E6572642E00

MyndFyre

Well it needs to be in a function, otherwise having code in arbitrary places won't do anything....
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.

Joe

while ("i".equalsIgnoreCase("e"))

You know i is never e, right? =p
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.


Ryan Marcus

That's the idea. It loops forever, because I can't figure out how to make code execute when a socket receives data.
Thanks, Ryan Marcus

Quote
<OG-Trust> I BET YOU GOT A CAR!
<OG-Trust> A JAPANESE CAR!
Quote
deadly: Big blue fatass to the rescue!
496620796F75722072656164696E6720746869732C20796F75722061206E6572642E00

Sidoh

Quote from: Ryan Marcus on December 06, 2005, 09:37:12 PM
That's the idea. It loops forever, because I can't figure out how to make code execute when a socket receives data.

Is while(true) illegal in Java? :\

MyndFyre

Quote from: Sidoh on December 06, 2005, 09:42:49 PM
Quote from: Ryan Marcus on December 06, 2005, 09:37:12 PM
That's the idea. It loops forever, because I can't figure out how to make code execute when a socket receives data.

Is while(true) illegal in Java? :\
No, and receive() should block until data arrives.
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.

Joe

Resolved, thanks to Joe!

I taught Ryan how to use a Timer. =)
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.


MyndFyre

You know, Timers aren't the proper way to implement multithreading when there is a Thread class.  :P
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.