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 2 of 2

Thread: client server program on a single machine

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default client server program on a single machine

    i hv done a program on a server client program on my own machine...i m posting the code..what i want to do is what i type in client site will be seen on the server site too..pls help me...

    Client...

    import java.io.*;
    import java.net.*;
     
    public class Client {
     
    	public static void main(String args[]) throws Exception	{
    	 	Socket cs = new Socket("localhost",6666);
    		int flag=1;
    	 	DataInputStream dis = new DataInputStream(System.in);
    		System.out.println("Client:");
    		ObjectOutputStream oos=new ObjectOutputStream(cs.getOutputStream());
    		while(flag==1)
                   {
    	 	//String str = dis.readLine();
    	 	//System.out.println(str);
    	 	oos.writeObject(dis.readLine());
    	 	if((str=="bye")||(str=="BYE"))
    			flag=0;
    		}
    		oos.close();
    		dis.close();
    	 cs.close();
    	}
    }


    Server...

    import java.io.*;
    import java.net.*;
     
     
    class Server
    	{
    	public static void main(String args[]) throws Exception
             {
    		ServerSocket ss=new ServerSocket(6666);
    		Socket ss1=ss.accept();
    		boolean flag=true;
    		DataInputStream dis=new DataInputStream(ss1.getInputStream());
    		System.out.println("connection established......");
    		System.out.println("server:");
    		ObjectOutputStream ps=new ObjectOutputStream(ss1.getOutputStream());
    		while(flag==true){
     
    			String str=dis.readLine();
    			System.out.println(str);
    			if((str=="bye")||(str=="BYE"))
    				flag=false;
    				}
    		System.out.println("connection closed....");
    		ps.close();
    		ss1.close();
    		ss.close();
     
    			}
    		}
    i m unable to produce the desired result..
    Last edited by Freaky Chris; December 16th, 2009 at 05:51 AM.


  2. #2
    Junior Member
    Join Date
    May 2009
    Posts
    25
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: client server program on a single machine

    I think the easiest way to get data from point A to point B trough socket is using DataInputStream and DataOutputStream

    DataInputStream (Java 2 Platform SE v1.4.2)
    DataOutputStream (Java 2 Platform SE v1.4.2)
    Last edited by Koâk; December 16th, 2009 at 02:05 PM.

Similar Threads

  1. Replies: 0
    Last Post: December 12th, 2009, 10:09 PM
  2. Simple server-client trouble
    By DC200 in forum Java Networking
    Replies: 3
    Last Post: November 12th, 2009, 08:16 AM
  3. Replies: 2
    Last Post: October 23rd, 2009, 02:34 AM
  4. Download a file to client from server
    By mvittalreddy in forum Java Networking
    Replies: 4
    Last Post: October 5th, 2009, 04:23 AM
  5. instead of printing in the client, report get printed in the server
    By jmvenkat in forum Java Theory & Questions
    Replies: 0
    Last Post: September 19th, 2009, 01:30 AM