This discussion is archived
0 Replies Latest reply: Dec 12, 2013 7:35 AM by 70e1c532-ca89-47d2-83c6-b34b8d6444c1 RSS

Message not sent when using IP-Address

70e1c532-ca89-47d2-83c6-b34b8d6444c1 Newbie
Currently Being Moderated

Hello,

I have a little problem using sockets. I've got a client and a server (code below) and when I'm using "localhost" I can connect to the Server and send messages to it. But as soon as I replace "localhost" through my IP/DynDNS Address, I can't send messages anymore. I get a connection (ServerSocket.accept() returns a Socket, "Connection" is printed), but the messages are only sent, if I call socket.getOutputStream.close().

 

Client:

 

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Client {
    private Socket socket;
    public static void main(String[] args) {
        new Client().connect();
    }
    
    public void connect() {
        try {
            socket = new Socket();
            socket.connect(new InetSocketAddress("letorat.selfhost.bz", 5001), 3000); //When using localhost everything works fine
            JFrame frame = new JFrame();
            frame.setSize(500,500);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JButton btn = new JButton("Klick");
            btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        sendMessage("Hi");
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            });
            frame.add(btn);
            frame.setVisible(true);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void sendMessage(String s) throws IOException {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write(s);
        writer.newLine();
        writer.flush(); //I have to add writer.close() in order to send the message!
    }
} //0

 

And the Server:

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
    
    public static void main(String[] args) {
        new Server().go();
    }
    
    public void go() {
        try {
            @SuppressWarnings("resource")
            ServerSocket serverSocket = new ServerSocket(5001);
            while(true) {
                Socket clientSocket = serverSocket.accept();
                Thread t = new Thread(new ClientHandler(clientSocket));
                t.start();
                System.out.println("Connection"); //Connection is printed!
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public class ClientHandler implements Runnable {
        private BufferedReader br;
        private Socket socket;
        private PrintWriter pw;
        
        public ClientHandler(Socket clientSocket) {
            try {
                socket = clientSocket;
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        public void run() {
            try {
                String msg;
                while ((msg = br.readLine()) != null) {
                    System.out.println(msg);
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points