1

I wrote a simple client-server application. It works very well on my computer. but when my friend tries to connect my server he can't. I create the server on my computer with port 23. Here is the part of creating the server:

public Server(int port_number) throws IOException{

        create_Server(port_number);
    }

    public static void main(String[] args) throws IOException {

        int port_number=23;

        new Server(port_number);
    }

    private void create_Server(int port_number) throws IOException{

        ss = new ServerSocket(port_number);

        System.out.println("Server is ready!");

        while(true){

            s=ss.accept();

            System.out.println(s.getLocalAddress().getHostName() + " was connected!");

                        send_con_mes();

            list.put(s,new DataOutputStream(s.getOutputStream()) );

            new ServerThread(s,this).start();
        }

    }

and here is the client part ;

public void start_Chat() {

    try {

        Ip_addr = JOptionPane.showInputDialog("Enter the IP number of the server to connect : ");
        s = new Socket(Ip_addr, 23);

        Client_name = JOptionPane.showInputDialog("Enter your Nickname : ");

        dis = new DataInputStream(s.getInputStream());         
        dos = new DataOutputStream(s.getOutputStream());
        new Thread(Client.this).start();

well I can talk, send private messages etc. When I connect to server on my computer as clients, but the final problem is a client from another IP cannot get connected.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
quartaela
  • 2,579
  • 16
  • 63
  • 99
  • 1
    Did You add a port-forwarding for you´re port in the router/firewall/both? – Juarrow Sep 06 '11 at 14:22
  • nope_?. i am new in network with java. – quartaela Sep 06 '11 at 14:23
  • 1
    I don't think it's a java problem, your code seems correct, it seems more like a normal network issue. Is your friend on your same network? Do you have windows? and in that case, do you have windows firewall active? – Simone Gianni Sep 06 '11 at 14:27
  • @KevinDTimm this is the only socket lines which shows to connect i mean just the little part. i can share the all code it will seem very complex :) – quartaela Sep 06 '11 at 14:30
  • @Simone no my friend is not on my network. and yes i am using Windows 7 but firewall doesnt active_? – quartaela Sep 06 '11 at 14:31
  • can your friend connect to anything else on your computer? like, if you run an Apache server or similar. – Simone Gianni Sep 06 '11 at 14:36
  • well i have never used any server programmes before and i am very new in network programming on java. so we didnt try to connect each other. – quartaela Sep 06 '11 at 14:38

1 Answers1

2

You have to configure your network to allow this port to be accessed. This means enabling firewall on you PC, and your routers etc. There is nothing you can do in Java to avoid having to get this right first.

EDIT: If the other machine is trying to connect to you via an Internet router they will have to use your public IP address rather than your internal PC address. If you don't know your public address you can use a site like http://whatismyipaddress.com/. Unless you have a static IP address it can change when you reconnect. (One reason to stay connected all the time)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • ok then i will enable firewall and try to do this port-forwarding subject. thanks for your help :) – quartaela Sep 06 '11 at 14:32
  • "stay connected all the time" will not necessarily prevent changes in the public IP address. If this were pssible they could give you a static IP right away. The point is, there aren't enough IPs. – Ingo Sep 06 '11 at 14:53
  • AFAIK, Your ISP can't change your IP address while you are connected and not have you lose all your connections. They can force you to disconnect and give you a different IP address when you reconnect. Your ISP can also give an ISP internal use only address which cannot be use publicly (only within the same ISP) e.g. 10.x.x.x – Peter Lawrey Sep 06 '11 at 15:37