0

I am trying to learn network programming in java so far i have written 2 codes, one for client side, one for server side. First i run serverCode and then i run client code however client is recieving null on its end.

Server Side code:

import java.net.*;
import java.io.*;
/**
 *
 * @author saksham
 */
public class Chatserver {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        ServerSocket serverSocket=new ServerSocket(6966);
        while (true){
            Socket request=serverSocket.accept();
            System.out.println("Connection established");
            PrintWriter pw=new PrintWriter(request.getOutputStream());
           // InputStreamReader ir=new InputStreamReader(request.getInputStream());
            //BufferedReader bf=new BufferedReader(ir);
            //String msg=bf.readLine();
            pw.println("you sent me the message:");
            request.close();


        }

    }

}

Client side code:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.io.*;
import java.util.Scanner;

/**
 *
 * @author saksham
 */
public class Chat {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        Socket socket=new Socket("127.0.0.1",6966);
       // PrintWriter pw=new PrintWriter(socket.getOutputStream());
        //System.out.println("\nEnter a message:->");
        //Scanner sc=new Scanner(System.in);
        //pw.println(sc.next());
        InputStreamReader ir=new InputStreamReader(socket.getInputStream());
        BufferedReader br=new BufferedReader(ir);
        String rcvd=br.readLine();
        socket.close();
        System.out.println(rcvd);
        rcvd=br.readLine();
        System.out.println(rcvd);


    }

}

I am very much aware i am not using a good coding style its just a test code since i am focussing on learning for now.

3
  • You might not want to close the socket before you are done reading from it. Commented Aug 19, 2017 at 20:51
  • atleast one message should have been printed right? Commented Aug 19, 2017 at 21:05
  • 1
    You need to close, or at least flush, the PrintWriter in class ChatServer before closing the socket. Commented Aug 19, 2017 at 23:28

1 Answer 1

2

You should have closed pw, not the socket. At present your output is still buffered in the PrintWriter, and never gets sent at all.

When you've fixed that:

  1. You are sending one line and then closing the socket.

  2. You are reading two lines. The first readLine() will return the line you sent. The second readLine() will return null, indicating end of stream.

Sign up to request clarification or add additional context in comments.

2 Comments

I added two lines of code as you mentioned they are pw.flush() pw.close() before closing the socket stil I am getting null two times while it should be null only once But when i removed the line that closes socket, problem got solved any explainations for that?
You didn't do what I said, or what you've claimed. I told you to close pw and not the socket. Closing pw closes the socket. If you closed the socket after pw, it would have worked. If you closed the socket before pw, neither the flush nor the close of pw could possibly have had any effect. Ergo if it didn't work, you closed the socket first. NB Flush before close is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.