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();
	}
}

Author Name :
Ranjith

Total : 33 Comments


33 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.

  15. sneha says:

    nice

  16. sneha says:

    nadkhullaaaaaaaaaa

  17. viktorpark says:

    This site is not even responding to the viewer’s reply…..plez do so,coz it will be of great help to us..
    well i also found that we cant reply frm client side….HELP NEEDED! urgent.

  18. rishiraj says:

    very nice….
    thanks!!!!

  19. SANJUBABA says:

    Nad ny Karycha

  20. Susrshsan Neupane says:

    very nice tutorial..if i need to use this type program in network what i will be do ?
    dear ,writer please write chatting system in GUI mode also

  21. Sudarshan Neupane says:

    very nice tutorial..if i need to use this type program in network what i will be do ?
    dear ,writer please write chatting system in GUI mode also

  22. babu says:

    nice code

  23. ramya says:

    pls find me solution for this program

    Write a client / server java program using TCP sockets, wherein the client sends the name of a program to be executed at the server. The server then sends the result of the executed program back to the client.

  24. Pankaj Sharma says:

    Thanks for this beautiful simple program. This helped me in a very big problem.

    Keep it up.

  25. pooja says:

    thank u gud to understant

  26. Visakh G says:

    Thank u very much………

  27. geetha says:

    i have typed and compiled the above two programs in different cmd prompt.
    I got compiled and created .class files for both. But i am getting “no classdeffound error”. how to solve this prob and run this program.

  28. ROBIN says:

    GUD YAR

  29. ruthanya says:

    Thank you very much because it is very very useful for all the student. it is the best one and i like it i wish to stay tuned to end of my job. simple program is easy to understand and once again thank you very much sir…………….

  30. mi says:

    how to executed

  31. bindusha says:

    Design a TCP/UDP Client Server System which exchanges messages by performing Encryption /Decryption using Caesar Cipher. The Client sends the data by encrypting using Caesar cipher and the server decrypts the cipher text .Extend the dialogue between client and server for initial key exchange followed by Data.

  32. bindusha says:

    please send me the code urgent

  33. pradip says:

    only server can send message to client.
    client can’t send message to server.
    what’s the use of this code ??????

Leave a Reply

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

Free email signup

Email: