0
votes

This is the code for my client and server.

class Client1 { Client1(int no) { try { String message; message="Hello this is client "+no; byte[] b =message.getBytes(); DatagramPacket dp = new DatagramPacket(b, b.length,InetAddress.getLocalHost(),3700); DatagramSocket sender = new DatagramSocket(); sender.send(dp); }catch (Exception e) { System.out.println("client shutdown"); } } }

Then my server class is

class Server1 {

int cnt=0;
String s1;
Server1()
{

    try {
            byte[] buffer = new byte[65536];
            DatagramPacket  incoming = new DatagramPacket(buffer, buffer.length);
            DatagramSocket  ds = new DatagramSocket(3700);
            ds.receive(incoming);
            byte[] data = incoming.getData();
            String s = new String(data, 0, incoming.getLength());
            System.out.println("Port" + incoming.getPort() + " on " + incoming.getAddress() + " sent this message:");

            System.out.println(s.toUpperCase());
            }

            catch (IOException e) 
            {
            System.err.println(e); 
            }
}   

}

Then my runnable implementation is

class prothread implements Runnable {

//long time=0;
    //int portno;
    int flag=0; // this is to differentiate between a server and client
    private String capitalizedSentence;
prothread(long l)
{
    if(l==1)
        { // it is a server
            flag=1;
        }
        else
        {
            flag=(int) l;
        }
}

@Override
public void run(){
    // TODO Auto-generated method stub

        System.out.println("Starting thread");    
        if(flag==1)// Code for server
            {
           Server1 s=new Server1();

            }
            else   // code for client
            {                   
              Client1 c=new Client1(flag);

             }

    }

}

Finally the class which deploys this client and server is

public class Samplepro31 {

public static void main(String[] args) {
    // First i'm going to create a server and then clients for it
        int i=1;
        int cnt=0;

         prothread[] p;
        Thread[] th;
        Random r =new Random();
         // Array has been declared 
        p=new prothread[10];// Memory allocated to it
        th= new Thread[1000];
        p[0]=new prothread(1);
        cnt=1;
        //p[0].setportno(cnt);
        th[0]=new Thread(p[0]);
        th[0].start();
        while(cnt<3)
        {

                p[cnt]=new prothread(cnt); 
                // here send the port number
                th[cnt]=new Thread(p[cnt]);
                //p[cnt1].setportno(cnt1);
                th[cnt].start();
                cnt++;
        }

  }

}

So problem I'm having is one server and only one client is running at a time instead 2 clients should be running the o/p i'm getting is :

Starting thread Starting thread Starting thread Inside clinet's constructor 2 java.net.BindException: Address already in use: Cannot bind HELLO THIS IS CLIENT 2

So can anybody tell me what I'm doing wrong?

1

1 Answers

0
votes

Don't bind the client to any particular port. Let the implementation select an available port to bind to.