GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Oct-2005, 12:55
collinm collinm is offline
New Member
 
Join Date: Mar 2005
Posts: 20
collinm is on a distinguished road

two server and many client....


hi

what i want to do is to create to start 2 tread of server in my class main server....
and each class server start a thread for each connection...

example server1 start with port 4444 and server2 with port 4445

all client connect to server1 except if the server is down...

my code

ServerMain class
JAVA Code:
import java.net.*;
import java.io.*;

public class ServerMain {

    public static void main(String[] args) throws IOException {        
        Server server1 = new Server(4444);
        Server server2 = new Server(4445);
        server1.start();
        server2.start();
   }
}

Server class
JAVA Code:
import java.net.*;
import java.io.*;

public class Server extends Thread{

    private static int nbRequest=0;

    public synchronized int addRequest(){
        return nbRequest++;
    }

    public synchronized int getRequest(){
        return nbRequest;
    }
    
    public int port=0;
    
    public Server(int port) {
        super("Server");
        this.port = port;
    }

    public void run() {
        ServerSocket serverSocket = null;
        Socket s=null;
        boolean listening = true;
        try {
            serverSocket = new ServerSocket(port);
            s=serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Could not listen on port.");
            System.exit(-1);
        }
        System.out.println("server run, wait connecting....");
        while (listening)
            new ServerThread(s).start();

        //serverSocket.close();
    }
}

JAVA Code:
import java.net.*;
import java.io.*;

public class ServerThread extends Thread {

    private Socket socket = null;
    public ServerThread(Socket socket) {
        super("ServerThread");
        this.socket = socket;
    }

    public void run() {

      try {
          System.out.println("connexion successfull");
          PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
          BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                            socket.getInputStream()));

          String inputLine, outputLine;
          while ((inputLine = in.readLine()) != null) {
            System.out.println("Server: " + inputLine);
            inputLine = inputLine.toUpperCase();
            out.println(inputLine);
            if (inputLine.equals("Bye."))
                  break;
            }
            out.close();
            in.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

client class
i need to add function to be able to try during 10 second to connect to server 1(4444).... i the server don't respond... i try to connect to server2 (port 4445)

JAVA Code:
import java.io.*;
import java.net.*;
import java.lang.*;

public class Client {
    public static void main(String[] args) throws IOException {
        String serverHostname = new String ("127.0.0.1");
        int port=4444
        System.out.println ("Essai de se connecter a  l'hote " + serverHostname + " au port. " + port);
        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            echoSocket = new Socket(serverHostname, port);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Hote inconnu: " + serverHostname);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Ne pas se connecter au serveur: " + serverHostname);
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String userInput;
        System.out.print ("Entree: ");
        while ((userInput = stdIn.readLine()) != null) {
            out.println(userInput);
            System.out.println("echo: " + in.readLine());
            System.out.print ("Entree: ");
        }
        out.close();
        in.close();
        stdIn.close();
        echoSocket.close();
    }
}


i start ServerMain

and after i start i client

the server write a lot of:
connexion successfull...

any idea?

thanks
Last edited by LuciWiz : 07-Oct-2005 at 00:10. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 10-Oct-2005, 00:30
fortytwo fortytwo is offline
New Member
 
Join Date: Oct 2005
Posts: 3
fortytwo is on a distinguished road

Re: two server and many client....


Gee, could it be because you have an infinite cycle ?

JAVA Code:
while (listening)
            new ServerThread(s).start();

You probably need something like

JAVA Code:
while (listening){
     s=serverSocket.accept();
     new ServerThread(s).start();
}

otherwise you are just launching an infinite number of ServerThreads for the same socket.
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:43.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.