Results 1 to 2 of 2
  1. #1
    FiniteRed is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Exclamation Simple TCP Client/Server not working?!?!?!

    Hi Everyone

    I have written a simple TCP Client and Server to allow a single client to connect to a server, Client sends some bytes and then the server send some back. Simple enough right? Well for some reason I cant get the server to send data back to the client

    The client sends data to the server just fine, and I get no errors from the application when I try to send some bytes back. Either the server is sending the bytes and client is not receiving them, or the server is not sending anything

    Any help would be greatly appreciated as im going a bit spair...

    FR

    Server Send:

    Java Code:
            
    OutputStream returnToClient = null;
            
    try {
                
        returnToClient = new DataOutputStream(clientSocket.getOutputStream());
                
        // Send data back to client
        if (length > 0) {
                    
            returnToClient.write(bytes, 0, length);
            returnToClient.flush();
                   
        } // if
                
        else System.out.println("Error, bytes to return length = 0");
           
    } // sendDataArray
            
    catch (IOException ex) {
        Logger.getLogger(JFlateServer.class.getName()).log(Level.SEVERE, null, ex);
    } // catch
            
    finally {
                
        try {
            returnToClient.close();
        } catch (IOException ex) {
            Logger.getLogger(JFlateServer.class.getName()).log(Level.SEVERE, null, ex);
        } // catch
               
    } // finally
    Client Rx:

    Java Code:
    DataOutputStream out = null;
    DataInputStream in = null;
    
        try {
    
            socket = new Socket(192.168.1.1, 1066);
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());
    
            if (bytes.length > 0) {
                        
                out.write(bytes, 0, bytesCounter);
                out.flush();
                        
            } // if
                    
            System.out.println("Avail: " + in.available()); // Something ANYTHING in here would be a good start!
                    
            socket.close();
    
        } catch (Exception e) {
            System.out.print("Whoops! It didn't work! " + e.getMessage());
        } // catch
                
        finally {
                    
            try {
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(JFlateClient.class.getName()).log(Level.SEVERE, null, ex);
            }
        } // finally
    Last edited by FiniteRed; 07-15-2011 at 02:16 PM. Reason: TMI

  2. #2
    FiniteRed is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default Solved

    Hi again

    Finally found out what was causing the issue, the Server was not processing the received packet until the client disconnected (blocking read) So once the client disconnected the packet was processed and data returned, but by this time the client was no longer there to hear the returned data...

    Got it working now

    Server Side Fix:

    Java Code:
    // Wait for data
    while ( dis.available() == 0 && dataCounter++ < RX_TIMEOUT ) {
                    
    	try {
    		Thread.sleep(1);
    	} catch (InterruptedException ex) {
    		Logger.getLogger(JFlateServer.class.getName()).log(Level.SEVERE, null, ex);
    	} // sleep
                    
    } // while
                    
    while ( dis.available() > 0 && (c = dis.read()) != -1) {
        data[bytesIn++] = ((byte) c);
    } // while
                    
    if(bytesIn == 0) {
        System.out.println("Client connected but failed to send any data within ~" + RX_TIMEOUT + "ms");
    } // if

Similar Threads

  1. Simple Client (2) - Server program
    By Reztem in forum New To Java
    Replies: 2
    Last Post: 01-09-2012, 03:05 AM
  2. problem with a simple client server app
    By Moorag in forum New To Java
    Replies: 0
    Last Post: 04-11-2011, 06:29 PM
  3. simple FTP server and FTP client
    By simontkk2005 in forum Networking
    Replies: 2
    Last Post: 11-17-2010, 11:38 AM
  4. Simple server-client
    By DC200 in forum New To Java
    Replies: 6
    Last Post: 12-09-2009, 06:13 PM
  5. Simple example Client Server Application
    By ferosh in forum Networking
    Replies: 1
    Last Post: 04-01-2007, 11:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •