Java Waddo Meheta Warella...

djmaxtor

Well-known member
  • Dec 28, 2009
    3,526
    412
    83
    Machanz, mei pallehain thiyenne mama Java forums kihipayakama dapu post ekak, bt thama honda answer hambune na. ekai ape EK kollangen help ekak illanna hithuwe :(:(..

    Post eka godak digai wage penne source code ekath deela thiyena nisai, eka hinda danna kenek innawanum nobala yanna epa honde.. danne nathnum BUMP ekakwath dala yanna amathaka karanna epa..


    I wrote a simple chat program which perfectly runs on the local host. But, when the client tries to connect to the server via the Internet using the public IP and the port 9999, it does not work as it did on LAN.

    As many people have suggested, I did Port-Forwarding as shown in the following figure.

    0dc403b2f49703e068cd0ff7e44360566a84a8d410d687dc68d4665384a5cab86g.jpg


    After configuring Port-Forwarding
    this website indicates that port 9999 remains open for the specific public IP (I run the server on 192.168.1.100 on private network). Although the port remains open, the client cannot connect to the server. Then I disabled the Windows firewall and tried the same process. But the problem still persists.

    Please can someone tell me how can I solve this problem..?


    Thanks in advance

    Sorry for my bad English


    Here is the code of the Chat application..

    Server:
    Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    class Server{
        
        ServerSocket ss;
        Socket s;
        ArrayList al=new ArrayList();
        
        Server(){
            listen(9999);
        }
        
        public static void main(String[] args){
            new Server();
        }
        
        public void listen(int port){
            try{
                ss=new ServerSocket(port);
                
                while(true){
                    s=ss.accept();
                    
                    PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
                    al.add(pw);
                    new ServerThread(this,s);
                }
                
            }catch(Exception e){System.out.println(e);}
        }
        
        public void sendToAll(String msg){
            try{
                synchronized(al){
                    for(int i=0;i<al.size();i++){
                        PrintWriter pw=(PrintWriter)al.get(i);
                        pw.println(msg);
                        pw.flush();
                    }
                }
            }catch(Exception e){System.out.println(e);}
        }
        
    }
    
    class ServerThread extends Thread{
        
        Server server;
        Socket soc;
        
        ServerThread(Server server, Socket soc){
            this.server=server;
            this.soc=soc;
            start();
        }
        
        public void run(){
            try{
                BufferedReader br=new BufferedReader(new InputStreamReader(soc.getInputStream()));
                
                while(true){
                    String msg=br.readLine();
                    server.sendToAll(msg);
                }
                
            }catch(Exception e){System.out.println(e);}
        }
    }
    Client:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.net.*;
    import java.io.*;
    
    class Client extends JFrame implements Runnable{
        
        JPanel p1=new JPanel(new BorderLayout());
        JTextField tf=new JTextField();
        JTextArea ta=new JTextArea();
        JLabel lbl1=new JLabel("Message");
        JButton b1=new JButton("Send");
        
        PrintWriter pw;
        BufferedReader br;
        Socket s;
        
        Client(){
            p1.add("West",lbl1);p1.add("Center",tf);p1.add("East",b1);
            
            setLayout(new BorderLayout());
            add("Center",ta);add("South",p1);
            
            try{
                s=new Socket("112.134.190.41",9999);
                pw=new PrintWriter(s.getOutputStream(),true);
                br=new BufferedReader(new InputStreamReader(s.getInputStream()));
            }catch(Exception e){System.out.println(e);}
            
            b1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){send(tf.getText());}});
            
            new Thread(this,s.toString()).start();
            
            setSize(300,400);
            setVisible(true);
        }
        
        public void send(String msg){
            try{
                pw.println(msg);
                pw.flush();
                
            }catch(Exception e){System.out.println(e);}
        }
        
        public static void main(String args[]){
            new Client();
        }
        
        public void run(){
            try{
                while(true){
                    ta.append(Thread.currentThread().toString()+br.readLine()+"\n");
                }
            }catch(Exception e){System.out.println(e);}
        }
    }
    Forum Posts:
    Code:
    [URL]http://www.coderanch.com/t/584849/sockets/java/Socket-Programming-over-Internet#2662282[/URL]
    
    [URL]http://forums.codeguru.com/showthread.php?524849-Socket-Programming-over-the-Internet&p=2072821&posted=1#post2072821[/URL]
    [URL="http://www.javaprogrammingforums.com/java-networking/16296-socket-programming-over-internet.html#post69370"]
    http://www.javaprogrammingforums.com/java-networking/16296-socket-programming-over-internet.html#post69370[/URL]