简体   繁体   中英

different client from different pc communicate with server using ip in java

I am using java.nio package for my chat application. However, it can only connect different clients in the localhost only. How can I connect multiple clients from different IP addresses?

Currently I am connecting to my client using the following code:

InetAddress.getByName("10.10.10.172");

I would like to be able to send messages from a client that has an IP address of 10.10.10.123 to a server which has an IP address of 10.10.10.124.

Thanks!

I think its the same way as you did with single pc.......

One more thing before i start.. 10.10.10.777 & 10.10.10.888 are invalid IPs

IPv4 is in dotted decimal format from 0-255

0 - Network Address 255 - BroadCast Address

0 and 255 are not used for giving ip to a pc ... use 1 - 254.

And try using port above 1024 , these ports are know as Well- Known ports and are used for running various service, like SMTP, FTP, HTTP, TELNET, etc.....

Thinks to be remembered....

  1. Switch off your Firewall , or else add an exception to the rules in firewall for the port, where you want to run the server.

  2. Create separate thread to handle multiple client at the Server End..

Eg :

The Server side code example:


public class ServerTest {

    ServerSocket s;

    public void go() {

        try {
            s = new ServerSocket(44457);

            while (true) {

                Socket incoming = s.accept();
                Thread t = new Thread(new MyCon(incoming));
                t.start();
            }
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    class MyCon implements Runnable {

        Socket incoming;

        public MyCon(Socket incoming) {

            this.incoming = incoming;
        }

        @Override
        public void run() {

            try {
                PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
                        true);
                InputStreamReader isr = new InputStreamReader(
                        incoming.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                String inp = null;

                boolean isDone = true;

                System.out.println("TYPE : BYE");
                System.out.println();
                while (isDone && ((inp = br.readLine()) != null)) {

                    System.out.println(inp);
                    if (inp.trim().equals("BYE")) {
                        System.out
                                .println("THANKS FOR CONNECTING...Bye for now");
                        isDone = false;
                        s.close();
                    }

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                try {
                    s.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {

        new ServerTest().go();

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM