java networking

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohana
    New Member
    • Jul 2006
    • 9

    java networking

    hi,
    i just want 2 communicate with the near by computer. i got this program from internet. i have two programs called Server.java and Client.java.

    i am putting that program here now,
    Server.java Program:

    import java.net.*;
    import java.io.*;

    public class Server
    {
    public static void main(String[] args) throws IOException{
    Socket client= null;
    PrintWriter pout=null;
    ServerSocket sock=null;

    try{
    sock= new ServerSocket(51 55);
    // now listen for connections

    while(true){
    client= sock.accept();

    // we have a connection
    pout = new PrintWriter(cli ent.getOutputSt ream(), true);

    // write the date to the socket
    pout.println(ne w java.util.Date( ).toString());

    pout.close();
    client.close();
    }
    }
    catch (IOException ioe) {
    System.err.prin tln(ioe);
    }
    finally {
    if (client !=null)
    client.close();
    if (sock !=null)
    sock.close();
    }
    }
    Client.java Program:

    import java.net.*;
    import java.io.*;

    public class Client
    {
    public static void main(String[] args) throws IOException{
    InputStream in = null;
    BufferedReader bin = null;
    Socket sock = null;

    try {
    // make connection to socket
    sock = new Socket("127.0.0 .1",5155);

    in = sock.getInputSt ream();
    bin = new BufferedReader( new InputStreamRead er(in));

    String line;
    while ((line= bin.readLine()) !=null)
    System.out.prin tln(line);
    }
    catch (IOException ioe) {
    System.err.prin tln(ioe);
    }
    finally{
    if (sock !=null)
    sock.close();
    }
    }
    }

    tell me wat shud i change in this program?
    is it correct doing this?
    Help me in doing this?
    its urgent



    }
  • Har8inho
    New Member
    • Jul 2006
    • 1

    #2
    Originally posted by mohana
    hi,
    i just want 2 communicate with the near by computer. i got this program from internet. i have two programs called Server.java and Client.java.

    i am putting that program here now,
    Server.java Program:

    import java.net.*;
    import java.io.*;

    public class Server
    {
    public static void main(String[] args) throws IOException{
    Socket client= null;
    PrintWriter pout=null;
    ServerSocket sock=null;

    try{
    sock= new ServerSocket(51 55);
    // now listen for connections

    while(true){
    client= sock.accept();

    // we have a connection
    pout = new PrintWriter(cli ent.getOutputSt ream(), true);

    // write the date to the socket
    pout.println(ne w java.util.Date( ).toString());

    pout.close();
    client.close();
    }
    }
    catch (IOException ioe) {
    System.err.prin tln(ioe);
    }
    finally {
    if (client !=null)
    client.close();
    if (sock !=null)
    sock.close();
    }
    }
    Client.java Program:

    import java.net.*;
    import java.io.*;

    public class Client
    {
    public static void main(String[] args) throws IOException{
    InputStream in = null;
    BufferedReader bin = null;
    Socket sock = null;

    try {
    // make connection to socket
    sock = new Socket("127.0.0 .1",5155);

    in = sock.getInputSt ream();
    bin = new BufferedReader( new InputStreamRead er(in));

    String line;
    while ((line= bin.readLine()) !=null)
    System.out.prin tln(line);
    }
    catch (IOException ioe) {
    System.err.prin tln(ioe);
    }
    finally{
    if (sock !=null)
    sock.close();
    }
    }
    }

    tell me wat shud i change in this program?
    is it correct doing this?
    Help me in doing this?
    its urgent



    }
    This program is correct!
    At first you must compile and run Server class and than Client

    Comment

    • mohana
      New Member
      • Jul 2006
      • 9

      #3
      it is throwing me ConnectionExcep tion error

      i dont no wat 2 do 4 this?

      Comment

      • mohana
        New Member
        • Jul 2006
        • 9

        #4
        hiiii,

        where shud i compile this?

        Computer1: server.java
        Computer2: Client.java. In this i am giving the IP address of that Server.

        In which computer i shud compile this?

        tell me . its urgent.

        Comment

        • aceka
          New Member
          • Aug 2006
          • 1

          #5
          You must look there must be same port (number), and same IP address to connect ot server. I'll give you another version. I have Net3.java and Net4.java. And I'll bring tomorrow.

          Comment

          Working...