java program for Client-Server Program using TCP/IP

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

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

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

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

  1. 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!

    Reply
  2. DEEPIKA.J

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

    Reply
  3. hum_randi_hai

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

    Reply
  4. jayachandra

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

    Reply
  5. jayachandra

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

    Reply
  6. viktorpark

    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.

    Reply
  7. Susrshsan Neupane

    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

    Reply

Leave a Reply