tags:

views:

82

answers:

2

Hi, These are two classes that one of them is in my Client application and the other is in Server application.

my MainServer class :

public class MainServer {

static Socket client = null;
static ServerSocket server = null;
static BufferedReader in;
static PrintWriter out;
static String line;

public static void main() {
    System.out.println("Server is starting...");
    System.out.println("Server is listening...");

    try {
        server = new ServerSocket(5050);
    } catch (IOException ex) {
        System.out.println("Could not listen on port 5050");
        System.exit(-1);

    }
    try {
        client = server.accept();
        System.out.println("Client Connected...");
    } catch (IOException e) {
        System.out.println("Accept failed: 5050");
        System.exit(-1);
    }

    try {
        in = new BufferedReader(new InputStreamReader(
                client.getInputStream()));
        out = new PrintWriter(client.getOutputStream(),
                true);
    } catch (IOException e) {
        System.out.println("Read failed");
        System.exit(-1);
    }
    while (true) {
        try {
            line = in.readLine();
//Send data back to client
                out.println(line);
            } catch (IOException e) {
                System.out.println("Read failed");
                System.exit(-1);
        }
    }

}}

my MainClient class :

public class MainClient implements Runnable {

private static InformationClass info = new InformationClass();
private static Socket c;
private static String text;

public static String getText() {
    return text;
}

public static void setText(String text) {
    MainClient.text = text;
}
private static PrintWriter os;
private static BufferedReader is;
static boolean closed = false;

/**
 * @param args the command line arguments
 */
public static void runAClient() {
    try {
        c = new Socket("localhost", 5050);

        os = new PrintWriter(c.getOutputStream(), true);
        is = new BufferedReader(new InputStreamReader(c.getInputStream()));
        System.out.println(is);
        String teXt = getText();
        os.println(teXt);

        String line = is.readLine();
        System.out.println("Text received: " + line);




    } catch (UnknownHostException ex) {
        System.err.println("Don't know about host");

    } catch (Exception e) {
        System.err.println("IOException:  " + e);

    }

}

public void run() {
    String responseLine;

    try {
        while ((responseLine = is.readLine()) != null) {
            System.out.println(responseLine);
            if (responseLine.indexOf("*** Bye") != -1) {
                break;
            }
        }
        closed = true;
    } catch (IOException e) {
        System.err.println("IOException:  " + e);
    }

}}

when I write on the text area of client and then click on the Send button : on the Server console these line will be written==>

init:
deps-jar:
compile-single:
run-single:
Server is starting...
Server is listening...
Client Connected...
java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:168)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
        at java.io.InputStreamReader.read(InputStreamReader.java:167)
        at java.io.BufferedReader.fill(BufferedReader.java:136)
        at java.io.BufferedReader.readLine(BufferedReader.java:299)
        at java.io.BufferedReader.readLine(BufferedReader.java:362)
        at ServerNetwork.MainServer.main(MainServer.java:67)
Java Result: -1
BUILD SUCCESSFUL (total time: 46 seconds)

and on the client console these lines will be written==>

init:
deps-jar:
compile-single:
run-single:
java.io.BufferedReader@18a7efd
Text received: null
BUILD SUCCESSFUL (total time: 41 seconds)

I have edited my post.please help me! i am beginner in network[:-(]

EDIT:please start reading from this part: these are two classes ,one for gui and the other for client.(network) it returns nothing on the console for server so it will return nothing to the client.please help me thanks. my GUI class:(a part of that) (like a chat frame which by clicking on the Send button .I will send something for the server)

my gui class(chat frame) a part of that:

private void SendActionPerformed(java.awt.event.ActionEvent evt) {                                         
setButtonIsSelected(true);
submit();
clear();
} 
 private void submit() {




String text = jTextArea1.getText();


jTextArea2.append(client.getCurrentName() + " : " + text + "\n");
MainClient.setText(client.getCurrentName() + " : " + text + "\n");
}

private static boolean buttonIsSelected = false ;

public static boolean isButtonIsSelected() {
    return buttonIsSelected;
}

public static void setButtonIsSelected(boolean buttonIsSelected) {
    ChatFrame.buttonIsSelected = buttonIsSelected;
}

my MainClient class:(a part of that)

show a chat frame.

if ( ChatFrame.isButtonIsSelected() == true) {

        String teXt = getText();
        System.out.println(teXt);
        os.println(teXt);
        String line;
        line = is.readLine();
        System.out.println("Text received: " + line);

    }

at first I will run the client class So the gui class will be run which name is chat Frame.

A: 

The server closes the connection before any data is sent so the client gets null (which means "end of file").

Aaron Digulla
I think all `PrintWriter` are consturected with (autoFlush = true).
bryantsai
+4  A: 

getText() is returning null, so you're writing "null" down to the server. At least, that's what I found after changing your just enough to get it to compile and run.

After changing getText() to return "hello" I got:

Server:

Server is starting...
Server is listening...
Client Connected...

Client:

java.io.BufferedReader@7d2152e6
Text received: hello

(The "BufferedReader@7d..." line is due to System.out.println(is);)

Note that after the client has disconnected, your server is going to keep trying to send "null" back to it, because you don't test for line being null in the server code.

Jon Skeet