java program for Client-Server Program using TCP/IP

Friday, April 30th, 2010

Program : Client-Server Program using TCP/IP
By : Kapil Lohia

Program 1:

 
import java.net.*;
import java.io.*;
 
class tcpip_server
{
	public static void main(String args[]) throws IOException
	{
		ServerSocket n1=null;
		try
		{
			n1=new ServerSocket(98);
		}
		catch(IOException e)
		{
			System.err.println("Port 98 could not be found");
			System.exit(1);
		}
		Socket c=null;
		try
		{
			c=n1.accept();
			System.out.println("Connection from "+c);
		}
		catch(IOException e)
		{
			System.out.println("Accept failed");
			System.exit(1);
		}
		PrintWriter out=new PrintWriter(c.getOutputStream(),true);
		BufferedReader in=new BufferedReader(new InputStreamReader(c.getInputStream()));
		String n;
		BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Ready to type now");
		while((n=sin.readLine())!=null)
		{
			out.println(n);
		}
		out.close();
		c.close();
		n1.close();
	}
}

Program 2

 
import java.net.*;
import java.io.*;
 
class tcpip_client
{
	public static void main(String args[]) throws IOException
	{
		Socket s=null;
		BufferedReader b=null;
 
		try
		{
			s=new Socket(InetAddress.getLocalHost(),98);
			b=new BufferedReader(new InputStreamReader(s.getInputStream()));
		}
 
		catch(UnknownHostException u)
		{
			System.err.println("I don't know host");
			System.exit(0);
		}
		String inp;
		while((inp=b.readLine())!=null)
		{
			System.out.println(inp);
		}
		b.close();
		s.close();
	}
}
Avatar Image

Author Name :
Ranjith

Total : 14 Comments


14 Responses to “java program for Client-Server Program using TCP/IP”

  1. chaitra says:

    java program for Client-Server Program using TCP/IP

  2. Rana says:

    firstly thank you so much for the help ! this is really a wonderful helpful site for the students. I want to stay tuned with ELECTROFRIENDS for lifelong. once again ,thank you so very much!

  3. gagandeep says:

    thanks dear….

  4. durgesh says:

    thnx for the help !!!!!

  5. rohit says:

    its really helpfull

  6. Mike Acker says:

    Very nice post.I was seeking like this post Green Life

  7. r.thilagavathy says:

    thank u sir

  8. mohan says:

    good job.and thanks

  9. vignesh says:

    thanks ……. really it is helpful

  10. DEEPIKA.J says:

    Hi………. not even single error in ur codes…. thank u very much!!!! REALLY AWESOME!!!!

  11. golu_tera_molu says:

    bakwas… whr to give the ip address and port num.

  12. hum_randi_hai says:

    aa bacho log randi baji karo.. inn sab main kya pada hai..
    randi khane main jo maja hai yaha nahi

  13. jayachandra says:

    In the above example we are just replying from server only, can you make it how to reply from client also.

  14. jayachandra says:

    In the above example we are just replying from server only, can you make it how to reply from client side.

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup