Results 1 to 8 of 8
  1. #1
    louwhopley is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Question Help neede with simple Networking program

    Hi, i have created a program which is almost finished (battleships:D).
    All i need is to implement now is the ability to play against friends over a LAN network.
    (It will only need to send a string over in the beginning and a integer every time a button is clicked - also receive those from opponent )

    I have read some of Sun's Java tutorials but don't have a clue what all those names and things mean.:confused:

    Can someone please help me with the it?:)

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Client:
    Java Code:
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    import java.net.Socket;
    
    public class Client {
    	public static void main (String[] args) throws Exception{
    		String 	sentence;
    		boolean run = true;
    
    		while(run){
    			System.out.print(">");
    			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    			//1234 is the port, pick one that isn't being used
    			Socket clientSocket = new Socket("ip of server here as a string i.e. 192.168.1.1",1234);
    			DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
    			BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    			sentence = in.readLine();
    			out.writeBytes(sentence+'\n');
    		}
    	}
    }
    Server
    Java Code:
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server {
    	public static void main(String[] args) throws Exception{
    		ServerSocket welcomeSocket = new ServerSocket(1234);
    		
    		while(true){
    	 		Socket connectionSocket = welcomeSocket.accept();
    			BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
    			DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
    			System.out.println(inFromClient.readLine());
    			outToClient.writeBytes(modifiedSentence);
    		}
    	}
    }

  3. #3
    louwhopley is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Default

    Thank you alot! i have figured it out and it workes perfect! thank you!
    Last edited by louwhopley; 08-03-2009 at 04:20 PM. Reason: I have figured it out!

  4. #4
    pushdown is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    an important tip:
    In network programming, server classes should have multi-threading ability so that it can deal with multiconnection. One way to do that is extending thread class for server class

  5. #5
    louwhopley is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Unhappy Help still needed please

    This is the entire coding for my game except the buttons (Areafield)
    The game playing itself, like making that you only have five hits etc. are not done yet due to the fact that I don't have the networking going yet.

    Can you please help me in implementing the networking into this game - e.g. it sends the button's number to the other PC and then he implements the hit etc. and also where to put all the commands like: Socket mySocket .....?


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    public class Play implements ActionListener
    {
        // Main window content
        JWindow startup_window = new JWindow ();
        JPanel startup_buttons = new JPanel ();
        JLabel startup_mainbg = new JLabel (new ImageIcon ("images/mainframe copy.jpg"));
        JButton startup_instructionsbtn = new JButton (new ImageIcon ("images/instructionsbtn.jpg"));
        JButton startup_singleplayer = new JButton (new ImageIcon ("images/singleplayerbtn.jpg"));
        JButton startup_multiplayer = new JButton (new ImageIcon ("images/multiplayerbtn.jpg"));
        JButton startup_exit = new JButton (new ImageIcon ("images/exitbtn.jpg"));
    
    
        // Instructions window content
        JWindow instructions_window = new JWindow ();
        JLabel instructions_bg = new JLabel (new ImageIcon ("images/instructframe.jpg"));
        JPanel instructions_backpnl = new JPanel ();
        JButton instructions_backbtn = new JButton (new ImageIcon ("images/backbtn.jpg"));
    
    
        // Ingame window content
        JWindow ingame_window = new JWindow ();
        JPanel ingame_enemypanel = new JPanel ();
        JPanel ingame_friendlypanel = new JPanel ();
        JPanel ingame_areas = new JPanel ();
        JPanel ingame_enemyvehicledamage = new JPanel ();
        JPanel ingame_missilecount = new JPanel ();
        JPanel ingame_mainmenupnl = new JPanel ();
        JLabel ingame_ne1 = new JLabel (new ImageIcon ("images/grid/ne1.jpg"));
        JLabel ingame_ne2 = new JLabel (new ImageIcon ("images/grid/ne2.jpg"));
        JLabel ingame_ne3 = new JLabel (new ImageIcon ("images/grid/ne3.jpg"));
        JLabel ingame_ne4 = new JLabel (new ImageIcon ("images/grid/ne4.jpg"));
        JLabel ingame_ne5 = new JLabel (new ImageIcon ("images/grid/ne5.jpg"));
        JLabel ingame_ne6 = new JLabel (new ImageIcon ("images/grid/ne6.jpg"));
        JLabel ingame_ne7 = new JLabel (new ImageIcon ("images/grid/ne7.jpg"));
        JLabel ingame_ne8 = new JLabel (new ImageIcon ("images/grid/ne8.jpg"));
        JLabel ingame_ne9 = new JLabel (new ImageIcon ("images/grid/ne9.jpg"));
        JLabel ingame_ne10 = new JLabel (new ImageIcon ("images/grid/ne10.jpg"));
        JLabel ingame_nea = new JLabel (new ImageIcon ("images/grid/nea.jpg"));
        JLabel ingame_neb = new JLabel (new ImageIcon ("images/grid/neb.jpg"));
        JLabel ingame_nec = new JLabel (new ImageIcon ("images/grid/nec.jpg"));
        JLabel ingame_ned = new JLabel (new ImageIcon ("images/grid/ned.jpg"));
        JLabel ingame_nee = new JLabel (new ImageIcon ("images/grid/nee.jpg"));
        JLabel ingame_nef = new JLabel (new ImageIcon ("images/grid/nef.jpg"));
        JLabel ingame_neg = new JLabel (new ImageIcon ("images/grid/neg.jpg"));
        JLabel ingame_neh = new JLabel (new ImageIcon ("images/grid/neh.jpg"));
        JLabel ingame_nei = new JLabel (new ImageIcon ("images/grid/nei.jpg"));
        JLabel ingame_nej = new JLabel (new ImageIcon ("images/grid/nej.jpg"));
        JLabel ingame_neblank = new JLabel (new ImageIcon ("images/grid/neblank.jpg"));
        JLabel ingame_nf1 = new JLabel (new ImageIcon ("images/grid/nf1.jpg"));
        JLabel ingame_nf2 = new JLabel (new ImageIcon ("images/grid/nf2.jpg"));
        JLabel ingame_nf3 = new JLabel (new ImageIcon ("images/grid/nf3.jpg"));
        JLabel ingame_nf4 = new JLabel (new ImageIcon ("images/grid/nf4.jpg"));
        JLabel ingame_nf5 = new JLabel (new ImageIcon ("images/grid/nf5.jpg"));
        JLabel ingame_nf6 = new JLabel (new ImageIcon ("images/grid/nf6.jpg"));
        JLabel ingame_nf7 = new JLabel (new ImageIcon ("images/grid/nf7.jpg"));
        JLabel ingame_nf8 = new JLabel (new ImageIcon ("images/grid/nf8.jpg"));
        JLabel ingame_nf9 = new JLabel (new ImageIcon ("images/grid/nf9.jpg"));
        JLabel ingame_nf10 = new JLabel (new ImageIcon ("images/grid/nf10.jpg"));
        JLabel ingame_nfa = new JLabel (new ImageIcon ("images/grid/nfa.jpg"));
        JLabel ingame_nfb = new JLabel (new ImageIcon ("images/grid/nfb.jpg"));
        JLabel ingame_nfc = new JLabel (new ImageIcon ("images/grid/nfc.jpg"));
        JLabel ingame_nfd = new JLabel (new ImageIcon ("images/grid/nfd.jpg"));
        JLabel ingame_nfe = new JLabel (new ImageIcon ("images/grid/nfe.jpg"));
        JLabel ingame_nff = new JLabel (new ImageIcon ("images/grid/nff.jpg"));
        JLabel ingame_nfg = new JLabel (new ImageIcon ("images/grid/nfg.jpg"));
        JLabel ingame_nfh = new JLabel (new ImageIcon ("images/grid/nfh.jpg"));
        JLabel ingame_nfi = new JLabel (new ImageIcon ("images/grid/nfi.jpg"));
        JLabel ingame_nfj = new JLabel (new ImageIcon ("images/grid/nfj.jpg"));
        JLabel ingame_nfblank = new JLabel (new ImageIcon ("images/grid/nfblank.jpg"));
        JLabel ingame_veh1 = new JLabel (new ImageIcon ("images/vehicle1.jpg"));
        JLabel ingame_veh2 = new JLabel (new ImageIcon ("images/vehicle2.jpg"));
        JLabel ingame_veh3 = new JLabel (new ImageIcon ("images/vehicle3.jpg"));
        JLabel ingame_veh5 = new JLabel (new ImageIcon ("images/vehicle4.jpg"));
        JLabel ingame_veh4 = new JLabel (new ImageIcon ("images/vehicle5.jpg"));
        JLabel ingame_controls = new JLabel (new ImageIcon ("images/ingameframe copy.jpg"));
        JLabel ingame_missile1 = new JLabel (new ImageIcon ("images/missile.jpg"));
        JLabel ingame_missile2 = new JLabel (new ImageIcon ("images/missile.jpg"));
        JLabel ingame_missile3 = new JLabel (new ImageIcon ("images/missile.jpg"));
        JLabel ingame_missile4 = new JLabel (new ImageIcon ("images/missile.jpg"));
        JLabel ingame_missile5 = new JLabel (new ImageIcon ("images/missile.jpg"));
        JButton ingame_mainmenu = new JButton (new ImageIcon ("images/mainmenubtn.jpg"));
        Areafield[] [] friendlyarea = new Areafield [10] [10];
        Areafield[] [] enemyarea = new Areafield [10] [10];
        //JPanel ingame_ = new JPanel ();
        //JButton ingame_ = new JButton (new ImageIcon (".jpg"));
        //Color ingame_color = new Color (165, 166, 158);
    
        Play ()
        {
            startup ();
            //ingame ();
        }
    
    
        void ingame ()  // add main menu button (quit) , edit enemy screen
        {
            ingame_window.setSize (1280, 1024); //Toolkit.getDefaultToolkit ().getScreenSize ().width, Toolkit.getDefaultToolkit ().getScreenSize ().height);
            ingame_window.getContentPane ().setLayout (new BorderLayout ());
            ingame_window.getContentPane ().setBackground (Color.black);
            ingame_areas.setLayout (new FlowLayout ());
            ingame_enemypanel.setLayout (new GridLayout (11, 11));
            ingame_friendlypanel.setLayout (new GridLayout (11, 11));
            ingame_enemyvehicledamage.setLayout (new GridLayout (5, 1, 0, 0));
            ingame_enemyvehicledamage.setSize (new Dimension (500, 1280));
            ingame_missilecount.setLayout (new GridLayout (5, 1, 0, 0));
            ingame_mainmenupnl.setLayout (new BorderLayout ());
            setAreafields (friendlyarea, enemyarea);
    
            ingame_enemypanel.setBackground (Color.black);
            ingame_friendlypanel.setBackground (Color.black);
            ingame_areas.setBackground (Color.black);
            ingame_enemyvehicledamage.setBackground (Color.black);
            ingame_mainmenupnl.setBackground (Color.black);
            ingame_mainmenu.setBackground (Color.black);
            ingame_mainmenu.setBorder (BorderFactory.createEmptyBorder ());
    
            ingame_enemyvehicledamage.add (ingame_veh1);
            ingame_enemyvehicledamage.add (ingame_veh2);
            ingame_enemyvehicledamage.add (ingame_veh3);
            ingame_enemyvehicledamage.add (ingame_veh4);
            ingame_enemyvehicledamage.add (ingame_veh5);
    
            ingame_missilecount.add (ingame_missile1);
            ingame_missilecount.add (ingame_missile2);
            ingame_missilecount.add (ingame_missile3);
            ingame_missilecount.add (ingame_missile4);
            ingame_missilecount.add (ingame_missile5);
    
            ingame_areas.add (ingame_enemypanel);
            ingame_areas.add (ingame_enemyvehicledamage);
            ingame_areas.add (ingame_friendlypanel);
            ingame_areas.add (ingame_missilecount);
    
            ingame_mainmenupnl.add ("West", ingame_mainmenu);
    
            ingame_window.getContentPane ().add ("North", ingame_controls);
            ingame_window.getContentPane ().add ("Center", ingame_mainmenupnl);
            ingame_window.getContentPane ().add ("South", ingame_areas);
            ingame_mainmenu.addActionListener (this);
            //ingame_.setBackground (Color.black);
            //ingame_.setBorder (BorderFactory.createEmptyBorder ());
            //ingame_.addActionListener (this);
    
            ingame_window.setLocationRelativeTo (null);
            ingame_window.setVisible (true);
        }
    
    
        void instructions ()
        {
            instructions_window.setSize (700, 500);
            instructions_window.getContentPane ().setLayout (new BorderLayout ());
            instructions_backpnl.setBackground (Color.black);
            instructions_backpnl.setLayout (new FlowLayout ());
            instructions_backbtn.setBackground (Color.black);
            instructions_backpnl.add (instructions_backbtn);
            instructions_backbtn.setBorder (BorderFactory.createEmptyBorder ());
            instructions_backbtn.addActionListener (this);
            instructions_window.getContentPane ().add ("Center", instructions_bg);
            instructions_window.getContentPane ().add ("South", instructions_backpnl);
            instructions_window.setLocationRelativeTo (null);
            instructions_window.setVisible (true);
        }
    
    
        void startup ()
        {
            startup_window.setSize (735, 500);
            startup_window.getContentPane ().setLayout (new BorderLayout ());
            startup_buttons.setBackground (Color.black);
            startup_buttons.setLayout (new FlowLayout ());
            startup_buttons.add (startup_instructionsbtn);
            startup_buttons.add (startup_singleplayer);
            startup_buttons.add (startup_multiplayer);
            startup_buttons.add (startup_exit);
            startup_instructionsbtn.setBackground (Color.black);
            startup_singleplayer.setBackground (Color.black);
            startup_multiplayer.setBackground (Color.black);
            startup_exit.setBackground (Color.black);
            startup_instructionsbtn.setBorder (BorderFactory.createEmptyBorder ());
            startup_singleplayer.setBorder (BorderFactory.createEmptyBorder ());
            startup_multiplayer.setBorder (BorderFactory.createEmptyBorder ());
            startup_exit.setBorder (BorderFactory.createEmptyBorder ());
            startup_instructionsbtn.addActionListener (this);
            startup_singleplayer.addActionListener (this);
            startup_multiplayer.addActionListener (this);
            startup_exit.addActionListener (this);
            startup_window.getContentPane ().add ("Center", startup_mainbg);
            startup_window.getContentPane ().add ("South", startup_buttons);
            startup_window.setLocationRelativeTo (null);
            startup_window.setVisible (true);
        }
    
    
        void setAreafields (Areafield[] [] friendly, Areafield[] [] enemy)
        {
            ingame_enemypanel.add (ingame_neblank);
            ingame_enemypanel.add (ingame_ne1);
            ingame_enemypanel.add (ingame_ne2);
            ingame_enemypanel.add (ingame_ne3);
            ingame_enemypanel.add (ingame_ne4);
            ingame_enemypanel.add (ingame_ne5);
            ingame_enemypanel.add (ingame_ne6);
            ingame_enemypanel.add (ingame_ne7);
            ingame_enemypanel.add (ingame_ne8);
            ingame_enemypanel.add (ingame_ne9);
            ingame_enemypanel.add (ingame_ne10);
            ingame_enemypanel.add (ingame_nea);
            ingame_friendlypanel.add (ingame_nfblank);
            ingame_friendlypanel.add (ingame_nf1);
            ingame_friendlypanel.add (ingame_nf2);
            ingame_friendlypanel.add (ingame_nf3);
            ingame_friendlypanel.add (ingame_nf4);
            ingame_friendlypanel.add (ingame_nf5);
            ingame_friendlypanel.add (ingame_nf6);
            ingame_friendlypanel.add (ingame_nf7);
            ingame_friendlypanel.add (ingame_nf8);
            ingame_friendlypanel.add (ingame_nf9);
            ingame_friendlypanel.add (ingame_nf10);
            ingame_friendlypanel.add (ingame_nfa);
            for (int cnt1 = 0 ; cnt1 < 10 ; cnt1++)
                for (int cnt2 = 0 ; cnt2 < 10 ; cnt2++)
                {
                    if (cnt1 == 1 && cnt2 == 0)
                    {
                        ingame_enemypanel.add (ingame_neb);
                        ingame_friendlypanel.add (ingame_nfb);
                    }
                    if (cnt1 == 2 && cnt2 == 0)
                    {
                        ingame_enemypanel.add (ingame_nec);
                        ingame_friendlypanel.add (ingame_nfc);
                    }
                    if (cnt1 == 3 && cnt2 == 0)
                    {
                        ingame_enemypanel.add (ingame_ned);
                        ingame_friendlypanel.add (ingame_nfd);
                    }
                    if (cnt1 == 4 && cnt2 == 0)
                    {
                        ingame_enemypanel.add (ingame_nee);
                        ingame_friendlypanel.add (ingame_nfe);
                    }
                    if (cnt1 == 5 && cnt2 == 0)
                    {
                        ingame_enemypanel.add (ingame_nef);
                        ingame_friendlypanel.add (ingame_nff);
                    }
                    if (cnt1 == 6 && cnt2 == 0)
                    {
                        ingame_enemypanel.add (ingame_neg);
                        ingame_friendlypanel.add (ingame_nfg);
                    }
                    if (cnt1 == 7 && cnt2 == 0)
                    {
                        ingame_enemypanel.add (ingame_neh);
                        ingame_friendlypanel.add (ingame_nfh);
                    }
                    if (cnt1 == 8 && cnt2 == 0)
                    {
                        ingame_enemypanel.add (ingame_nei);
                        ingame_friendlypanel.add (ingame_nfi);
                    }
                    if (cnt1 == 9 && cnt2 == 0)
                    {
                        ingame_enemypanel.add (ingame_nej);
                        ingame_friendlypanel.add (ingame_nfj);
                    }
                    friendly [cnt1] [cnt2] = new Areafield ();
                    friendly [cnt1] [cnt2].addActionListener (this);
                    friendly [cnt1] [cnt2].setPreferredSize (new Dimension (40, 40));
                    ingame_friendlypanel.add (friendly [cnt1] [cnt2]);
                    enemy [cnt1] [cnt2] = new Areafield ();
                    enemy [cnt1] [cnt2].addActionListener (this);
                    enemy [cnt1] [cnt2].setPreferredSize (new Dimension (40, 40));
                    ingame_enemypanel.add (enemy [cnt1] [cnt2]);
                }
        }
    
    
        void multiplayer ()  // Vra meneer wat fout is m.a.w. die klomp keer wat die message op pop.
        {
            JOptionPane.showMessageDialog (null, "Multiplayer function is not yet enabled.", "", JOptionPane.INFORMATION_MESSAGE);
        }
    
    
        public void actionPerformed (ActionEvent e)
        {
            if (e.getSource () == startup_instructionsbtn)
            {
                startup_window.dispose ();
                instructions ();
            }
            if (e.getSource () == instructions_backbtn)
            {
                instructions_window.dispose ();
                startup ();
            }
            if (e.getSource () == startup_multiplayer)
            {
                multiplayer ();
            }
            if (e.getSource () == startup_singleplayer)
            {
                startup_window.dispose ();
                ingame ();
            }
            if (e.getSource () == startup_exit)
            {
                System.exit (0);
            }
            if (e.getSource () == ingame_mainmenu)
            {
                ingame_window.dispose ();
                startup ();
            }
            for (int cnt1 = 0 ; cnt1 < 10 ; cnt1++)
                for (int cnt2 = 0 ; cnt2 < 10 ; cnt2++)
                {
                    if (e.getSource () == friendlyarea [cnt1] [cnt2])
                        friendlyarea [cnt1] [cnt2].clicked ();
                    if (e.getSource () == enemyarea [cnt1] [cnt2])
                        enemyarea [cnt1] [cnt2].clicked ();
                }
            ingame_window.toFront ();
            startup_window.toFront ();
            instructions_window.toFront ();
        }
    
    
        public static void main (String[] args)
        {
            Play x = new Play ();
        }
    }
    Also, I don't really understand what you mean by multithreading...?

    Please help me and thanks so far!

  6. #6
    louwhopley is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Default

    What i meant was that I dont understand how to split those coding up and put it in different methods eg actionPerformed etc. This program must be able to send and receive constantly and update the gameplay - like which button was pressed...

    I also cant figure out how to make the program decide who must be the "server" and who must be the "client". For example we both open this program on our separate PCs over a LAN and then there must probably be a create or join button....?

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Both computers can be both clients and servers at the same time, or you can have a dedicated server. You need to decide which works best for how you want to play. If you want to play games ad-hoc, then you need to build in client and server functionality into every copy of the game, and one person can host and others join. If you want a dedicated server, then every copy of the game is just a client, and a custom server app you write exists somewhere on the network, and all game clients connect to it.

  8. #8
    louwhopley is offline Member
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Lightbulb

    Oh ok, that makes sense... It is a really small app so I guess it would work best if both server & client functionality is in each copy.

    I have worked on a little LAN chat program, just to learn how it works and where to wright each statement like:
    Java Code:
     public void actionPerformed (ActionEvent e)
        {
              BufferReader in = new...
        }
    So if I understand where to wright what here then I think I would certainly know what to wright where in my Battleship project.

    Can you please look thru my Chat program and tell me where I have written the stuff on the wrong place etc...?

    Java Code:
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Main implements ActionListener
    {
    
        static JTextArea ta1 = new JTextArea (10, 10);
        static JButton btn1 = new JButton ("Click me");
        static JTextField txt1 = new JTextField (10);
        static String newMessage = "\n" + "   ------------------------------------------------" + "\n";
        static int socketNumber = 3333;
        static String IP;
        static ServerSocket mySocket;
        static Socket connectionSocket;
        static DataOutputStream outStream;
        static DataInputStream inStream;
        static BufferedReader inReader;
        static BufferedReader outReader;
    
        Main ()
        {
            try
            {
                mySocket = new ServerSocket (socketNumber);
            }
            catch (IOException abc)
            {
                ta1.append ("   Can't connect!" + "\n   " + abc + newMessage);
            }
            // Die GUI se uitleg
            JFrame myframe = new JFrame ("Chatr BOX");
            myframe.setSize (500, 500);
            myframe.getContentPane ().setBackground (Color.red);
            BorderLayout border = new BorderLayout ();
            myframe.getContentPane ().setLayout (border);
            btn1.setBackground (Color.blue);
            btn1.setForeground (Color.white);
            btn1.addActionListener (this);
            txt1.addActionListener (this);
            ta1.setEditable (false);
            ta1.setText ("   Type \"host\" to host a session\n   Type \"join\" to join a session" + newMessage);
            myframe.getContentPane ().add (btn1);
            myframe.getContentPane ().add (txt1);
            myframe.getContentPane ().add (ta1);
            myframe.getContentPane ().add ("North", txt1);
            myframe.getContentPane ().add ("South", btn1);
            myframe.getContentPane ().add ("Center", ta1);
            myframe.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            myframe.setLocationRelativeTo (null);
            myframe.setVisible (true);
            // Einde van die GUI se uitleg
        }
    
    
        public void actionPerformed (ActionEvent e)
        {
            if (e.getSource () == btn1)
            {
                String text = txt1.getText ();
                if (text.equals ("join"))
                {
                    try
                    {
                        IP = JOptionPane.showInputDialog ("Type the IP you wish to connect to (e.g. 192.168.0.15)");
                        connectionSocket = new Socket (IP, socketNumber);
                        outStream = new DataOutputStream (connectionSocket.getOutputStream ());
                        inReader = new BufferedReader (new InputStreamReader (connectionSocket.getInputStream ()));
                        ta1.append ("   Succesfully connected to:\n   " + IP + newMessage);
                    }
                    catch (IOException abc)
                    {
                        ta1.append ("   Can't connect!" + "\n   " + abc + newMessage);
                    }
                }
                else
                    if (text.equals ("host"))
                    {
                        try
                        {
                            connectionSocket = mySocket.accept ();
                            outStream = new DataOutputStream (connectionSocket.getOutputStream ());
                            inReader = new BufferedReader (new InputStreamReader (connectionSocket.getInputStream ()));
                            ta1.append ("   Succesfully hosted" + newMessage);
                        }
                        catch (IOException abc)
                        {
                            ta1.append ("   Can't connect!" + "\n   " + abc + newMessage);
                        }
                    }
                    else
                        if (connectionSocket == null)
                        {
                            ta1.append ("   You are not connected to any chat sessions\n   Type \"host\" to host a session\n   Type \"join\" to join a session" + newMessage);
                        }
                        else
                        {
                            try
                            {
                                outStream.writeBytes (text);
                            }
                            catch (IOException abc)
                            {
                                ta1.append ("   Unable to send!" + "\n   " + abc + newMessage);
                            }
                            ta1.append ("   Sent: " + text + newMessage);
                        }
                txt1.setText ("");
            }
        }
    
    
        static void receiveText ()
        {
            while (true)
            {
                if (connectionSocket != null)
                {
                    try
                    {
                        inReader = new BufferedReader (new InputStreamReader (connectionSocket.getInputStream ()));
                        ta1.append ("   Received: " + inReader.readLine () + newMessage);
                    }
                    catch (IOException abc)
                    {
                        ta1.append ("   Can't connect!" + "\n   " + abc + newMessage);
                    }
                }
            }
        }
    
    
        public static void main (String[] args) throws Exception
        {
            Main xyz = new Main ();
            receiveText ();
        }
    }
    Once again, THANK YOU quad64bit for all your help you are offering!

Similar Threads

  1. Help with simple Blackjack Program?
    By meeper3000 in forum New To Java
    Replies: 2
    Last Post: 04-26-2009, 10:46 AM
  2. simple chat program
    By munishmhr in forum Networking
    Replies: 2
    Last Post: 03-25-2009, 05:00 PM
  3. Simple Program
    By TheRocket in forum Advanced Java
    Replies: 15
    Last Post: 12-30-2008, 03:35 PM
  4. Stuck - simple program
    By dirtycash in forum New To Java
    Replies: 4
    Last Post: 11-24-2008, 08:44 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 10:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •