0
votes

I want to create a Client/Server application on java, server with an IP address 192.168.1.100 waiting for a client request on port 4500.

the client reads the string from the keyboard, sends a connection request to the server. once the connection is established, it sends the string to the server.

this is the code I tried :

for Serveur:

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

public class Serveur {

    public Serveur() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try
        {
            ServerSocket ecoute;
            ecoute = new ServerSocket(1111);
            Socket service = null;
            System.out.println("Serveur en attente d'un client !");
            while(true)
            {

                service = ecoute.accept();
                System.out.println("Client connécté !");
                DataInputStream is = new DataInputStream(service.getInputStream());
                System.out.println("Client dit : " + is.readUTF().toUpperCase());
                service.close();
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();

        }
    }

}

for client :

import java.net.*;
import java.io.*;
import java.util.Scanner;

public class Client {

    public Client() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Connexion en cours ...");
        try {
            Socket s = new Socket("localhost", 1111);
            DataOutputStream os = new DataOutputStream(s.getOutputStream());
            Scanner sn = new Scanner(System.in);
            os.writeUTF(sn.nextLine());
        } catch ( IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Connécté au serveur !");

    }

}

but I had a problem with this code :

java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.(Unknown Source) at java.net.Socket.(Unknown Source) at Client.main(Client.java:18)

2
At which side this exception is coming?? - Vishal K
@VishalK the client side - Yukina Yukinatte
I have another question how can I send some message from the server to the client ? - Yukina Yukinatte

2 Answers

1
votes

Your code runs correctly on my machine.

It seems that the issue here is not with your code, but with your network setup.

You should:

  1. Ensure that your IP address and port settings are correct
  2. Check your firewall settings on both computers and make sure they aren't blocking the traffic
  3. Ensure that your hosts file is correct (C:\Windows\system32\drivers\etc\hosts on Windows) The localhost entry may be incorrectly set.
0
votes

Without more information about your environment, it will be difficult to give you a definitive answer.

However, the most common reason for this is that you haven't properly forwarded the ports on your router. Doing so will be different depending on the manufacturer of your router, but, generally speaking, you will need to forward port 1111 to local IP address 192.168.1.100 (or whatever the local IP is of the machine you're running the Serveur on).

(Side note: I'm not sure why you say your server is listening for requests on port 4500 when your code says everything is set up for 1111.)