Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Multiple clients, single server, No threading

FatimaFatima BangladeshPosts: 1

i am attempting to wwrite a code for single server multiple clients without threading. my logic is to use different for numbers.
my code runs perfectly for single server single client. how can i modify this to incorporate multiple client w/o threading?
[code]
package tcpsmg;

import java.io.*;
import java.net.*;

class TCPServer
{
public static void main(String argv[]) throws Exception
{
int clientServerPorts[]=new int[5];
clientServerPorts[0]=6789;
clientServerPorts[1]=8002;
clientServerPorts[2]=8003;

            int clientFlag =0;

    String clientSentence;
    String capitalizedSentence;


            ServerSocket welcomeSocket= new ServerSocket(6789); clientFlag =1;
            /*
            ServerSocket welcomeSocket2= new ServerSocket(8002); clientFlag =2;
            ServerSocket welcomeSocket3= new ServerSocket(8003); clientFlag =3;
            */
    while (true)
    { 

         //  if(clientFlag == 1)
          // {Socket connectionSocket=welcomeSocket.accept();}
           // else if(clientFlag == 2) { Socket connectionSocket=welcomeSocket2.accept();}
           // else {Socket connectionSocket=welcomeSocket3.accept(); }

           Socket connectionSocket=welcomeSocket.accept();
            BufferedReader inFromClient=
            new BufferedReader(new InputStreamReader(
                connectionSocket.getInputStream()));
        DataOutputStream outToClient=
            new DataOutputStream(
                connectionSocket.getOutputStream());
        clientSentence=inFromClient.readLine();
        System.out.println("From Client: "+clientSentence);


                    //processing done here
        capitalizedSentence=
            clientSentence.toUpperCase()+'\n';

        outToClient.writeBytes(capitalizedSentence);

    }

}

}

package tcpsmg;

import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;

            BufferedReader inFromUser=
        new BufferedReader(new InputStreamReader(System.in));

    InetAddress inetAddress=InetAddress.getLocalHost();
    //.getByName(String hostname); "CL11"
    System.out.println(inetAddress);



            /*
    if (clientFLag==1) {Socket clientSocket = new Socket(inetAddress,6789);}
    else if (clientFLag==2) {Socket clientSocket = new Socket(inetAddress,8002);}
    else {Socket clientSocket = new Socket(inetAddress,8003);}




    */

    Socket clientSocket = new Socket(inetAddress,6789);
    DataOutputStream outToServer=
        new  DataOutputStream(clientSocket.getOutputStream());

    BufferedReader inFromServer=
        new  BufferedReader(new InputStreamReader
                        (clientSocket.getInputStream()));


    sentence=inFromUser.readLine();
    outToServer.writeBytes(sentence+'\n');

    modifiedSentence=inFromServer.readLine();
    System.out.println("From Server: "+modifiedSentence );
    clientSocket.close();


}

}

[/code]

Sign In or Register to comment.