Results 1 to 7 of 7
  1. #1
    ja107 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Client to Client communication

    Is it possible for me to send a message to my own computer by creating a socket using my own local ip and a random port above 1024?




    try {

    InetAddress ip=InetAddress.getLocalHost();
    InetAddress host = InetAddress.getByAddress(new byte []{(byte) 192,(byte)168,(byte)1,(byte)100});


    System.out.println(getMYIP());
    echoSocket = new Socket(ip, 1098);
    System.out.println("sandish");
    out = new PrintWriter(echoSocket.getOutputStream(), true);
    System.out.println("askurn");
    in = new BufferedReader(new InputStreamReader(
    echoSocket.getInputStream()));
    } catch (UnknownHostException e) {
    System.err.println("Don't know about ipadd: ip");
    System.exit(1);
    } catch (IOException e) {
    System.err.println("Couldn't get I/O for "
    + "the connection to: ip");
    System.exit(1);
    }

    The above is part of the code. I get the IO exception error.

    Could anyone help me with this?

    Thank you.

    Sandy

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    post the exception
    freedom exists in the world of ideas

  3. #3
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default

    Yes. If you mean setting up 1 program on your computer. And on the same computer setting up another, different, program. And then have them communicate over sockets.

    As far as I know one of the programs needs to be set up with a Server Socket. And the other a Client socket.

    But I could be wrong I am only very new to Network programming.

  4. #4
    vdparaliya is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Thumbs up Solution for Client to Client Communication

    Try this coding I think u can get ur solution


    import java.net.*;
    import java.rmi.*;
    import java.io.*;
    import java.util.*;
    public class ClientToClient
    {
    private static int ch;
    private static Scanner sc = new Scanner(System.in);
    private static ServerSocket ss;
    private static Socket sk;
    private static int port;
    private static String ipAdd = null;
    public static void main(String arg[])
    {

    port = (new Integer(arg[0]).intValue());
    try{
    ipAdd = (InetAddress.getLocalHost()).toString();
    System.out.println("\n**************************** **********************\n");
    System.out.println("IP \t: " + ipAdd);
    System.out.println("\nPort \t: " + port);
    do
    {
    choiceMenu();
    if(ch==1)
    {
    AcceptConnection();
    }
    else
    if(ch==2)
    {
    RequstConnection();
    }
    else
    if(ch==3)
    {
    SendMsg();
    }
    else
    if(ch==4)
    {
    DisplayMsg();
    }
    }
    while(ch!=5);
    }catch(Exception e)
    {}
    }
    private static void choiceMenu()
    {
    System.out.println("\n**************************** **********************");
    System.out.println("*********************| MENU |*********************");
    System.out.println("--------------------------------------------------\n");
    System.out.println("1] \t Accept Connection");
    System.out.println("2] \t Request for Connection");
    System.out.println("3] \t Send Message");
    System.out.println("4] \t Display Message");
    System.out.println("5] \t Exit");
    System.out.print("\nChoice A Option : ");
    ch=sc.nextInt();
    }
    private static void AcceptConnection()
    {
    int rePort = 0;
    try{
    System.out.print("Enter Port : ");
    rePort = sc.nextInt();
    ss = new ServerSocket(rePort);
    sk = ss.accept();
    System.out.println("Request Accepted");
    }catch(Exception e)
    {}
    }
    private static void RequstConnection()
    {
    String reIp = null;
    int rePort = 0;
    try{
    System.out.print("Enter IP : ");
    reIp=sc.next();
    System.out.print("Enter Port : ");
    rePort=sc.nextInt();
    sk = new Socket(reIp,rePort);
    System.out.println("Request Accepted");

    }catch(Exception e)
    {}
    }
    private static void SendMsg()
    {
    String msg = null;
    try{

    System.out.print("Enter Message : ");
    msg = sc.toString();
    ObjectOutputStream oos = new ObjectOutputStream(sk.getOutputStream());
    oos.writeObject(msg);
    }catch(Exception e)
    {}
    }
    private static void DisplayMsg()
    {
    try{

    ObjectInputStream ois = new ObjectInputStream(sk.getInputStream());
    String msg = (String)ois.readObject();
    System.out.println("Messesage : " + msg);

    }catch(Exception e)
    {}
    }
    }

    Now Use this Programme like below

    javac ClientToClient.java
    start java ClientToClient 3335

    come back in command window
    start java ClientToClient 3336

    now you have two client window use menu and u can send messege from one window to another window first please send request to connect and accept request of connection in another window then u can send and receive messages from one window to another if you like then please reply me or if you have any problem then tell me I will try to solve ur problem

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    @vdparaliya

    Please use code tags when posting code.

  6. #6
    kamaltomail is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi ..

    I am not able to excute ur code .. can u pls say the steps to do it .. when i do it it thrus exception

  7. #7
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Basing on the code of vdparaliya,
    the logic of Client to Client is mostly present there.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Similar Threads

  1. send message from a client to another client
    By esko in forum Networking
    Replies: 2
    Last Post: 02-04-2010, 07:21 PM
  2. Replies: 5
    Last Post: 11-01-2009, 05:16 PM
  3. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  5. client-server communication problem
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 02:21 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
  •