The Artima Developer Community
Sponsored Link

Java Answers Forum
two server and many client....

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Marc Collin

Posts: 1
Nickname: collinm
Registered: Oct, 2005

two server and many client.... Posted: Oct 6, 2005 11:49 AM
Reply to this message Reply
Advertisement
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

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

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



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)

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

Topic: Running thread in JFrame using Jpannel Previous Topic   Next Topic Topic: kindly help me to explain this...please

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use