Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default 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();
          }
        }

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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...

  3. #3
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    what do u mean by threads ?

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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!

  5. #5
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    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();
    	}
        }
    }

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    It has a problem with 'socket'
    Please clarify: what exactly do you mean by "a problem"?

    Java Code:
    public class ConnectFourServer {
        public static void main(String[] args) throws IOException {
            
            //....
            while (listening){
            	 new ThreadExample(ss.accept()).start();
            }
                ss.close();
          }
        }
    Isn't that going to make a hell of a lot of threads?

  7. #7
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    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

  8. #8
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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

  1. Ireport: connection to SQL server refused
    By kolonaki in forum Advanced Java
    Replies: 2
    Last Post: 04-14-2010, 05:13 PM
  2. sql server connection problem
    By m.ali in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-22-2010, 06:09 PM
  3. Remote SQL Server Connection
    By saso1310 in forum JDBC
    Replies: 2
    Last Post: 05-15-2009, 07:39 AM
  4. Connection the sql server
    By Preethi in forum New To Java
    Replies: 7
    Last Post: 06-26-2008, 03:05 PM
  5. Sql server connection problem
    By tanvirtonu in forum JDBC
    Replies: 1
    Last Post: 03-31-2008, 12:46 PM

Posting Permissions

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