Results 1 to 6 of 6
Thread: Client Connect to Self
- 07-30-2011, 10:15 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
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:
HOPE you can help me..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; } }
Thanks!
- 07-30-2011, 10:47 PM #2
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!
- 07-31-2011, 08:56 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
Another question? How can I enabled 2 players via internet? Is there any way?
- 07-31-2011, 01:52 PM #4
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?How can I enabled 2 players via internet
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?
- 07-31-2011, 04:30 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
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..
- 07-31-2011, 04:35 PM #6
I still do not understand what you mean by "connect to the internet"?is this possible to 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
-
How to create remote client to connect with MS Access?
By Constance in forum New To JavaReplies: 9Last Post: 10-19-2012, 04:16 AM -
Any idea on how to connect to database from client side?
By jing-yi in forum New To JavaReplies: 1Last Post: 07-01-2011, 04:59 PM -
How to block Client request to connect with server
By mfaizan24 in forum NetworkingReplies: 3Last Post: 02-28-2010, 07:26 PM -
Client Socket Fails to Connect to ServerSocket
By Sir_Fz in forum NetworkingReplies: 4Last Post: 11-03-2009, 07:44 AM -
Server not able to connect client
By Kapil Gupta in forum New To JavaReplies: 15Last Post: 07-22-2008, 04:09 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks