Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java - Socket Programming

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Java - Socket Programming

    I am new to Socket Programming. I know this is simple question but I want to learn this.

    I am trying with simple example sending a message between client and server. But I am getting error and I couldn't figure what is the problem. Can Some one please help.

    How can I get the output (if I run the app.java alone) as

    From Process 6
    Message sent to client is 12

    From Server 12

    App.java---------------------->


    import java.io.IOException;


    public class App {

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

    try {


    Master master_objec = new Master();
    master_objec.start();

    Process process_object = new Process();
    process_object.start();

    }
    catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    }


    Master.java-------------------->
    public class Master {


    ServerSocket master_socket = null;
    private static Socket master_listen = null;
    final int PORT_NUMBER = 10004;
    PrintWriter printWriter = null;
    BufferedReader bufferedReader = null;


    public void start() throws IOException
    {
    try
    {

    master_socket = new ServerSocket(PORT_NUMBER);
    System.out.println("Server started. Awaiting connection requests...");

    while(true){
    //Input Stream
    master_listen = master_socket.accept();
    InputStream is = master_listen.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    int process_time = br.read();
    System.out.println("From Process "+ process_time);



    //Out put stream
    OutputStream os = master_listen.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(os);
    BufferedWriter bw = new BufferedWriter(osw);
    int new_process_time = process_time*2;
    bw.write(new_process_time);
    System.out.println("Message sent to the client is "+new_process_time);
    bw.flush();
    }
    }catch(IOException ioexception)
    {
    System.out.println("Error");

    }

    finally
    {
    try
    {
    master_listen.close();
    }
    catch(Exception e){}
    }
    }

    }


    Process.java------------------------------------>


    public class Process {


    private static Socket connects = null;
    int process_time =6;

    public void start() throws IOException{
    int port = 10004;
    connects = new Socket("localhost", port);

    OutputStream os = connects.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(os);
    BufferedWriter bw = new BufferedWriter(osw);
    bw.write(process_time);
    System.out.println("Message sent to the Server is "+process_time);
    bw.flush();


    InputStream is = connects.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    int process_time_new = br.read();
    System.out.println("From Master "+ process_time_new);


    }
    }

    This is the output I am getiing:

    java.net.BindException: Address already in use: JVM_Bind
    at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketBind(Unkno wn Source)
    at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    at java.net.PlainSocketImpl.bind(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at Master.start(Master.java:28)
    at App.main(App.java:12)
    Message sent to the Server is 6
    From Master 6
    Last edited by smylalwys; October 10th, 2014 at 07:49 AM.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java - Socket Programming

    The message says it all. The address you are trying to use is already being used by an other Socket. Each address can only be used once at a time.
    Perhaps you didnt kill your JVM properly when testing before or maybe another program is using this same address.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java - Socket Programming

    Hi Cornix,

    I even tried changing the port number but then when I run App.java, It just keep waiting for connection... saying
    Server started. Awaiting connection requests...

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java - Socket Programming

    This thread has been cross posted here:

    http://www.java-forums.org/networking/93187-java-socket-programming.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java - Socket Programming

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

Similar Threads

  1. Socket Programming
    By keepStriving in forum Java Networking
    Replies: 8
    Last Post: January 23rd, 2014, 07:34 AM
  2. java rmi and socket programming
    By sumit kumar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 21st, 2013, 12:28 AM
  3. Java Socket Programming
    By varunkukreja24 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 17th, 2013, 11:34 PM
  4. Java Socket and RMI programming help
    By Yogesh4591 in forum Java Networking
    Replies: 0
    Last Post: November 17th, 2012, 10:57 PM
  5. Java socket programming
    By lucy in forum Java Networking
    Replies: 1
    Last Post: April 26th, 2010, 09:13 PM

Tags for this Thread