Results 1 to 8 of 8
Thread: Server Connection Code
- 05-27-2010, 04:26 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Server Connection Code
Hi
I'm working on a connect 4 game which is must be applied on a network (server and client).
The first thing, I wrote a server connection class. This class is for chatting between two people.
First when the two people connect to the server, the server will send number 1 to the first person and number 2 to the second person. Also, I must allow the person who gets number 1 chatting first and the second one can not chat until the first one done. Also, when the person number 1 sends any thing, it must be appeared in the screen of person number 2 and vise versa.
The problems are:
1) when the person number 1 sends any thing, it show up in the screen of the second person but the problem when the second person sends any thing, it doesn't show up in the screen of the first one.
2) I couldn't make the person who gets number 1 to play first, that mean the person who gets number 2 can start chatting before the first one and this is forbidden.
This is my code
Java Code:import java.net.*; import java.io.*; public class ConnectFourServer { public static void main(String[] args) throws IOException { //ConnectProtocol board = new ConnectProtocol(); ServerSocket ss = null; try { ss = new ServerSocket(1550); } catch (IOException e) { System.err.println("Could not listen on port: 1550."); System.exit(1); } Socket cs1 = null; Socket cs2 = null; try { cs1 = ss.accept(); cs2 = ss.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } BufferedReader in = null; PrintWriter out = null; BufferedReader in1 = null; PrintWriter out1 = null; try { in = new BufferedReader (new InputStreamReader(cs1.getInputStream())); out = new PrintWriter (cs1.getOutputStream(), true /* autoFlush */); in1 = new BufferedReader (new InputStreamReader(cs2.getInputStream())); out1 = new PrintWriter (cs2.getOutputStream(), true /* autoFlush */); out.println( "1" ); out1.println( "2" ); boolean done = false; String line=""; String line1=""; while (!done) { line = in.readLine(); if (line == null ) { done = true; } else { out.println("Echo: " + line); out1.println("Echo: " + line); if (line.trim().equals("BYE")) { done = true; } } }//while while (!done) { line1 = in1.readLine(); if ( line1 == null) { done = true; } else { out.println("Echo: " + line1); out1.println("Echo: " + line1); if (line1.trim().equals("BYE")) { done = true; } } }//while } catch (IOException e) { System.err.println("Unable to read from or write to the client: " + e.getMessage()); } cs1.close(); ss.close(); } }
- 05-27-2010, 04:46 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
you're in your first while(!done) loop. until the first guys says "BYE", the second guy's stuff won't get read at all.
you need multiple threads for this...
- 05-27-2010, 11:13 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
what do u mean by threads ?
- 05-27-2010, 11:30 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
by threads, I mean separate parts of your program that execute concurrently. That way, both your while loops could execute at the same time, without one blocking the other.
Google a "java thread tutorial" and start there. Good luck!
- 05-28-2010, 12:30 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Thank u very much and appreciate that.
I read about thread and wrote the following codes depending on what I understood.
It has a problem with 'socket'
Java Code:import java.net.*; import java.io.*; public class ConnectFourServer { public static void main(String[] args) throws IOException { //ConnectProtocol board = new ConnectProtocol(); ServerSocket ss = null; boolean listening = true; try { ss = new ServerSocket(1550); } catch (IOException e) { System.err.println("Could not listen on port: 1550."); System.exit(1); } while (listening){ new ThreadExample(ss.accept()).start(); } ss.close(); } }Java Code:import java.net.*; import java.io.*; public class ThreadExample extends Thread { private Socket socket = null; private Socket socket1 = null; public ThreadExample(Socket socket, Socket socket1) { super("ThreadExample"); this.socket = socket; this.socket = socket1; } public void run() { try { PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); PrintWriter out1 = new PrintWriter(socket1.getOutputStream(), true); BufferedReader in1 = new BufferedReader( new InputStreamReader( socket1.getInputStream())); out.println( "1" ); out1.println( "2" ); boolean done = false; String line=""; // String line1=""; while (!done) { line = in.readLine(); if (line == null ) { done = true; } else { out.println("Echo: " + line); out1.println("Echo: " + line); if (line.trim().equals("BYE")) { done = true; } } }//while out.close(); in.close(); socket.close(); socket1.close(); } catch (IOException e) { e.printStackTrace(); } } }
- 05-28-2010, 01:10 AM #6
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Please clarify: what exactly do you mean by "a problem"?
Isn't that going to make a hell of a lot of threads?Java Code:public class ConnectFourServer { public static void main(String[] args) throws IOException { //.... while (listening){ new ThreadExample(ss.accept()).start(); } ss.close(); } }
- 05-28-2010, 01:18 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
What I want is that if each one sends a message, this message shows up in the screen of the second person and vise versa.
I use 2 sockets to give each person 1 socket and this is problem that I think
Should I use one socket or two ? If two, how ?
I have tried many times to solve this problem but I couldn't
- 05-28-2010, 02:26 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
That was some quick turnaround on learning threads! respect!
the approach you're taking (two sockets each) is not very generic, but it'll work for 2 clients connecting to your server. make this work first, then think how to extend it further.
For now, what you should try to do is something like this:
Java Code:// in the main class, not inside any loop Socket s1 = ss.accept(); Socket s2 = ss.accept(); (new ThreadExample(s1, s2)).start(); (new ThreadExample(s2, s1)).start();
Similar Threads
-
Ireport: connection to SQL server refused
By kolonaki in forum Advanced JavaReplies: 2Last Post: 04-14-2010, 05:13 PM -
sql server connection problem
By m.ali in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-22-2010, 06:09 PM -
Remote SQL Server Connection
By saso1310 in forum JDBCReplies: 2Last Post: 05-15-2009, 07:39 AM -
Connection the sql server
By Preethi in forum New To JavaReplies: 7Last Post: 06-26-2008, 03:05 PM -
Sql server connection problem
By tanvirtonu in forum JDBCReplies: 1Last Post: 03-31-2008, 12:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks