Results 1 to 4 of 4
  1. #1
    smylalwys is offline Member
    Join Date
    Oct 2014
    Posts
    2
    Rep Power
    0

    Unhappy 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 getting:


    Message sent to the Server is 6
    From Master 6
    Last edited by smylalwys; 10-10-2014 at 03:01 PM.

  2. #2
    smylalwys is offline Member
    Join Date
    Oct 2014
    Posts
    2
    Rep Power
    0

    Unhappy 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

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Java - Socket Programming

    There is another process listening on port 10004. Most likely a previous run of your program. Make sure to kill all your (server) programs before starting another.
    Last edited by SurfMan; 10-10-2014 at 09:36 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Java - Socket Programming

    Forum Rules

    I've merged two threads here. Next time, don't cross post.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Socket programming between actionscript3 and java
    By rahulrahul in forum Networking
    Replies: 2
    Last Post: 02-25-2013, 09:14 AM
  2. Java Socket Programming
    By ramyaarian in forum Networking
    Replies: 1
    Last Post: 04-19-2012, 01:19 AM
  3. Socket Programming ERROR : Socket not connected
    By vishrut_n_shah in forum Networking
    Replies: 0
    Last Post: 11-04-2011, 09:32 AM
  4. Socket Programming
    By blutao in forum New To Java
    Replies: 4
    Last Post: 10-07-2010, 06:29 AM
  5. socket programming
    By Omarero in forum New To Java
    Replies: 5
    Last Post: 12-18-2008, 04:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •