Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need help with networking and Streams in Java

streamwriter stream

  • Please log in to reply
2 replies to this topic

#1 the_flirt

the_flirt

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 09 April 2010 - 09:15 AM

Hi,
I was asked to make a project in Java. A P2P voice chat client.

I understand that networking in Java requires creating objects of Socket and ServerSocket.
So isn't that client server networking? What do I need to do to make it P2P? Essentially whats the difference between client server and P2P, apart from the fact that in P2P the programs running on the 2 computers have to be the same?

Also, I just learnt about Input and Output streams. But I can't understand which stream to use when for example: I wanted to create a simple program to send a message from the client to the server and a reply from the server to the client.

Heres the code I came up with:

//server program
import java.net.*;
import java.io.*;

public class FServers {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        System.out.println("Connected...");
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

       
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

    String inputLine = "";
        while((inputLine = in.readLine()) != null)
        {
            System.out.println(inputLine);
        }
        out.write("THIS IS THE REPLY FROM THE SERVER TO THE CLIENT");
        
             
       
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

//Client program
import java.io.*;
import java.net.*;

public class FClient {
    public static void main(String[] args) throws IOException {

        Socket kkSocket = null;
        BufferedWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket("127.0.0.1", 4444);
            out = new BufferedWriter(new OutputStreamWriter(kkSocket.getOutputStream()));
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Error");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
     //   String fromUser;

                   
        
                out.write("HI THIS IS A CLIENT!");
        //fromServer=in.readLine();
        while((fromServer = in.readLine()) != null)
        {
            System.out.println(fromServer);
        }
           out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}
The connection is being accepted... but im not getting the messages displayed... either I'm not able to send and receive...or I'm doing something wrong in printing the message...
but this program works if I use PrintWriter instead of BufferedWriter... like this:

//server
import java.net.*;
import java.io.*;

public class KServer {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

       
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

        String inputLine = in.readLine();
                System.out.println("Client to Server "+inputLine);
        out.println("THIS IS THE REPLY FROM THE SERVER TO THE CLIENT");
        
             
       
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

//client
import java.io.*;
import java.net.*;

public class FClient {
    public static void main(String[] args) throws IOException {

        Socket kkSocket = null;
        BufferedWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket("127.0.0.1", 4444);
            out = new BufferedWriter(kkSocket.getOutputStream());
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String fromUser;

                   
        
                out.println("HI THIS IS A CLIENT!");
        fromServer=in.readLine();
        System.out.println("Server to client: "+fromServer);
           out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}
so basically I dont know when to use BufferedReader, BufferedWriter, BufferedInputStream, BufferedOutputStream, DataInputStream, DataOutPutStream, PrintWriter...etc...
If someone could explain...or just give me a link where its explained in simple language

Thanks in advance
  • 0

#2 the_flirt

the_flirt

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 13 April 2010 - 09:42 PM

Hello? Some help?
  • 0

#3 phpforfun

phpforfun

    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1056 posts

Posted 13 April 2010 - 10:54 PM

Do the needful, please advise
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download