0

i am trying to connect with Client Class to server that uses in another computer with another IP...i wrote the ip of the server and the same port in the Socket but there isn't connection...I succeed to connect to the server when i run the server and the Client in the same computer but when i wrote 127.0.0.1 in the ip

sorry on my lame english...

Server class:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import javax.swing.*;

   public class ClientSide   {
   Socket client;
   ObjectOutputStream out1;

  //////////////////////
JFrame frame=new JFrame("Client");
JTextArea ta=new JTextArea(20,10);
JButton btn=new JButton("Enter");
JTextArea send=new JTextArea(10,5);
 ///////////////////////


             /////Gui////
      ClientSide(){

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
     frame.setSize(600,500);

     JPanel p=new JPanel(new BorderLayout());
     frame.add(p);
     JScrollPane spta=new JScrollPane(ta);
     p.add(btn,BorderLayout.EAST);
     p.add(spta,BorderLayout.CENTER);
     p.add(send,BorderLayout.SOUTH);
     ta.setBackground(Color.cyan);
     frame.revalidate();
             ////set Client//
     try {      
        client=new Socket("127.0.0.1",8080);

        ta.setText("            -Connection-");

        ////set Output/////
         out1=new ObjectOutputStream( client.getOutputStream());
         out1.flush();



         btn.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent arg0) {
                try {
                    out1.writeObject(send.getText());
                    out1.flush();
                    ta.setText(ta.getText()+"\nYou:"+send.getText());

                } catch (IOException e1) {
                    e1.printStackTrace();
                }

                  if (send.getText().equals("Bye"))

                         try {
                             client.close();
                             frame.dispose();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

            }
        });

         //////set Input////
         ObjectInputStream in = new ObjectInputStream(client.getInputStream());
         Object inputLine; ;


         while(true){
             try {
                 out1.flush();
                inputLine = in.readObject();
                if(inputLine!=null){
                    ta.setText(ta.getText()+"\nClient:"+inputLine);
                    }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
             }




    } catch (IOException e) {
        e.printStackTrace();
    }
    }


public static void main(String[] args) {

            new ClientSide();


  }
  }

Client class:

import java.awt.*; 
import java.awt.event.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;



 public class ServerSide    {
ObjectOutputStream  out;
ServerSocket ss;
Socket so;
JFrame frame=new JFrame("Server");
///////
JTextArea ta=new JTextArea(20,10);
JTextArea send=new JTextArea(10,5);
JButton enter=new JButton("Enter");
    ////set Gui////

     ServerSide(){
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame .setVisible(true);
 frame. setSize(600,500);

 JPanel p=new JPanel(new BorderLayout());
 frame. add(p);
 JScrollPane spta=new JScrollPane(ta);

  p.add(enter,BorderLayout.EAST);
  p.add(spta,BorderLayout.CENTER);
  p.add(send,BorderLayout.SOUTH);
  ta.setBackground(Color.cyan);
  frame.revalidate();

 /////Server///
    try {
     int port=8080;
      System.out.println(port);
    ss = new ServerSocket(8080);
    so=ss.accept();
    ta.setText(ta.getText()+"\n            -Connection-");
     /////set Output////
      out = new ObjectOutputStream(so.getOutputStream());
      out.flush();

     enter.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

             try {
                 Object ob=send.getText();
                out.writeObject(ob);
                SwingUtilities.invokeLater(new Runnable(){

                    public void run() {
                            ta.setText(ta.getText()+"\nYou:"+send.getText());

                        }

                    });
            } catch (IOException e1) {
                e1.printStackTrace();
            }

              if (send.getText().equals("Bye"))

                     try {
                        so.close();
                        ss.close();
                        frame.dispose();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

        }
    });


     /////set Input///
     ObjectInputStream in = new ObjectInputStream(so.getInputStream());
     String ob ;
     while(true){
     try {

         ob = (String) in.readObject();
        if(ob!=null){
            ta.setText(ta.getText()+"\nClient:"+ob);
            }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
     }





} catch (IOException e) {
    e.printStackTrace();
}

 }





public static void main(String[] args) {

                new ServerSide();

    }
    }

exception:

    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.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at ControlMouseProject.ClientSide.<init>(ClientSide.java:41)
    at ControlMouseProject.ClientSide.main(ClientSide.java:104)

line 41:
     client=new Socket("IP",8080);
Gowtham
  • 11,853
  • 12
  • 43
  • 64
user3997118
  • 71
  • 2
  • 10
  • whats the error/exception? – rns Jan 17 '15 at 10:28
  • You must use the IP address of the other computer instead of 127.0.0.1. How are they connected? Some WLAN? – laune Jan 17 '15 at 10:59
  • i wrote 127.0.0.1 but when i change it to the computer IP i get EXCEPTION i know that if i want to connect to other computer i need to replace it...that what i did ...but still i get EXCEPTION..what it is wlan? – user3997118 Jan 17 '15 at 11:22
  • Could there be any hw or sw firewalls in the net? Can you connect from client comptuer to server computer using some other program. For example telnet or even browser? You can start your server and try to connect it.m – ikettu Jan 17 '15 at 20:35
  • tnx for comment i will check it later and another question how i can connect to computer in the same ROUTER?cause 127.0.0.1 wont help and thwy have the same IP... – user3997118 Jan 18 '15 at 05:27
  • i disabled the firewall and the server too... you see problem in the code? – user3997118 Jan 18 '15 at 14:03
  • someone please i need help... – user3997118 Jan 21 '15 at 18:26
  • Did you check the IP of the server computer? Have you tested the connection from client computer to server computer with some other program than your own client? For example with telnet. – ikettu Jan 23 '15 at 13:09
  • yes i checked the IP – user3997118 Jan 23 '15 at 18:59

1 Answers1

0

Disable personal firewall of your server PC. I had to disable the personal firewall of my server pc in my antivirus to fix this issue. Hope it will work for you too.