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

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

  1. Sudarshan 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
  2. 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.

    Reply
  3. Pankaj Sharma

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

    Keep it up.

    Reply
  4. geetha

    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.

    Reply
  5. ruthanya

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

    Reply
  6. 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.

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

    Reply

Leave a Reply