testing a client server

I wrote a server that listens for client messages, it's a variation of http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html. I wrote them both in eclipse as java classes in the same project. To test it I have a client class with a main that starts the server and then sends messages to it. When I run it the program just hangs at serverSocket.accept(); according to the javadoc for ServerSocket accept is not asynchronous? That would explain the hanging, but then how does the tutorial code work then?

UPDATE - here is my working code: Here is the working code:


/*imports neglected for brevity */
public class MyServer {
public static final String hostname = "localhost";
public static final int portNum = 4444;

ServerSocket serverSocket;
BufferedReader serverReader;

File serverLog;
FileWriter fw;
BufferedWriter serverWriter;

Socket clientSocket;

public static void main(String[] args) {
MyServer server = new MyServer(portNum);
    // start the server so it can listen to client messages


public MyServer(int portNum) {

    try {
        // endpt for server side, used to listen for client socket
        serverSocket = new ServerSocket(portNum);

        /* have server socket listen for connection, return client socket.
         * serverSocket can now talk to clientSocket
        clientSocket = serverSocket.accept();

        // server writes messages to this log
        serverLog = new File("ServerLog.txt");
        fw = new FileWriter(serverLog.getAbsoluteFile());
        serverWriter = serverWriter = new BufferedWriter(fw);

        /* server reads from this stream that is populated by the client's
         * OUTPUT stream/client socket's INPUT stream
        serverReader = new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream())
    catch (Exception e) {

public void start() {
    String clientMsg;
    try {
        while((clientMsg = serverReader.readLine()) != null) {
            if(clientMsg.startsWith("exit")) {
    } catch (IOException e) {
        // TODO Auto-generated catch block


MyClient.java public class MyClient { public static final String hostname = "localhost"; public static final int portNum = 4444;

public static void main(String[] args) {

    String msg = "message 1";
    try {
        // server is listening on http://localhost:4444
        Socket serversSocket = new Socket(hostname, portNum);
        PrintWriter clientOut = new     PrintWriter(serversSocket.getOutputStream(), true);

        // send first message

        msg = "message 2";
        // send second message

        msg = "exit";
        // this will stop the server
    catch (Exception e) {



The tutorial assumes you are not running them in the same program. If you must run them in the same program, then start your server in a separate thread.

If you go through the tutorial it creates two applications one with client one with server.

You cannot create a variation like this as, when you call the constructor, your whole application blocks in clientSocket = serverSocket.accept();.

If you insist on creating a single application for whatever reason, you can do multithreading. But I do not see any reason why you would want to do that.

if you have an android phone you can test this with the app TCP socket make sure you PortForward the port the server is listening to. some isp also block ports so make sure with your isp that all ports are open trust me broke my head on this one :)


also make sure your server has the public ip and not local ip

if you test this localy then the code above is fine if not you will need to add

bind(SocketAddress endpoint)
/*Binds the ServerSocket to a specific address (IP address and port number).*/

you can find your ip by typeing in google: whats my ip

Need Your Help

Insert Post Title into the title tag of thumbnail link

php wordpress

This is what I have and I am able to insert the Post Title into the title value of the thumbnail, but instead I want it to be inserted into the title value of the link that is wrapped around it.

Converting realtime video into an array using Matlab

arrays matlab real-time frame video-processing

I have a problem with realtime video processing in Matlab for gesture recognition. I have tried