Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-09-2013, 11:33 AM   PM User | #1
mruser11
New to the CF scene

 
Join Date: Feb 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
mruser11 is an unknown quantity at this point
Problem with my Client Server program

Hi guys!

I have made a multithreaded Server and client program in which the client connects, has a little discussion, is presented with a list of options for file download and is then meant to download the selected file.

My problem is that the discussion happens fine and the options are presented but after the server sends "Sending File" no file is received at the client end.

My client is below:
import java.io.*;
import java.net.*;

Code:
public class Client {
    //define states
    private static final int WAITTEXT = 1;
    private static final int WAITFILE = 2;
    //and our current state
    private static int state = WAITTEXT;
    
    public static void main(String[] args) throws IOException {
 
        Socket ourSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
 
        //try and connect
        try {
            ourSocket = new Socket("127.0.0.1", 2222);
            out = new PrintWriter(ourSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(ourSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Can't find host: 127.0.0.1");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to 127.0.0.1");
            System.exit(1);
        }
 
        //set up
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String fromUser;
        OUTER:
        while ((fromServer = in.readLine()) != null) {
            //if we are waiting for text do the below
            if(state == WAITTEXT){
                System.out.println("Server: " + fromServer);
                switch (fromServer) {
                    case "Connection Closed":
                        break OUTER;
                    case "Sending File":
                        state = WAITFILE;
                        break;
                }
                if(state == WAITTEXT){
                    fromUser = stdIn.readLine();
                    if (fromUser != null) {
                        System.out.println("Client: " + fromUser);
                        out.println(fromUser);
                    }
                }
            }//else if we are waiting for a file
            else if(state == WAITFILE){
                int bytesRead;
                int current = 0;
                byte [] mybytearray = new byte [6022386];
                InputStream is = ourSocket.getInputStream();
                FileOutputStream fos = new FileOutputStream("name.file");
                BufferedOutputStream bos;
                bos = new BufferedOutputStream(fos);
                bytesRead = is.read(mybytearray,0,mybytearray.length);
                current = bytesRead;
                do {
                    bytesRead = is.read(mybytearray,current,(mybytearray.length-current));
                    if(bytesRead >= 0) {
                        current += bytesRead;
                    }
                } while (bytesRead > -1);
                bos.write(mybytearray,0,current);
                bos.flush();
                bos.close();
                System.out.println("Client: Received");
                state = WAITTEXT;
            }
        }
 
        //close everything
        out.close();
        in.close();
        stdIn.close();
        ourSocket.close();
    }
}
I have posted the server thread code http://pastebin.com/JtHcB9SC
I don't think it is anything to do with the server side, but as far as I can see with the use of states on the client side everything should be fine!

Any help or pointers at all would be appreciated!
mruser11 is offline   Reply With Quote
Reply

Bookmarks

Tags
client, server

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:19 PM.


Advertisement
Log in to turn off these ads.