Results 1 to 6 of 6
  1. #1
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default Client Connect to Self

    Im just wondering if a client can be a host and a same time also.

    I got a 2 player board game. Its 100% working on 2 players via LAN..

    Now I just want to improve it and write some AI for 1 Players also, but my problem is that my code is only for 2 players because every activity is being send to the client..

    Now my question how can i make PROGRAM A to connect to PROGRAM A also using the 127.0.0.1 IP address?

    Heres the code of my socket:

    Java Code:
    import java.io.*;
    import java.net.*;
    import javax.swing.JTextField;
    
    public class Socketmaster
            implements Runnable
    {
    
            InetAddress address;
            private ServerSocket serverObject;
            private Socket socketObject;
            private String strLine;
            private BufferedReader inBuffer;
            private PrintWriter outBuffer;
            private JTextField inTextField;
            private String strServerIP;
            private String strInText;
            private int intSocket;
            private boolean blnConnected;
            private boolean blnServerMode;
    
            Socketmaster(JTextField jtextfield, int i)
            {
            address = null;
            serverObject = null;
            socketObject = null;
            strLine = "";
            inBuffer = null;
            outBuffer = null;
            strServerIP = "";
            strInText = "";
            intSocket = 0;
            blnConnected = false;
            blnServerMode = true;
            blnServerMode = true;
            inTextField = jtextfield;
            strServerIP = "";
            intSocket = i;
            }
    
            Socketmaster(JTextField jtextfield, String s, int i)
            {
            address = null;
            serverObject = null;
            socketObject = null;
            strLine = "";
            inBuffer = null;
            outBuffer = null;
            strServerIP = "";
            strInText = "";
            intSocket = 0;
            blnConnected = false;
            blnServerMode = true;
            InetAddress inetaddress = null;
            try
            {
                    inetaddress = InetAddress.getByName(s);
            }
            catch(UnknownHostException unknownhostexception)
            {
                    System.out.println("Host Name does not exist");
                    System.exit(1);
                    return;
            }
            blnServerMode = false;
            inTextField = jtextfield;
            strServerIP = inetaddress.getHostAddress();
            intSocket = i;
            }
    
            public void setString(String s)
            {
            strInText = s;
            }
    
            public void run()
            {
            if(blnServerMode)
            {
                    try
                    {
                    serverObject = new ServerSocket(intSocket);
                    }
                    catch(IOException ioexception)
                    {
                    System.out.println("Could not listen on port: " + intSocket);
                    System.exit(-1);
                    }
                    while(!blnConnected)
                    {
                    System.out.println("Listening for client");
                    try
                    {
                            socketObject = serverObject.accept();
                            blnConnected = true;
                    }
                    catch(IOException ioexception1)
                    {
                            System.out.println("Accept failed: " + intSocket);
                            System.exit(-1);
                    }
                    }
                    try
                    {
                    inBuffer = new BufferedReader(new InputStreamReader(socketObject.getInputStream()));
                    outBuffer = new PrintWriter(socketObject.getOutputStream(), true);
                    }
                    catch(IOException ioexception2)
                    {
                    System.out.println("in or out failed");
                    System.exit(-1);
                    }
            } else
            {
                    System.out.println("Connecting to server");
                    try
                    {
                    socketObject = new Socket(strServerIP, intSocket);
                    outBuffer = new PrintWriter(socketObject.getOutputStream(), true);
                    inBuffer = new BufferedReader(new InputStreamReader(socketObject.getInputStream()));
                    }
                    catch(UnknownHostException unknownhostexception)
                    {
                    System.out.println("Unknown host");
                    System.exit(1);
                    }
                    catch(IOException ioexception3)
                    {
                    System.out.println("No I/O");
                    System.exit(1);
                    }
            }
    
            System.out.println("Connected.  Waiting for message...");
            do
            {
                    try
                    {
                    do
                    {
                            while(strLine != null)
                            {
                            strLine = inBuffer.readLine();
                            System.out.println("Recieving Message: " + strLine);
                            strInText = strLine;
                            inTextField.setText(strLine);
                            inTextField.postActionEvent();
                            }
                            System.exit(-1);
                    } while(true);
                    }
                    catch(IOException ioexception4)
                    {
                    System.out.println("Read failed");
                    }
                    System.exit(-1);
            } while(true);
            }
    
            public boolean GetConnect()
            {
            return blnConnected;
            }
    
            public void sendText(String s)
            {
            if(inBuffer != null && outBuffer != null)
            {
                    System.out.println("Sending Message: " + s);
                    outBuffer.println(s);
            }
            }
    
            public String getText()
            {
            return strInText;
            }
    }
    HOPE you can help me..

    Thanks!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    If it's already working for two players, it seems like it would be very simple to implement an AI opponent. (Well, the AI itself might not be simple, but the networking part should be.) Open a port on the server as if you were waiting for another human opponent, but then launch the AI client in a thread that connects to the port and plays with you.
    Get in the habit of using standard Java naming conventions!

  3. #3
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default

    Another question? How can I enabled 2 players via internet? Is there any way?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    How can I enabled 2 players via internet
    Can you explain what you want to happen? Describe what you want if a player starts his program and then another player starts his program. What should happen?
    Do you have a server at a fixed IP address that the players can connect to?
    Or do all of the players know each others IP addresses?

  5. #5
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default

    If a player starts the program.. He can choose if he wants to be a host or a client. If he wants to be a host the program will wait to the client program.. If the player choose client it will ask for IP address of the server..

    Now my question, is this possible to connect to the internet? I dont have server but i have domain and shared hosting..

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    is this possible to connect to the internet?
    I still do not understand what you mean by "connect to the internet"?
    Do you mean you want a dedicated server that lives at an IP address that any player can connect to?
    The way you described your code must mean that it works on the internet. One player starts his program and chooses to be server. The other player starts his program as a client. The client program must have the internet IP address of the server.

Similar Threads

  1. How to create remote client to connect with MS Access?
    By Constance in forum New To Java
    Replies: 9
    Last Post: 10-19-2012, 04:16 AM
  2. Replies: 1
    Last Post: 07-01-2011, 04:59 PM
  3. How to block Client request to connect with server
    By mfaizan24 in forum Networking
    Replies: 3
    Last Post: 02-28-2010, 07:26 PM
  4. Client Socket Fails to Connect to ServerSocket
    By Sir_Fz in forum Networking
    Replies: 4
    Last Post: 11-03-2009, 07:44 AM
  5. Server not able to connect client
    By Kapil Gupta in forum New To Java
    Replies: 15
    Last Post: 07-22-2008, 04:09 AM

Posting Permissions

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