Results 1 to 2 of 2
  1. #1
    jesroni is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Question java twoway communication

    hi i am new to java i wrote following code for two way communication..it worked well
    i have a 2 questions
    1)when i close server client does not closed..how to close it?
    2)when i run a program server and client start a chat .client send one message to server it received..and client also received problem is client send 2 message one by one send and receive how to resolve this?
    Server:
    import java.io.*;
    import java.net.*;
    class MultiServer
    {
    public static void main(String[] args) throws IOException
    {
    ServerSocket serverSock=new ServerSocket(3000);//server socket creation
    System.out.println("waiting for client.........");
    Socket socket=serverSock.accept();//listening client connection and accept the connection
    System.out.println("client connected ");
    BufferedReader keyRead=new BufferedReader(new InputStreamReader(System.in));//reading from keyboard(keyRead object)
    OutputStream ostream=socket.getOutputStream();//sending to client
    PrintWriter pw=new PrintWriter(ostream,true);
    InputStream istream=socket.getInputStream();//receiving from server(istream object)
    BufferedReader receiveRead=new BufferedReader(new InputStreamReader(istream));//Stream for receiving data from client
    String receiveMessage,sendMessage;
    while(true)
    {
    if((receiveMessage=receiveRead.readLine())!= null)
    {
    System.out.println("client:>"+ receiveMessage);//receive the message from client
    }

    sendMessage=keyRead.readLine();
    pw.println(sendMessage);
    System.out.flush();//flush the Stream
    if(sendMessage.equals("bye"))
    {
    break;
    }
    }
    }
    }
    Client:
    import java.io.*;
    import java.net.*;
    public class MultiClient
    {
    public static void main(String[] args) throws IOException
    {
    Socket clientSock=new Socket("192.168.1.113", 3000);//socket creation
    System.out.println("Client connected to the server");
    //reading from keyboard(keyRead object)
    BufferedReader keyRead=new BufferedReader(new InputStreamReader(System.in));
    OutputStream ostream=clientSock.getOutputStream();//communication stream associated with socket
    //sending message to client (pwrite object)
    PrintWriter pwrite=new PrintWriter(ostream,true);
    InputStream istream=clientSock.getInputStream();
    //receiving from server(receiveRead object)
    BufferedReader receiveRead=new BufferedReader(new InputStreamReader(istream));
    System.out.println("to Start the chat, type message and press Enter key");
    String receiveMessage , sendMessage;
    while(true)
    {
    sendMessage=keyRead.readLine();//keyboard reading
    pwrite.println(sendMessage);//sending to server
    System.out.flush();
    if((receiveMessage=receiveRead.readLine())!=null)//receive from server
    {
    System.out.println("server:>"+receiveMessage);//displaying message
    }
    if(sendMessage.equals("bye"))
    {
    break;
    }
    }

    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: java twoway communication

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

Similar Threads

  1. Replies: 1
    Last Post: 12-08-2012, 03:27 AM
  2. Java Arduino Communication
    By Anel Murselji in forum Advanced Java
    Replies: 8
    Last Post: 06-22-2011, 01:03 PM
  3. Java -> Web Communication
    By Johnb1 in forum Networking
    Replies: 0
    Last Post: 01-17-2011, 07:37 PM
  4. Java and C++ communication
    By mahesh.gmb in forum Advanced Java
    Replies: 13
    Last Post: 01-04-2011, 04:22 PM
  5. Java Servlet and Web Service communication
    By shussain@del.aithent.com in forum Java Servlet
    Replies: 36
    Last Post: 03-16-2010, 08:28 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
  •